I am learning C# and I made a console application that reads a text entry and searches an API that returns a value in JSON, then I show these values by console. Now I need to pass this to a graphical environment and what I have done is create a class "MyClass" and within that class I have created a method "MyMethod" that will receive as a parameter a 15 digit number and will query the API (such as the console application did) and then return those values.
What I do not know is how to create the method correctly and how to instantiate it from another class because it generates errors.
My JSON:
{
"success": true,
"result": {
"id": "20429683581",
"name": "CINEPLEX S.A",
"Condition": "HABIDO",
"PLE": "02\/01\/2013",
"lawyers": [
{
"type": "DNI",
"numdoc": "07871885",
"name": "PONCE PINTO ALEJANDRO EDUARDO",
"date": "22\/08\/2000"
},
{
"type": "DNI",
"numdoc": "09333203",
"name": "SORIANO BARRANTES JOSE FERNANDO",
"date": "22\/09\/2008"
}
],
"workers": [
{
"time": "2017-07",
"service": "8"
},
{
"time": "2018-06",
"service": "13"
}
]
}
}
I have these defined classes:
public class Lawyer
{
public string type { get; set; }
public string numdoc { get; set; }
public string name { get; set; }
public string date { get; set; }
}
public class Worker
{
public string time { get; set; }
public string service { get; set; }
}
public class Result
{
public string id { get; set; }
public string name { get; set; }
public string Condition { get; set; }
public string PLE { get; set; }
public List<Lawyer> lawyers { get; set; }
public List<Worker> workers { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public Result result { get; set; }
}
In a console application they worked well:
RootObject rootobject;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.example.com/api/?get=" + inid);
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
{
var json = reader.ReadToEnd();
rootobject = JsonConvert.DeserializeObject<RootObject>(json);
}
if (rootobject.success == true)
{
Console.WriteLine("---------------");
Console.WriteLine("ID: " + rootobject.result.id);
Console.WriteLine("NAME: " + rootobject.result.name);
Console.WriteLine("CONDITION: " + rootobject.result.Condition);
Console.WriteLine("PLE: " + rootobject.result.PLE);
}
else
{
Console.WriteLine("---------------");
Console.WriteLine("NOTHING");
}
}
catch (Exception)
{
Console.WriteLine("---------------");
Console.WriteLine("ERROR");
}
I have since migrated to a GUI based application using MyClass:
class MyClass
{
public void MyMethod(int inRUC){
RootObject rootobject;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.example.com/api/?get=" + inRUC);
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
{
var json = reader.ReadToEnd();
rootobject = JsonConvert.DeserializeObject<RootObject>(json);
}
}
}
Now I want to get the value of a text entry in the graphic form and instantiate the class to get the results and print them in the text fields of the same form, but I do not know how to instantiate it correctly and how to place the values correctly, this is what I am currently trying: (txtRuc1) is my input of my form and EditText1 is my output of my form:
private void Button1_ClickBefore(object sboObject, SAPbouiCOM.SBOItemEventArg pVal, out bool BubbleEvent)
{
if (string.IsNullOrEmpty(txtRuc1.Value.ToString()))
{
BubbleEvent = false;
}
else
{
//Ok, I known that it is wrong
BubbleEvent = true;
MyClass obj = new Sunat();
EditText1.Value = obj.MyMethod(txtRuc1.value).rootobject.result.name;
}
}
What would be the correct form? Visual studio returns the error: Cannot convert "string" to "int" in these line:
obj.MyMethod(txtRuc1.value).rootobject.result.name
EditText1class code?obj.MyMethod(int.Parse(txtRuc1.value)).rootobject.result.name