1

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
3
  • Could you put the json data and EditText1 class code? Commented Sep 2, 2018 at 21:31
  • yes, I just put it Commented Sep 2, 2018 at 21:35
  • You need to parse the string into an int: obj.MyMethod(int.Parse(txtRuc1.value)).rootobject.result.name Commented Sep 2, 2018 at 21:37

1 Answer 1

2

first, your MyMethod need to return RootObject instead of void

public RootObject MyMethod(int inRUC){
    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();
        return JsonConvert.DeserializeObject<RootObject>(json);
    }
}

then the error was caused by txtRuc1.value might string value but the MyMethod method needs int.

so you can try to use int.TryParse method to parse the input value to int the pass it.

int para = 0;
int.TryParse(txtRuc1.value,out para);

EditText1.Value = obj.MyMethod(para).rootobject.result.name;
Sign up to request clarification or add additional context in comments.

2 Comments

thank you!! a little question...This method will call one time to the API?, or will call API for every EditText2.value /EditText3.value ...?
when you call the method and pass the parameter you will call the API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.