Results 1 to 5 of 5

Thread: Reading Environment Variables & Setting Control Values

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    15

    Post Reading Environment Variables & Setting Control Values

    Hello,

    I'm looking for an opinion on a piece of code I've thrown together from a few examples I found on this site and elsewhere.

    I've got an old program that I inherited at work (not really my job, I have next to zero programming ability but I have just have to do it), for whatever reason the author of this old program chose to keep the various configuration values for it in environment variables and there are a lot of them... 70+. Now I can't imagine this was a good idea even back in 1996 but here we are with no source code for the old software and a need to configure it without quite as much pain moving forward.

    I wanted to avoid copy pasting a lot of code to read each environment variable so this is what I put together:

    vb.net Code:
    1. Dim EV As System.Collections.DictionaryEntry
    2.  
    3.         For Each EV In Environment.GetEnvironmentVariables()
    4.  
    5.             If EV.Key.ToString.StartsWith("LOBAppPrefix_") Then
    6.  
    7.                 Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
    8.  
    9.                 Do Until ctl Is Nothing
    10.                     'Use ctl here.
    11.  
    12.                     If ctl.Name = EV.Key.ToString Then
    13.  
    14.                         If TypeOf ctl Is CheckBox Then
    15.                             CType(ctl, CheckBox).Checked = True
    16.                         ElseIf TypeOf ctl Is ComboBox Then
    17.                             CType(ctl, ComboBox).SelectedValue = EV.Value.ToString
    18.                         End If
    19.  
    20.                     End If
    21.  
    22.                     ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
    23.  
    24.                 Loop
    25.  
    26.             End If
    27.  
    28.         Next

    I name the controls to match the environment variable and it does work how I expected but I would be interested to know if there is a better way of doing this that might improve its performance.

    Thanks to jmcilhinney for a much easier to understand method of looping though all controls and pradeep for GetEnvironmentVariables example.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Reading Environment Variables & Setting Control Values

    Welcome to VBForums

    Your code seems valid, just a bit long-winded and a bit inefficient (as it goes thru each control once for each LOBAppPrefix_ environment variable).

    As you have the control names the same as the environment variables, you can actually get the controls by name. eg:
    vb.net Code:
    1. Dim EV As System.Collections.DictionaryEntry
    2.  
    3.         For Each EV In Environment.GetEnvironmentVariables()
    4.  
    5.             Dim EVKey as String = EV.Key.ToString
    6.             If EVKey.StartsWith("LOBAppPrefix_") Then
    7.  
    8.                 Dim ctl As Control = Me.Controls(EVKey)
    9.                 If TypeOf ctl Is CheckBox Then
    10.                     CType(ctl, CheckBox).Checked = True
    11.                 ElseIf TypeOf ctl Is ComboBox Then
    12.                     CType(ctl, ComboBox).SelectedValue = EV.Value.ToString
    13.                 End If
    14.             End If
    15.  
    16.         Next

    As for the original use of environment variables, it probably started out as just a few (and using environment variables for a few would have been sensible back then), and then expanded over time... at which point it wasn't such a great idea any more, but changing the code could have taken more time than it was worth.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    15

    Re: Reading Environment Variables & Setting Control Values

    Quote Originally Posted by si_the_geek View Post
    Welcome to VBForums
    Your code seems valid, just a bit long-winded and a bit inefficient (as it goes thru each control once for each LOBAppPrefix_ environment variable).
    Thanks

    That was the concern I had but I could not think of/find another way to do it aside from individually getting each environment variable and setting the control, I did briefly test doing that but when I put a timer on it to see what the performance was like it was quite slow.

    Quote Originally Posted by si_the_geek View Post
    As you have the control names the same as the environment variables, you can actually get the controls by name. eg:
    I don't have my old code but I think I had something very close to that at one point but in trying to organize the form a little I have ended up with the controls are spread over 4 tabs. This lead me to finding the code from jmcilhinney, using it meant I did not have to worry about where the control was.

    Thanks again!

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    15

    Re: Reading Environment Variables & Setting Control Values

    I made a small change, it seems to have solved my problem with controls being in tabs. Is this any better than just looping through the controls like I was doing?

    From:
    vb.net Code:
    1. Dim ctl As Control = Me.Controls(EVKey)
    To:
    vb.net Code:
    1. Dim ctl As Control = Me.Controls.Find(EVKey, True)(0)

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Reading Environment Variables & Setting Control Values

    In terms of speed it might be, I'm not sure so you'd need to test. It does make for simpler code tho, which is a nice thing to have.

    An alternative is to invert the loops, so you find the controls, and then get the matching environment variable. eg:
    vb.net Code:
    1. Dim EnvironmentVariables As IDictionary = Environment.GetEnvironmentVariables()
    2.         Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
    3.         Do Until ctl Is Nothing
    4.             If ctl.Name.StartsWith("LOBAppPrefix_") Then
    5.                 Dim EV As System.Collections.DictionaryEntry = EnvironmentVariables.Item(ctl.Name)
    6.                 If EV IsNot Nothing Then
    7.  
    8.                     If TypeOf ctl Is CheckBox Then
    9.                         CType(ctl, CheckBox).Checked = True
    10.                     ElseIf TypeOf ctl Is ComboBox Then
    11.                         CType(ctl, ComboBox).SelectedValue = EV.Value.ToString
    12.                     End If
    13.  
    14.                 End If
    15.                 ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
    16.             End If
    17.         Loop
    This is untested, so may need minor fixes.. but it should be quick, as well as being simpler code (because one of the loops is replaced by a dictionary search).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width