I have a select case I want to be able to check certain expressions... ie:
VB Code:
Select Case Command case "spawn "+x+":"+y 'do this case "spawn "+x1+":"+y2+" "+x2+":"+y2" 'do that end select
Any idea how I would go about doing that?
Printable View
I have a select case I want to be able to check certain expressions... ie:
VB Code:
Select Case Command case "spawn "+x+":"+y 'do this case "spawn "+x1+":"+y2+" "+x2+":"+y2" 'do that end select
Any idea how I would go about doing that?
I assume by wildcard you mean to catch anything that you don't have in your other case expressions. Use Else.
eg
VB Code:
Select Case command case "spawn "+x+":"+y 'do this case "spawn "+x1+":"+y2+" "+x2+":"+y2" 'do that case Else 'do something else end select
Actally I was referring to the first two cases. In this situation I never know for sure what x, y, x1, y1, x2 and y2 may be
Hi,
First of all, if you never know what any of those values are, then how do you expect to differentiate between them? If you want to treat x and y as anything and also treat x1, x2, y1 and y2 as anything, they will all be treated as the same.
What you are looking for is something like:
VB Code:
Dim strCommand As String = "Spawn this" Select Case UCase(Microsoft.VisualBasic.Left(strCommand, 5)) Case "SPAWN" MessageBox.Show("Spawn") End Select
or with Option Compare set to Text,
VB Code:
Dim strCommand As String = "Spawn this" Select Case Microsoft.VisualBasic.Left(strCommand, 5) Case "SPAWN" MessageBox.Show("Spawn") End Select
"if you never know what any of those values are, then how do you expect to differentiate between them? "
by their syantax, one gives absolute coordinates and one gives a range
You're solution probably won't work since theres going to be more than just the two commands so I might just switch to if/else
If their syntax differentiates them, you can check that. But as variable x a/o variable y (which is all you gave us), Taxes is right.
Hi Graff
Getting information out of you is like extracting teeth.:bigyello:
How many commands are there going to be? If there are several then you need a different approach entirely.
Do you apply a different action depending upon the name of the command?
If so, perhaps you should set up a sub or function (which must be Public or Shared) with the same name as the command and then use the CallByName function (I LOVE that one:bigyello: ). If you do this in conjunction with overloading you have no need for Select Case or If....ElseIf routines.
I don't know how many commands there will be but there are already 6
BanList
BanList Count
HelpList
HelpList Count
Spawn x:y (where x and y are coordinates)
Spawn x1:y1 x2:y2
It comes from parsing a tab delimited log file that is structured as:
msgtype receiver sender msg
I would parse
2 Newbie1 Graff %spawn 23,40
And then I would spawn newbie1 at the specific coords if I got
2 Newbie1 Graff %spawn 0,0 30,30
it would spawn them randomly somewhere in that range
How does the callbyname function work in this case, don't I need a control?
I just had a thought, if I the split function I may be able to do
VB Code:
dim arrCommand() as string arrCommand = split(Command," ") select case arrCommand(0) case "%spawn" if arrCommand(3) is nothing then Spawn(arrCommand(2)) else Spawn(arrCommand(2),arrCommand(3)) end if 'do other commands end select
Hi,
Just to clarify your last post, what it appears to me that you are doing is:
1. Put each separate word (or anything else delimited by a space) into one element of an array.
2. If array(0) = "%spawn" you then call a procedure named spawn, which is obviously overloaded, passing to it the required parameter(s).
You also seem to be using "spawn" as though it is a VB.NET function or command. What precisely do you mean by
"And then I would spawn newbie1 at the specific coords if I got"
I have previously assumed that you had procedures named "spawn", "banlist" and "helplist" etc which required parameters to be passed for x & y (denoting 2 coordinates)and that you then had similarly named procedures requiring parameters for x1,y1 x2,y2 (denoting 2 ranges).
Please confirm my understanding.
One last thought. You are talking about VB.NET here arn't you?
Yes VB.Net
My program is a bot for a game which parses log files and then responds accordingly via predefined commands.
1. yes
2. yes
The spawn function does precisely this
VB Code:
Public Sub Spawn(Coord as String) dim x,y as integer 'Some authentication goes here 'to make sure user has proper rights 'to request this command 'Coord comes in as x:y x = Coord.substring(1,1) y = Coord.substring(3,1) 'Send Message to game ' SendMsg is a method that uses the postMessage api to communicate with the game 'ReplyTo is a global variable to the class and is defined elsewhere SendMsg(":" + ReplyTo + ":*spawn " + x + "," + y) end sub Public Sub Spawn(Coord1 as string, Coord2 as string) dim x,y,x1,x2,y1,y2 as integer 'Some authentication goes here 'to make sure user has proper rights 'to request this command 'Coord comes in as x:y x1 = Coord1.substring(1,1) y1 = Coord1.substring(3,1) x2 = Coord2.substring(1,1) y2 = Coord2.substring(3,1) x = (x2 - x1 + 1) * Rnd() + x1 y = (y2 - y1 + 1) * Rnd() + y1 'Send Message to game ' SendMsg is a method that uses the postMessage api to communicate with the game 'ReplyTo is a global variable to the class and is defined elsewhere SendMsg(":" + ReplyTo + ":*spawn " + x + "," + y) end sub
Not all commands require ranges, infact so far only the spawn one does which is why it was in the example and the others were not.
HI,
OK. All is now clear.
In order to avoid a long Select Case or If.. Else If.... you will have to use the CallByName function.
e.g. put the name of the procedure to be called into a string, say strName
then use something like
if number of parameters>1 then
CallByName(strName)(param1, param2)
else
CallByName(strName)(param1)
End If
You will of course have to use dummy parameters in those procedures which do not require parameters.
All procedures will have to be Public.
Hope that is clear.
I'll give it a shot, thanks.