Problem with Select command
Blitz3D Forums/Blitz3D Beginners Area/Problem with Select command
| ||
This one has stumped me, I am using a select statement to check a string variable. I keep getting the error "Expecting 'Case','Default' or 'End Select'. But as you can see I have all three in the code??? Function ReadCTA(file$) ; ; Name$ ; Added in the CheckCTA() Function ; SpoolCPC$ ; ReadLine and find "(STLNUMMER=" value is 8 characters from this point. ; SpoolDIR$ ; ReadLine and find "## " Directory is the rest of the line, trim trailing spaces. ; SpoolNO$ ; ReadLine and find "(MSECTION=" value is from this point and closing ")" after. ; FoundFile=ReadFile(Folder$+"\"+file$) While Not Eof(FoundFile) GotLine$=ReadLine(FoundFile) Select GotLine$ Case Instr(GotLine$,"(STLNUMMER=",1)>0 cta.SpoolCPC$=Mid$(GotLine$,Instr(GotLine$,"(STLNUMMER=",1)+12,8) Case Instr(GotLine$,"## ",1)>0 cta.SpoolDIR$=Mid$(GotLine$,Instr(GotLine$,"## ",1)+4,Len(GotLine$)-3) Case Instr(GotLine$,"(MSECTION=",1)>0 cta.SpoolDIR$=Mid$(GotLine$,Instr(GotLine$,"(MSECTION=",1)+11,Len(GotLine$)-10) Default End Select Wend CloseFile(FoundFile) End Function Thanks Luna. |
| ||
You don't normally use the Case statement like that. Try this: Select True Case Instr(GotLine$,"(STLNUMMER=",1)>0 cta.SpoolCPC$=Mid$(GotLine$,Instr(GotLine$,"(STLNUMMER=",1)+12,8) Case Instr(GotLine$,"## ",1)>0 cta.SpoolDIR$=Mid$(GotLine$,Instr(GotLine$,"## ",1)+4,Len(GotLine$)-3) Case Instr(GotLine$,"(MSECTION=",1)>0 cta.SpoolDIR$=Mid$(GotLine$,Instr(GotLine$,"(MSECTION=",1)+11,Len(GotLine$)-10) Default End Select |
| ||
> You don't normally use the Case statement like that. What?!? leeluna, the problem is not with select, it's with "cta.SpoolCPC$" I don't knwo what you wanted to do, but as it is, the compiler understands that you have somewhere an object named cta whose type is "SpoolCPC", and then it encounters a "$" that says it's a string. It's a syntax error, because either cta is a string, either it's an object but it can't be both. |
| ||
[EDITED to protect the innocent] Koriolos - All of the Case conditions are checking a string (GotLine$) with a Boolean value (True or False) derived from an Instr() carried out on the same variable. This makes no sense. You are correct that the cta variable is used incorrectly. He might need to use either cta\SpoolDIR etc or merely ctaSpoolDIR, depending on what he is trying to achieve. |
| ||
Checking a string against a Boolean value ( 0 or 1 ) actually does make sense in Blitz due to the automatic type conversion.a$ = 7 Select a$ Case 3 : Print "three" Case 7 : Print "seven" Default ; do nothing End Select WaitKey : End A string value is expected so the 3 in 'Case 3' is automatically converted to "3". This is why you can do something like Print "x = " + x There really is nothing syntactically wrong with the Select/Case in the original example. The compiler is confused by the cta.SpoolCPC$ and gives a misleading error message. |
| ||
Floyd - that might be true. But, how likely do you think that is looking at the code? |
| ||
The code may be strange, but technically correct. Here is an example which actually uses Boolean values. a$ = 1 Select a$ Case 5>4 : Print "five is bigger than four" End Select WaitKey : End It's goofy, but it does work. The original code compiles if you rename cta.SpoolCPC to ctaSpoolCPC etc. |
| ||
Koriolos - All of the Case conditions are checking a string (GotLine$) with a Boolean value (True or False) derived from an Instr() carried out on the same variable. This makes no sense. I just hadn't noticed you had replaced "Select GotLine" with "Select True", and couldn't understand why you posted this, when it didn't even worked and didn't address the problem he faced. And as pointed by Floyd it can make sense, although it's pretty obvious here that it's just the result of a misconception of how select works (but only telling him that didn't help him much with his problem).BTW, that's "Koriolis" :) Oh, and must I understand by reading "[EDITED to protect the innocent]" that you took it bad? Had a bad day or what? |
| ||
Thanks for the help people, I got around it by doing it like this:-Function ReadCTA(file$) ; ; Name$ ; Added in the CheckCTA() Function ; SpoolCPC$ ; ReadLine and find "(STLNUMMER=" value is 8 characters from this point. ; SpoolDIR$ ; ReadLine and find "## " Directory is the rest of the line, trim trailing spaces. ; SpoolNO$ ; ReadLine and find "(MSECTION=" value is from this point and closing ")" after. ; FoundFile=ReadFile(Folder$+"\"+file$) While Not Eof(FoundFile) GotLine$=ReadLine(FoundFile) If Instr(GotLine$,"(STLNUMMER=",1)>0 cta\SpoolCPC=Mid$(GotLine$,Instr(GotLine$,"(STLNUMMER=",1)+11,8) EndIf If Instr(GotLine$,"## ",1)>0 cta\SpoolDIR=Mid$(GotLine$,Instr(GotLine$,"## ",1)+14,Len(GotLine$)-14) EndIf If Instr(GotLine$,"(MSECTION=",1)>0 cta\SpoolNO=Mid$(GotLine$,Instr(GotLine$,"(MSECTION=",1)+10,Len(GotLine$)-11) EndIf Wend CloseFile(FoundFile) End Function I will suss out these pesky TYPES if it kills me!!! Luna |
| ||
MasterBeaker you are spot on as well it also works like this:-Function ReadCTA(file$) ; ; Name$ ; Added in the CheckCTA() Function ; SpoolCPC$ ; ReadLine and find "(STLNUMMER=" value is 8 characters from this point. ; SpoolDIR$ ; ReadLine and find "## " Directory is the rest of the line, trim trailing spaces. ; SpoolNO$ ; ReadLine and find "(MSECTION=" value is from this point and closing ")" after. ; FoundFile=ReadFile(Folder$+"\"+file$) While Not Eof(FoundFile) GotLine$=ReadLine(FoundFile) Select True Case Instr(GotLine$,"(STLNUMMER=",1)>0 cta\SpoolCPC=Mid$(GotLine$,Instr(GotLine$,"(STLNUMMER=",1)+11,8) Case Instr(GotLine$,"## ",1)>0 cta\SpoolDIR=Mid$(GotLine$,Instr(GotLine$,"## ",1)+14,Len(GotLine$)-14) Case Instr(GotLine$,"(MSECTION=",1)>0 cta\SpoolNO=Mid$(GotLine$,Instr(GotLine$,"(MSECTION=",1)+10,Len(GotLine$)-11) End Select Wend CloseFile(FoundFile) End Function So basically everyone but ME was correct and I need to spend more time understanding TYPES and also checking my code more carefully before giving up and posting here wasting everybodys time. Thanks Luna. |