Niels
Niels

Reputation: 95

How to store for each loop values in different variables

Case:

I'm trying to create a object which expects 18 parameters, these parameters i want to obtain from a for each loop. How do i do this?

Code of class which expects 18 parameters:

public WaardeObjecten(string waardeNaam1, string waarde1, string waardeNaam2, string waarde2, string waardeNaam3, string waarde3, string waardeNaam4, string waarde4, string waardeNaam5, string waarde5, string waardeNaam6, string waarde6, string waardeNaam7, string waarde7, string waardeNaam8, string waarde8, string waardeNaam9, string waarde9)
{
    this.waardeNaam1 = waardeNaam1;
    this.waarde1 = waarde1;

    this.waardeNaam2 = waardeNaam2;
    this.waarde2 = waarde2;

    this.waardeNaam3 = waardeNaam3;
    this.waarde3 = waarde3;

    this.waardeNaam4 = waardeNaam4;
    this.waarde4 = waarde4;

    this.waardeNaam5 = waardeNaam5;
    this.waarde5 = waarde5;

    this.waardeNaam6 = waardeNaam6;
    this.waarde6 = waarde6;

    this.waardeNaam7 = waardeNaam7;
    this.waarde7 = waarde7;

    this.waardeNaam8 = waardeNaam8;
    this.waarde8 = waarde8;

    this.waardeNaam9 = waardeNaam9;
    this.waarde9 = waarde9;
}

Code i got so far to create a object and fill it:

foreach (Panel p in panels)
{
    //ALWAYS 9 * (2 values) panels.
    var selectedRadioButton = p.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
    if (selectedRadioButton != null)
    {
        totalStringForRadioButtons += $"{selectedRadioButton.Name} : {selectedRadioButton.Text} | ";
    }
}

WaardeObjecten obj = new WaardeObjecten(**Expects 18 parameters here**);

For each for each result(9) i want to obtain : selectedRadioButton.Name and selectedRadioButton.Text(2) and put this 18 values in the object.

Upvotes: 0

Views: 3437

Answers (3)

KulaDamian
KulaDamian

Reputation: 154

A better alternative would be to store the data in the array and send it to the WaardeObjecten constructor. Like this:

public WaardeObjecten(string[] waardeNaam, string[] waarde)
{ //do the assignment here}

whereas in the foreach loop, you can add data in the string array:

int i = 0;
string waardenaam[9];
string waarde[9];
foreach (Panel p in panels)
{
     var selectedRadioButton = p.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);

     if (selectedRadioButton != null)
     {
           waardenaam[i] = selectedRadioButton.Name;
           waarde[i] = selectedRadioButton.Text;
     }
     i++;
}

WaardeObjecten obj = new WaardeObjecten(waardenaam, waarde);

Upvotes: 0

musefan
musefan

Reputation: 48415

Overall it looks like you have problems with your design and should probably consider refactoring your code. However, that would be a bit too much to go into in an answer so I will stick with directly providing a solution to your current code.

I would suggest you use a string array to store your values, which you populate in your loop and then use that to supply the parameters to your function.

Declare and use the string array like so:

string[] values = new string[18];

int count = 0;
foreach (Panel p in panels)
{
    //ALWAYS 9 * (2 values) panels.
    var selectedRadioButton = p.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
    if (selectedRadioButton != null)
    {
        values[count] = selectedRadioButton.Name;
        values[count+1] = selectedRadioButton.Text;
    }

    count+=2;
}

Now, you can call your function like this:

WaardeObjecten obj = new WaardeObjecten(values[0], values[1], values[2] /*etc.*/);

Alternatively, you could change your function to this:

public WaardeObjecten(params string[] values)
{
    this.waardeNaam1 = values[0];
    this.waarde1 = values[1];

    // etc.
}

And then just simplify the call:

WaardeObjecten obj = new WaardeObjecten(values);

Upvotes: 1

Geoffrey
Geoffrey

Reputation: 976

As pointed out, you should refactor your code. Not only would this fix your problem, it will also make it more flexible in the future.

public class WaardePaar
{
    public string Naam { get; set; }
    public string Waarde { get; set; }
}

public WaardeObjecten(IEnumerable<WaardePaar> paren)
{
    // store in a private field list or array
    _values = paren.ToList();
}

Now you can just iterate over them or use an indexer to get specific values.

public WaardePaar this[int index]
{
    get { return _values[index]; }
    set { _values[index] = value; }
}

Upvotes: 1

Related Questions