Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Alternative to nested if...then...else statement

runey71runey71 Posts: 5Member
I'll try and give a brief description of what I am trying to do... Basically I have a string that extracted from a text file called strCombined... I want to look at that string and if it contains a certain phrase then I want to act on it... If not then perform a default action...

Now I can do this pretty easily using a nested if...then...else statement... That's long winded and messy though...

[code]If strCombined.IndexOf("Cambridge University") Then
strSearchName = "Cambridge University"
Else
If strCombined.IndexOf("FM Towns") Then
strSearchName = "FM Towns"
Else
If strCombined.IndexOf("Jupiter Cantab") Then
strSearchName = "Jupiter Cantab"
Else
strSearchName = ""
End If
End If
End If[/code]

I was hoping to use some kind of select...case statement instead...

[code]Select Case strCombined
Case strCombined.IndexOf("Cambridge University")
strSearchName = "Cambridge Univeristy"
Case strCombined.IndexOf("FM Towns")
strSearchName = "FM Towns"
Case strCombined.IndexOf("Jupiter Cantab")
strSearchName = "Jupiter Cantab"
Case Else
strSearchName = ""
End Select[/code]

This obviously is not working... I'm hoping someone here can help me to either get this working with a select...case statement or suggest some other alternative...

Thanks in advance

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    Mmm... Ever use ElseIf? It works very similar to a Select Case.
    [code]
    If strCombined.IndexOf("Cambridge University") Then
    strSearchName = "Cambridge University"
    ElseIf strCombined.IndexOf("FM Towns") Then
    strSearchName = "FM Towns"
    ElseIf strCombined.IndexOf("Jupiter Cantab") Then
    strSearchName = "Jupiter Cantab"
    Else
    strSearchName = ""
    End If
    [/code]

    Hope this helps,
    Happy coding!
    -firesickle.com
  • runey71runey71 Posts: 5Member
    No I've never seen an ElseIf before... I'm not exactly a programmer :) I just tend to throw together little applications as and when I need something...

    It does look incredibly like a SelectCase statement... I'll try it out shortly but it looks like it will work perfectly...

    Thank you

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    : No I've never seen an ElseIf before... I'm not exactly a programmer
    : :) I just tend to throw together little applications as and when I
    : need something...
    :
    : It does look incredibly like a SelectCase statement... I'll try it
    : out shortly but it looks like it will work perfectly...
    :
    : Thank you
    :
    :
    You already write better code then a lot of programmers I know... haha
    I took a second look at your original post, It looked like you have a huge string of combined values (I am making an assumption based on your variable name).
    How are you handling the case where two of those If Statements would be true. In a Select Case or the If Statements you and I both wrote, the first case that is met will be the only case that is executed...
    I figure you've already thought of this, but incase you haven't...

    happy coding :)
    firesickle.com
  • runey71runey71 Posts: 5Member
    : You already write better code then a lot of programmers I know...
    : haha
    : I took a second look at your original post, It looked like you have
    : a huge string of combined values (I am making an assumption based on
    : your variable name).
    : How are you handling the case where two of those If Statements would
    : be true. In a Select Case or the If Statements you and I both wrote,
    : the first case that is met will be the only case that is executed...
    : I figure you've already thought of this, but incase you haven't...
    :
    : happy coding :)
    : firesickle.com

    Well thanks for the compliment :)

    Basically what this program does, at least this bit anyway, is read in one Datfile at a time... It's really only interested in the header... A process I still need to improve to speed up things... It then looks to see if that Datfile header contains one of 11 possible strings (I only showed a couple in my example)... If so then take appropriate action... I didn't originally want to do it this way but unfortunately the Datfile does not use a deliminator between the Manufacturer and System... They are however pretty much constant and should rarely change...

    Anyway long story short there is no way for more one case statement, or if statement, to be true for any given Datfile...

    Actually just had a thought to help make the program a bit more future proof... Remove those possible strings into a separate text file... Read it in line by line and create an array... say aryManufacturer... Then use a for...next loop to do the check... That way if the Datfiles change in the future, ie add or remove manufacterers, then only a text file change is required to make the program compatible again...

    [code]For Each v As String In aryManufacturer
    If strCombined.IndexOf(v) <> -1 Then
    strSearchName = aryManufacterer(v)
    Exit For
    End If
    Next[/code]

    Sorry just thinking out loud :)

    I actually have the program well enough for my own purposes... However it would be nice to make it flexible and robust enough to release to those who might need it...
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    You could bring them into a classic Array, and try and judge how many you will have coming in somehow (by putting an integer value in the top line of you flatfile is my favorite method), or you could try using an ArrayList object (which is a nifty little variable size array object in System.Collections)

    So I would read in the list of cases (this is psuedo code, just writing out the concept, if you want to do this and need help I can help you write the code):

    [code]
    Dim Arr as System.Collections.ArrayList

    Do While Not EndOfFile
    Str = "readLine"
    Arr.Add(Str)
    Loop
    [/code]

    [code]
    For i as integer = 0 to Arr.Count - 1
    If strCombined.indexOf(DirectCast(Arr.Item(i), String)) > -1 Then
    StrSomething = DirectCast(Arr.Item(i), String)
    Exit For
    End If
    Next i
    [/code]
Sign In or Register to comment.