Home Can a method in the class receive the same class as parameter?
Reply: 0

Can a method in the class receive the same class as parameter?

user1872
1#
user1872 Published in July 20, 2018, 4:47 am

I have a class

public class Position
{
    public double X_long { get; set; }
    public double Y_lat { get; set; }
}

And I have a separated function to send the Position to my webservice.

private void PositionSaveWeb(Position sp)
{
    var httpWebRequest = (HttpWebRequest)WebRequest
                           .Create("http://myserver.com/ApiTest/api/Position");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 
            | SecurityProtocolType.Tls11 
            | SecurityProtocolType.Tls;

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = JsonConvert.SerializeObject(sp);
        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();
    }

    string result = TryWebRequest(httpWebRequest);
}

I wonder can/should I add that method to the class or is ok like is now:

public class Position
{
    public double X_long { get; set; }
    public double Y_lat { get; set; }

    public void Save (Position sp) {
    }
}

The problems I have here are:

  • In javascript when you declare a class you have this. So I could call JsonConvert.SerializeObject(this); but here don't know how I can serialize the object. Unless I use the public variables to create a new Position Object inside the method. But that feel weird using the thing you are creating to create something. I don't want create an infinitive recursive loop or something.
  • Now the class also include a method, would this method also be serialized.

So should Save be a method of the Class, and if so how I add it?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.319802 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO