Take the 2-minute tour ×

I am parsing an array of values with two properties (Key and Value).

Based on the keyword Key certain functions are called which test the Value.

The trouble is, I am tasked with modifying the keywords for some projects for a new project. They share like 90% of the keywords but some are unique to each project and some others are common but other functions need to be called.

At the moment the code looks like:

Public Structure Options
    Public Property Key
    Public Property Value
End Structure

Public Sub CheckPresentation(OptionsList as List(of Options))
    for each Elem in Optionslist
        select case elem.key
            case 1
                if elem.Value<>"bla" then
                end if
            case 2
            case 99
       end select
End Sub

I first wanted to simply build a new class and inherit the old class. But since all of the logic is in the select cases, that would require complete rebuilding with huge code overlap. Does anybody have an idea how to build this better?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You should create a method for each of your keywords.

e.g. for your case 1, create the following method:

Sub LogIfNotBla(elem As Options)
    If elem.Value<>"bla" Then
    End If
End Sub

Do this for every keyword.

Then, create a mapping which maps each keyword to its handler using a dictionary:

Dim handler = new Dictionary(Of Int32, Action(Of Options)) From 
    {1,  AddressOf LogIfNotBla},
    {2,  AddressOf Foo},
    {99, AddressOf FooBar}

and instead of your gigantic Select Case, simply use a lookup to call the right method:

Public Sub CheckPresentation(OptionsList as List(of Options))
    For Each elem in Optionslist
End Sub

Now you can alter the behaviour in two ways:


Mark the methods overridable and overwrite them in a subclass:

Overrides Sub LogIfNotBla(elem As Options)
    If elem.Value<>"bla" And SomeThingElse Then
    End If
End Sub


Alter the handler dictionary to call other methods on specific keywords:

handler(1) = AddressOf AnotherMethodInsteadOfLogIfNotBla
share|improve this answer
Thanks that looks very promising! I will try it and If it works as desired accept your answer. –  Christian Sauer Oct 5 '12 at 9:40
add comment

You can always call the base class' method for default behavior. For instance, lets say you only want to override the behavior for keys 1, 4, and 72, you could do this in your derived class:

Public Overrides Sub CheckPresentation(OptionsList as List(of Options))
    For Each Elem In Optionslist
        Select Case elem.key
            Case 1
            Case 4
            Case 72
            Case Else
        End Select
End Sub
share|improve this answer
Thx, that would work, too - but is difficult for cases where the code inside the case has to be modified. I think the Solution from Mr. Steak works better here, so I will go withhis solution. Sorry guys ;) –  Christian Sauer Oct 5 '12 at 15:11
add comment

Your Answer


