Juw
Juw

Reputation: 2089

Sending multiple parameters to a method in C#?

Sending multiple parameters to a method?

So, i am pretty new to C# and Visual Studio and i am "learning by doing" and asking questions. I am writing a small Windows Form application.

I am trying to send another form values.

I have a list of objects from this class:

class Cars
{
    public string Name { get; private set; }
    public string Color { get; private set; }

    public Cars(string name, string color)
    {
        this.Name = name;
        this.Color = color;
    }
 }

So in my Form1 i have access to these objects by using:

List<Cars> cars = new List<Cars>();

This list of Cars is loaded in other methods.

So now, i am trying to send another form (Edit form) a car. I would like to do this:

var form2 = new frmEdit(cars[0]);

But then compiler complains about that i need to set my class to public...bad OOP. So then i could do it like this:

var form2 = new frmEdit(cars[0].Name,cars[0].Color);

Fine! But if this was another language like Javascript or PHP i would have sent an object. So i have read about "Anonymus Types" in C# so i thought that could be a good solution.

But the receiveing form doesn´t know about that...so it will complain if i use it like this:

car.Name;

So what should i do here? I am trying to use at least "some" good OOP so i think it is a bad solution making the Cars class public. The Edit form does not need to know about the Cars class.

Thank you for any advice!

[EDIT] Edit form constructor:

public frmEdit(string name, string color)
{
textName.Text = name;
textColor.Text = color;
}

Upvotes: 0

Views: 147

Answers (1)

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 581

Change your frmEdit to:

private Cars myCar; // add this var.

public frmEdit(Cars car)
{
     this.myCar = car; // now you have your car stored if you need
     textName.Text = car.Name;
     textColor.Text = car.Color;
}

And Then:

var form2 = new frmEdit(cars[0]);

Upvotes: 1

Related Questions