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

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

Juan Carlos Oropeza
1#
Juan Carlos Oropeza Published in 2018-01-12 22:00:53Z

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?

Scott Hannen
2#
Scott Hannen Reply to 2018-01-12 22:56:21Z

To directly answer the title question, a class can have a method with a parameter if its own type, and that's normal.

public class Values
{
    public int X { get; set; }
    public int Y { get; set; }

    public void Add(Values other)
    {
        X += other.X;
        Y += other.Y;
    }
}

Methods don't get serialized, so the class would be serialized the same with or without that method.


I definitely wouldn't put a PositionSaveWeb method in your Position class. The purpose of your Position class is to represent a position. There are a million things you can do with that class - serialize it, post it in an HttpRequest, send it in an email, etc. But those things aren't the responsibility of that class.

It's called the Single Responsibility Principle. A class has one purpose, and only one reason to change. So if you determine that a Position has three points, you might change that class. But you wouldn't want to change that class because now you want to send it via some different method.

So in this case I'd recommend that PositionSaveWeb should be its own class.

dasblinkenlight
3#
dasblinkenlight Reply to 2018-01-12 22:06:05Z

You can absolutely add the Save method to your class. However, since you are not using Position object itself, Save method should be static:

public static void Save (Position sp) {
    ...
}

that feel weird using the thing you are creating to create something

Using static solves this problem

the class also include a method, would this method also be serialized.

In C# methods are not objects (although there are objects that describe methods, namely, MethodInfo) Hence, methods don't get serialized.

SinistraD
4#
SinistraD Reply to 2018-01-12 22:35:28Z

JSON doesn't contain methods, so any method you add to Position will not be included.

You can use this in C# as well, like in JavaScript, in this case. While they mean a slightly different thing, you can use it to reference the current object.

class Test {
    public void test() {
        Console.WriteLine(this);
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Test test = new Test();
        test.test();
    }
}

Will output Test (Fiddle)

Note

That said I still don't recommend adding the Save method to the Position itself, because you are most probably adding too many responsibilities, violating the Single Responsibility Principle

If you leave the Position parameter on a method of Position, no one would be able to extend the class properly, breaking the Open/closed principle as well.

If you make it static like @dasblinkenlight said, you are also breaking the Dependency inversion principle

JavaScript is a functional language, but C# is object oriented. While you can implement/force one on another (e.g. TypeScript), you might face other problems on the long run. You can always introduce new classes to break apart code that feels weird, which I would recommend on this case.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO