can i use a variable’s value as the name of an object in C#

I have several Dropdown lists that need to have the same data inserted into them, and I was wondering if I could make a function to fill in all this data and pass it a variable to use as the name of the object.

public void populateDDL(double MaxNumber, string DDLName)
{
    double currentNumber = 0
    while (currentNumber < MaxNumber)
    {
        thickness = currentNumber + .5;
        ListItem Gen1Item = new ListItem();
        Gen1Item.Text = currentNumber.ToString();
        Gen1Item.Value = currentNumber.ToString();
        DDLName.Items.Add(Gen1Item);
    }
}

The function would accept a max number that it would fill the DDL to in increments of .5, then use the string passed to it to indicate which DropDownList it should be filling.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could pass in a reference to the actual DropDownList instead of passing in a string:

public void populateDDL(double MaxNumber, DropDownList dll)
{
    double currentNumber = 0
    while (currentNumber < MaxNumber)
    {
        thickness = currentNumber + .5;
        ListItem Gen1Item = new ListItem();
        Gen1Item.Text = currentNumber.ToString();
        Gen1Item.Value = currentNumber.ToString();
        ddl.Items.Add(Gen1Item);
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x