Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: string convert negative typing typeless automagically
a= 5 + "27" b= strcat("Sue is ", 7 , " years old") Message(a,b)
x="abcd--" if x>1000 Message("Hello", "World") endif
WinBatch looks at the type of the x variable, decides that it is NOT a number, looks at the 1000, decides it is a number, but not compatible with a compare with x, so it converts the 1000 to a string and the real compare is if "abcd--" > "1000".
Then it does a string compare. "a" comes after "1" in the ansi collating sequence, so "abcd--" is indeed greater than "1000".
This code worked in version 99p:
x=" " while 1 x=x+1 if x==10 then break endwhile exitbut not now. Now you get error "'x=x+1' 3057: Variable could not be converted to a valid number".
Is this a bug, or will variables need to be initialized to zero?
WinBatch tries *real hard* to convert strings to numbers for processing. But it has to be carefull not to get carried away otherwise stuff like
if 0 == " "would be true. I think a bug fix in this area may have bit you.
Yes. Numbers should be initialized to numbers not nulls strings or blanks.
You can do stuff like
a = "5" + "3"which is adding the strings 5 and 3. Winbatch recognizes that you want to add two things, but they are strings. So it sees if it can convert these strings to numbers. If the conversion is successful, it adds the numbers.
So what is a number. One must specify rules that describe the allowed numbers.
so
2 is a number 2.0 is a number 2. is a number 0.2 is a number .2 is a number . well it was a number for a while (0.0) but we fixed it. 2e10 is a number 1e1 is a number e is not a number (it was for a while) 1e0" " was a number in some cases (0) until we decided to insist that to be a number the string actually has to have a number in it.
Many language have "typing" also called "strong typing" where you have to very specifically state what each variable is and any attempt to use it for another purpose gets some kind of error.
WinBatch sports "negative-typing" where it will try all kinds of tricks to attempt to coerce the provided item into the kinds it actually needs for the computation
a=strcat("5" + "4", " Hmmm")Converting numbers to strings is usually not a problem until you get to the wierd floating point numbers that can only be represented in E notation
-1.2345E-123Which looks confusing in a string. However converting strings to numbers can be tricky
1A1 is not a number 1E1 isunder the theory that if you need a number and the string fits the category, then lets do it.
Effects:
"1E1" == "010" basically 10 == 10But if you are comparing part numbers to see if part number 1E1 is the same as part number 010 you will get wierd results. That's life. There are workarounds.
So "negative-typing" is handy but also dangerous. That's why some language has "strong-typing" to make you define all your variables and what they can do before they even look at your code. Actually it is essential for bigger projects. WinBatch on the other hand was designed as a quick and dirty development language for shorter programs where one person could actually understand all the code involved. Negative typing is very very handy for small programs.
Article ID: W12985
Filename: Converting Strings to Numbers and Negative Typing.txt
File Created: 2001:04:03:14:57:30
Last Updated: 2001:04:03:14:57:30