0

Here's the thing: I want to get the string named "IPAddress" from FullParse.cs to Form1.cs. What would it be if I'll get multiple variables aside from "IPAddress"?

Here's the code I've made

FullParse.cs

using RestSharp;


namespace WindowsFormsApp2
{
    internal class FullParse
    {
     
        public void getIP() { 
        RestClient client = new RestClient("http://ip-api.com/json/?fields=9009");
        var request = new RestRequest(Method.GET);
        IRestResponse response = client.Execute(request);
        string source = (response.Content);
        dynamic data = JObject.Parse(source);
         string IPAddress = data.query;

        }
    }
}

Form1.cs

using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FullParse fullParse = new FullParse();
            
        }
    }
}

It's still my first time using C# especially OOP so I don't really know if i'm doing this right.

aiof
  • 11
  • 5
  • change `public void getIP()` to `public string getIp()` so that the method returns a string. At the end of the method add `return IpAddress;` Then after `FullParse fullParse = new FullParse();` add `var ipAddress = fullParse.getIp();` – Scott Hannen Jan 13 '22 at 19:18
  • 1
    Thank you! It worked! I guess I need to learn more about C# objects – aiof Jan 13 '22 at 19:45
  • Last question: What would it be if I'll get multiple variables on getIP(); ? – aiof Jan 13 '22 at 19:57
  • There are numerous ways to do this. This answer covers them. https://stackoverflow.com/a/30632338/5101046. But for practical purposes I would ignore everything except for returning a class or struct. The other options might be useful later, but by far the most common solution is to return an object which contains the values you want. – Scott Hannen Jan 13 '22 at 20:15

1 Answers1

0

Example of one way you can do this with multiple out parameters:

Changes to FullParse.cs:

using RestSharp;


namespace WindowsFormsApp2
{
    internal class FullParse
    {
     
        public string getIP(out int firstOutInt, out string secondOutString) { 
        RestClient client = new RestClient("http://ip-api.com/json/?fields=9009");
        var request = new RestRequest(Method.GET);
        IRestResponse response = client.Execute(request);
        string source = (response.Content);
        dynamic data = JObject.Parse(source);
        string IPAddress = data.query;
        int firstOutInt = 1234;
        string secondOutString = "some text";
        return IPAddress;

        }
    }
}

Changes to Form1.cs:

using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FullParse fullParse = new FullParse();
            string ipAddress = fullParse.getIP(out int firstOutInt, out string secondOutString);

            //doSomething is not defined here - this is just an example of a function using the values from getIP
            doSomething(ipAddress, firstOutInt, secondOutString);
        }
    }
}
Deep in the Code
  • 572
  • 1
  • 6
  • 20