0

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.

1
  • 7
    Ideally you would significantly refactor your code, but in this specific instance of code I would suggest you create a string array with length 18, fill it with your values during the loop, and then use that to supply the parameters to your function call Commented Oct 24, 2017 at 8:59

3 Answers 3

1

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; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I dont get this code, what code i should place in "WaardeObjecten"? and what code in my main class?
Instead of keeping two arrays with matching indices, create an object that holds these two values (this is "WaardePaar"). In your main class you then simply store an array or List (here _values) that you can easily access with the indexer function. So if you implement the indexer in WaardeObjecten then you can basically call myWaardeObjectenInstance[4] to get the pair{ Naam, Waarde }
1

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);

5 Comments

Where do u get [i] from?
@Niels: Sorry, it was meant to be count. I was in "for loop auto-pilot mode"... fixed
i see, let me try.
How would you setup the waardeObjecten class?
@Niels: Sorry, it would be too much to go into detail in either a comment or an answer. Also, I don't have your class code anyway to be able to give an accurate review. But it seems you would be better have a list rather that 9 different sets of properties. Try to keep in mind, if you are naming something with a number, you are probably doing it wrong (though there are rare exceptions)
0

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);

8 Comments

Getting the error at [i]: "bad array declarator to declare a managed array the rank specifier precedes the variable's identifier" in this code.
I just posted a rough code here to give you an idea as how to do it. Is your code in c++?
Check the code now, the string arrays have to be initialized outside the loop.
It's in C#, there are probally better ways to get to the same point but I'm still learning.
@MubashiraZaman: First of all, the question is tagged with C# so you shouldn't need to ask (why you have mentioned Java at all is beyond me). Secondly, you should really test your code to make sure it work if you are not comfortable enough to post it from memory. Not only is your declaration syntax wrong, but you still haven't declared them outside the loop as you claim... you know the loop is the foreach part right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.