Question how to reference a form field and its properties using dynamically named field

talind

New member
Joined
Sep 20, 2019
Messages
1
Programming Experience
10+
I am used to using vb.net and trying to convert a vb.net application to C# and not having the greatest of luck when I am trying to reference fields that are named per hour sun10, sun11,sun12 etc... i want to loop through and set eachs propertys but can't figure out how i can achieve this.

here is what i have been trying to use.

C#:
while (tempcnt < hourblocks)
{
    lasthr = (currhr + hrcntadd);

    string suntemp = "sun" + (tempcnt + 10);
    string montemp = "mon" + (tempcnt + 10);
    string tuetemp = "tue" + (tempcnt + 10);
    string wedtemp = "wed" + (tempcnt + 10);
    string thutemp = "thu" + (tempcnt + 10);
    string fritemp = "fri" + (tempcnt + 10);
    string sattemp = "sat" + (tempcnt + 10);

    if (sunhours[tempcnt] == 0)
    {
        suntemp.visible = false;
    }
    else
    {
        switch (Math.Round((sunhours[tempcnt] / weekcnt) / passhblock, 0))
        {
            case when < 1:
                "sun" + tempcnt + 10.visible = true;
                suntemp.text = Math.Round(sunhours[tempcnt] / weekcnt, 0);
                suntemp.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                break;
            case 1:
                suntemp.visible = true;
                suntemp.text = Math.Round(sunhours[tempcnt] / weekcnt, 0);
                suntemp.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFCC");
 
Last edited by a moderator:
Can we assume that you are using WinForms when you said that "I am trying to reference fields that are named per hour sun10, sun11,sun12 etc.. "?

If so, you can iterate over the Controls collection of your Form to find controls whose Name property matches your desired name. That also assumes that you've let the WinForms designer automatically set the Name property of each control when you set the name of the control, and then kept things in sync. The other alternative is to use reflection to find member variables with your desired name.

Regardless, both approaches will be relatively slow. If you want more speed, put references for your controls into a Dictionary where the key is the "name" of the control, and the value is a reference to the control.

As an aside, it seems interesting that another user, Makandriaco, is also converting their VB.NET program into C# at the same time you are.
 
For future reference, please don't trim the leading whitespace from the first line of your code and then leave it on every other line. Everything you do that makes it harder to read your code is making it less likely that you will get the help you want or as quickly as you could. Please take the small amount to time required to post code that is as readable as possible, which includes formatting it as code and making sure that your indenting is consistent. If you have to trim whitespace manually then do so, but that is unlikely. The code editor on this site will trim the whitespace from an entire selected block using Shift+Tab and you can select an arbitrary block to copy in VS by depressing the Alt key, so there's no need to select leading whitespace in the first place.
 
Off topic.

The code editor on this site will trim the whitespace from an entire selected block using Shift+Tab
I can't seem to get this to work. Everytime I press Shift-Tab, the focus is just placed on my avatar. Do I need to be using a particular browser? Do I need to be using a Mac? I've tried using Opera and Chrome on Win10 with the same result.
 
Back on topic:

I am used to using vb.net and trying to convert a vb.net application to C# and not having the greatest of luck when I am trying to reference fields that are named per hour sun10, sun11,sun12 etc...
Can you show us the equivalent VB.NET code that you are trying to convert to C#?
 
Off topic.


I can't seem to get this to work. Everytime I press Shift-Tab, the focus is just placed on my avatar. Do I need to be using a particular browser? Do I need to be using a Mac? I've tried using Opera and Chrome on Win10 with the same result.
I use the Dev version of Chromium-based Edge, so it should probably work the same in Chrome at least. If I use the editor toolbar to open the Insert Code dialogue, paste a block of with leading whitespace, select the code and then press Shift+Tab, the whitespace is trimmed such that the least-indented line(s) is left-aligned and all other indenting is maintained.
 
Back
Top Bottom