VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Alternative to nested if...then...else statement Posted by runey71 on 15 Dec 2008 at 8:29 PM
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...

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


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

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


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
Report
Re: Alternative to nested if...then...else statement Posted by seancampbell on 16 Dec 2008 at 11:06 AM
Mmm... Ever use ElseIf? It works very similar to a Select Case.
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


Hope this helps,
Happy coding!
-firesickle.com
Report
Re: Alternative to nested if...then...else statement Posted by runey71 on 16 Dec 2008 at 2:49 PM
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

Report
Re: Alternative to nested if...then...else statement Posted by seancampbell on 16 Dec 2008 at 4:54 PM
: 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
Report
Re: Alternative to nested if...then...else statement Posted by runey71 on 16 Dec 2008 at 5:16 PM
: 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...

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


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...
Report
Re: Alternative to nested if...then...else statement Posted by seancampbell on 17 Dec 2008 at 10:11 AM
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):

Dim Arr as System.Collections.ArrayList

Do While Not EndOfFile
Str = "readLine"
Arr.Add(Str)
Loop


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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.