Results 1 to 13 of 13

Thread: Decision making without a huge select case?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    10

    Question Decision making without a huge select case?

    I'm trying to figure out if there was a better way to do the following. I'm writting a program that recieves data from a telnet server. It looks at the data and takes actions based on what the data is. The problem is not with the socket connections . The problem is with how to code the decision making.

    I was thinking of doing the following. I've got a list of all the known "tokens" (strings that will be recieved from the telnet server). I put the tokens into a string array. I've got a function that takes a string of data from the server and compares it to all the known tokens. If it finds a matching token it returns the index of that token in the string array. Then it passes that number to another function that does the appropriate action for the token. The action taking function is just a big select case.

    My question is, is there a better way to do this than with a big select case?

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I would start by looking into regular expressions.
    Whadayamean it doesn't work....
    It works fine on my machine!

  3. #3
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Nevermind, it wouldn't help with your problem. Still looking...
    Whadayamean it doesn't work....
    It works fine on my machine!

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Then it passes that number to another function that does the appropriate action for the token.
    Does that function call other functions based on the value passed in?
    Whadayamean it doesn't work....
    It works fine on my machine!

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    10
    Originally posted by CyberHawke
    Does that function call other functions based on the value passed in?
    It calls functions and sets values. Mostly it sets values.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I have a hazy idea that if you can create a Class which will perform the operation you require and make that class a collection, you would be able to reference that class by relating the collection number to the array index selected.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Hmm I once had the same problem when developing an irc client. I resolved the problem in a different way.

    For each kind of function I wanted to perform I had a class that would be derived from the IrcFunctionClass. That class would have a property that would be the FunctioName and then an array of arguments.

    Each of those classes had a Run(arg) method.

    Then everytime I received something from the irc server my function would loop through a collection with all those classes(that before the connection have been added to the collection) and run the .Run() method.

    You can even put all those classes each one in a different DLL

    I hope I made myself clear
    \m/\m/

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    With acknowledgements to ABX for his response to another thread

    Set up each of the different procedures you need (one each to handle each of the tokens I guess) as methods. Name them, "Method1", "Method2", "Method3" etc.

    When you get the index of the Token hold it in a string variable (as a string of course) say strTest. (Concanting it with "Method")

    Then
    VB Code:
    1. CallByName(Me, strTest, CallType.Method)
    2.  
    3. Sub Method1()
    4.   MessageBox.Show("This is CallByName")
    5. End Sub

    If you need to pass a parameter to the Method then that is optional,

    VB Code:
    1. CallByName(Me, strTest, CallType.Method, TextBox1.Text)
    2.  
    3. Sub Method1(strMine As String)
    4.   MessageBox.Show(strMine)
    5. End Sub

    Pretty isn't it?
    Last edited by taxes; Jul 14th, 2004 at 04:51 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    10
    hey guys, thanks for the tips. I'm not sure which one is gonna work for me as I don't have the entire program planed out yet. They all look pretty helpful though.

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Walter82
    hey guys, thanks for the tips. I'm not sure which one is gonna work for me as I don't have the entire program planed out yet. They all look pretty helpful though.
    Don't forget, it has emerged on another thread that methods (Subs or Functions) called with CallByName function cannot be declared as private or friend.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    10
    I've decided to go with the callbyname method.

    The way I do it is pretty cool (I think).

    I've got 2 classes: TokenActionPair and Action.

    Action holds all the parameters for the CallByName method. It has a method called TakeAction that does the CallByName call. It also houses all the different action sub routines that my program will use.

    TokenActionPair is just as the name implies. It holds the token as a string, and the action as an Action.

    Then I've got an arraylist I call TokenActionList that holds TokenActionPair objects. When my socket recieves data is passes it to a function that compares the data to all the tokens in the TokenActionPairs of the TokenActionList. If a token is found in the data, the TakeAction method is called on the Action object of the TokenActionPair.

    It works out pretty well. Using the arraylist I can dynamcally add pairs to my code and not worry about matching index values to a select case.

    There is only one problem that I'm having. CallByName won't work if the method isn't public. I get "Public member 'Send' on type 'Action' not found." I'm calling it across classes, but I set the Object parameter of CallByName to the Action object. I thought it would be able to access the private methods that way. I don't really wanna make the methods public since they don't have to be (barring CallByName seems to require it).

    Any clues?

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    10
    heh, I didn't see your reply taxes before making my post. ah well I guess they'll have to be public.

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Walter82
    heh, I didn't see your reply taxes before making my post. ah well I guess they'll have to be public.
    Well, you found it for yourself. Good
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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