Maxime de Lange
Maxime de Lange

Reputation: 297

C# constructors with parameters - passing on

I got a question about constructors for my Windows Forms Application. First of all I want to say that I am new to programming.

The thing is this. I am making a constructor in another class that should hold different parameter values. In this case it should be int X, int Y, int Length, int Height. What I want to do here is to make the X, Y, Length and Height all random for my picturebox. I send code down below:

class Rechthoekcs
{
    Random random = new Random();

    public int Xas
    {
        get;
        set;
    }
    public int Yas
    {
        get;
        set;
    }
    public int Lengte
    {
        get;
        set;
    }
    public int Breedte
    {
        get;
        set;
    }
    public Rechthoekcs(int x, int y, int lengte, int breedte)
    {
        this.Xas = x;
        this.Yas = y;
        this.Lengte = lengte;
        this.Breedte = breedte;
        x = random.Next(x);
        y = random.Next(y);
        lengte = random.Next(lengte);
        breedte = random.Next(breedte);
    }

From my Form1 I want to call this class/constructor

But it gives me an error. It says "Does not contain a constructor with 0 arguments" and I know that because I typed the x, y, length and width. But I cannot add just the variables from the other class into the new parameter. I really don't get it. I find constructors very hard. I never know what parameters I should give with it...

I send the code down below from my Form1:

public partial class Form1 : Form
{
    Rechthoekcs Rechthoek = new Rechthoekcs(.......);
    public Form1()
    {
        InitializeComponent();
    }

It it really frustrating for me. I tried looking up on the web and books and such but all the explanation about which parameters should be given for a constructor is not clear to me. Could someone explain this? Not understanding it drives me insane. Plus I am getting often stuck at these points..

Upvotes: 0

Views: 139

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

Not the answer OP is looking for, but this is how you solve "I don't yet know the value but I need to construct object anyway" version of the question.

You can delay requesting the data by passing Func<T> for each parameter instead of just T assuming the values actually will be available by the time they needed:

class UseDelayedValues
{
    Func<int> x;
    public UseDelayedValues(Func<int> x)
    {
       this.x = x;
    }

    public UseWithX(int other)
    {
       return other + x();
    }
}

int value = 0;
var r = new UseDelayedValues(() => value);
value = 42;// get some value 
Console.WriteLine(r.UseDelayedValues(1));

var delayedFromTextbox = new UseDelayedValues(() => int.Parse(textBox1.Value));

Lines using the UseDelayedValues can be spread over time. I.e. instance constructed in constructor, but value used only when form is shown by button click.

Upvotes: 0

Adam
Adam

Reputation: 2440

public Rechthoekcs(int x, int y, int lengte, int breedte)
{
    this.Xas = x;
    this.Yas = y;
    this.Lengte = lengte;
    this.Breedte = breedte;
    x = random.Next(x);
    y = random.Next(y);
    lengte = random.Next(lengte);
    breedte = random.Next(breedte);
}

You are assigning the values of the parameters to your private Data Members before you do anything "randomizing" about them. You are simply changing the values of the parameters in your constructor without assigning them. Swap the order you do them in.

public Rechthoekcs(int x, int y, int lengte, int breedte)
{

    x = random.Next(x);
    y = random.Next(y);
    lengte = random.Next(lengte);
    breedte = random.Next(breedte);

    this.Xas = x;
    this.Yas = y;
    this.Lengte = lengte;
    this.Breedte = breedte;
}

Now you have successfully randomized your values that will be set to your data member variables ASSUMING that those variable you put in there actually exist, which they should. Better/more modular code would do this randomization where you create your object.

Ex: Object foo = new Object(new Random, new Random, new Random, new Random)

Upvotes: 1

Related Questions