Results 1 to 7 of 7

Thread: Shortening Code

  1. #1

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Shortening Code

    Hey guys, this isn't a real bug, but its been bothering me.

    I need a way to shorten this case method:

    vb Code:
    1. Select Case bgcolorVar
    2.                     Case "red"
    3.                         MyScripts.BackgroundImage = My.Resources.red
    4.                         offlineAdd.BackgroundImage = My.Resources.red
    5.                         Info.BackgroundImage = My.Resources.red
    6.                         main.BackgroundImage = My.Resources.red
    7.                         AboutBox.BackgroundImage = My.Resources.red
    8.                         Add.BackgroundImage = My.Resources.red
    9.                         bug_report.BackgroundImage = My.Resources.red
    10.                         BugWindow.BackgroundImage = My.Resources.red
    11.                         Credits.BackgroundImage = My.Resources.red
    12.                         settings.BackgroundImage = My.Resources.red
    13.                         Splash.BackgroundImage = My.Resources.red
    14.                         Updates.BackgroundImage = My.Resources.red
    15.                     Case "orange"
    16.                         MyScripts.BackgroundImage = My.Resources.orange
    17.                         offlineAdd.BackgroundImage = My.Resources.orange
    18.                         Info.BackgroundImage = My.Resources.orange
    19.                         main.BackgroundImage = My.Resources.orange
    20.                         AboutBox.BackgroundImage = My.Resources.orange
    21.                         Add.BackgroundImage = My.Resources.orange
    22.                         bug_report.BackgroundImage = My.Resources.orange
    23.                         BugWindow.BackgroundImage = My.Resources.orange
    24.                         Credits.BackgroundImage = My.Resources.orange
    25.                         settings.BackgroundImage = My.Resources.orange
    26.                         Splash.BackgroundImage = My.Resources.orange
    27.                         Updates.BackgroundImage = My.Resources.orange
    28.                     Case "green"
    29.                         MyScripts.BackgroundImage = My.Resources.green
    30.                         offlineAdd.BackgroundImage = My.Resources.green
    31.                         Info.BackgroundImage = My.Resources.green
    32.                         main.BackgroundImage = My.Resources.green
    33.                         AboutBox.BackgroundImage = My.Resources.green
    34.                         Add.BackgroundImage = My.Resources.green
    35.                         bug_report.BackgroundImage = My.Resources.green
    36.                         BugWindow.BackgroundImage = My.Resources.green
    37.                         Credits.BackgroundImage = My.Resources.green
    38.                         settings.BackgroundImage = My.Resources.green
    39.                         Splash.BackgroundImage = My.Resources.green
    40.                         Updates.BackgroundImage = My.Resources.green
    41.                     Case "blue"
    42.                         MyScripts.BackgroundImage = My.Resources.blue
    43.                         offlineAdd.BackgroundImage = My.Resources.blue
    44.                         Info.BackgroundImage = My.Resources.blue
    45.                         main.BackgroundImage = My.Resources.blue
    46.                         AboutBox.BackgroundImage = My.Resources.blue
    47.                         Add.BackgroundImage = My.Resources.blue
    48.                         bug_report.BackgroundImage = My.Resources.blue
    49.                         BugWindow.BackgroundImage = My.Resources.blue
    50.                         Credits.BackgroundImage = My.Resources.blue
    51.                         settings.BackgroundImage = My.Resources.blue
    52.                         Splash.BackgroundImage = My.Resources.blue
    53.                         Updates.BackgroundImage = My.Resources.blue
    54.                     Case "violet"
    55.                         MyScripts.BackgroundImage = My.Resources.purple
    56.                         offlineAdd.BackgroundImage = My.Resources.purple
    57.                         Info.BackgroundImage = My.Resources.purple
    58.                         main.BackgroundImage = My.Resources.purple
    59.                         AboutBox.BackgroundImage = My.Resources.purple
    60.                         Add.BackgroundImage = My.Resources.purple
    61.                         bug_report.BackgroundImage = My.Resources.purple
    62.                         BugWindow.BackgroundImage = My.Resources.purple
    63.                         Credits.BackgroundImage = My.Resources.purple
    64.                         settings.BackgroundImage = My.Resources.purple
    65.                         Splash.BackgroundImage = My.Resources.purple
    66.                         Updates.BackgroundImage = My.Resources.purple
    67.                     Case "default"
    68.                         MyScripts.BackgroundImage = My.Resources._default
    69.                         offlineAdd.BackgroundImage = My.Resources._default
    70.                         Info.BackgroundImage = My.Resources._default
    71.                         main.BackgroundImage = My.Resources._default
    72.                         AboutBox.BackgroundImage = My.Resources._default
    73.                         Add.BackgroundImage = My.Resources._default
    74.                         bug_report.BackgroundImage = My.Resources._default
    75.                         BugWindow.BackgroundImage = My.Resources._default
    76.                         Credits.BackgroundImage = My.Resources._default
    77.                         settings.BackgroundImage = My.Resources._default
    78.                         Splash.BackgroundImage = My.Resources._default
    79.                         Updates.BackgroundImage = My.Resources._default
    80.                 End Select

    Any way I can do this?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Shortening Code

    Yeah, you could fill a single variable (of a type that BackgroundImage accepts) in the select case. You'd be filling the variable with either My.Resources.Orange, or My.Resource.green, or My.Resources.red. You could then move all those assignments out to after the select statement and assign the variable to them. That would cut the code length by not quite two thirds.
    My usual boring signature: Nothing

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Shortening Code

    Code:
        Dim Ctrls() As Control = {MyScripts, offlineAdd, Info, main, AboutBox, Add, bug_report, BugWindow, Credits, settings, Splash, Updates} 
        Dim Resources() As System.Drawing.Image = {My.Resources.red, My.Resources.orange, My.Resources.green, My.Resources.blue, My.Resources.violet, My.Resources._default} 
        Public Sub ShortenCode() 
            Dim Index As Integer = 0 
            Select Case bgcolorVar 
                Case "red" 
                    Index = 0 
                Case "orange" 
                    Index = 1 
                Case "green" 
                    Index = 2 
                Case "blue" 
                    Index = 3 
                Case "violet" 
                    Index = 4 
                Case "default" 
                    Index = 5 
            End Select 
            For Each ctrl As Control In Ctrls 
                ctrl.BackgroundImage = Resources(Index) 
            Next 
        End Sub
    22/80 = 28% of the size

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Shortening Code

    I think the key to shortening code is to balance compact code with readability.

    If I were judging a code beauty contest I'd suggest taking parts from each of Shaggy_Hiker and ForumAccount's examples...

    I like the idea of ForumAccount's control array rather than referencing the controls individually, but I'd opt for Shaggy_Hiker's idea of a variable to hold the resource (on the basis that the resulting code is slightly more readable and doesn't rely on arbitrary "magic numbers" to index an array of resources)

  5. #5
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: Shortening Code

    hey formlesstree4

    this is wht Shaggy Hiker is advising

    vb Code:
    1. Dim oImage As Image
    2.  
    3.         Select Case bgcolorVar
    4.             Case "red"
    5.                 oImage = My.Resources.red
    6.             Case "orange"
    7.                 oImage = My.Resources.orange
    8.             Case "green"
    9.                 oImage = My.Resources.green
    10.             Case "blue"
    11.                 oImage = My.Resources.blue
    12.             Case "violet"
    13.                 oImage = My.Resources.violet
    14.             Case "default"
    15.                 oImage = My.Resources.My.Resources._default
    16.         End Select
    17.  
    18.         MyScripts.BackgroundImage = oImage
    19.         offlineAdd.BackgroundImage = oImage
    20.         Info.BackgroundImage = oImage
    21.         main.BackgroundImage = oImage
    22.         AboutBox.BackgroundImage = oImage
    23.         Add.BackgroundImage = oImage
    24.         bug_report.BackgroundImage = oImage
    25.         BugWindow.BackgroundImage = oImage
    26.         Credits.BackgroundImage = oImage
    27.         settings.BackgroundImage = oImage
    28.         Splash.BackgroundImage = oImage
    29.         Updates.BackgroundImage = oImage

    or optionally all controls can be stored in array as forumaccount suggest will be an another step for Shortening Code :-)
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Shortening Code

    Su ki, thanks for writing out what I didn't bother with.

    There is a VERY slight advantage to what I suggested in that it is effectively an unrolled loop. There once was a time when that mattered, but certainly not now. What ForumAccount suggested is much more compact, and as easy to extend, in my opinion.
    My usual boring signature: Nothing

  7. #7
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Shortening Code

    You don't need a case statement. My.Resources supports dictionary type search:

    Code:
    Dim Ctrls() As Control = {MyScripts, offlineAdd, Info, main, AboutBox, Add, bug_report, BugWindow, Credits, settings, Splash, Updates} 
    For Each ctrl As Control In Ctrls
        ctrl.BackgroundImage = CType(My.Resources(bgcolorVar), Image)
    Next

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