Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48177

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

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:

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

Upvotes: 1

Views: 78

Answers (3)

Scott Hannen
Scott Hannen

Reputation: 29207

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.

Upvotes: 1

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.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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.

Upvotes: 1

Related Questions