Hi there. I was working on an object when for whatever reason the compiler kept claiming "this" is not declared, when that's a keyword right? Can anyone help me figure out what exactly is wrong here? I use fbide and freebasic 1.08
I also originally wanted it so that I could use a seperate function to store buffer as an attribute of the object so it doesn't have to read from the file every time the programmer wants to fetch an int from the config file but...oh well, I'll figure that out later. I just wanna know what's up with the "this" keyword here.
type cfgReader
fileName as string
declare function lookfor_integer(byval label as string) as integer
declare constructor(byval new_fileName as string)
declare constructor()
end type
constructor cfgReader(byval new_fileName as string)
this.fileName = new_fileName
end constructor
constructor cfgReader()
this.fileName = ""
end constructor
function lookfor_integer(byval label as string) as integer
dim as integer fileNum = freefile
dim as integer linesCount = 0
dim as string buffer()
open this.fileName for input as #fileNum
do
linesCount += 1
redim preserve buffer(1 to linesCount)
line input #fileNum, buffer(linesCount)
loop until (buffer(linesCount) = "")
close #fileNum
dim as integer result
for index as integer = lbound(buffer) to ubound(buffer) step 1
dim as integer location = 0
dim as integer locationEnd = 0
location = instr(buffer(index), label)
if (location > 0) then
ltrim(buffer(index), label)
result = val(buffer(index))
end if
next index
return result
end function
Avoid the Integer data-type because it is NOT of a fixed size: use Long/ULong instead.
This lets you compile in 32 and 64-bits, without changing the files data (by the executable).
IOW, you've got, what is commonly known as: portable code
@D.J.Peters - this is about file read/write, NOT about the parsing ...
(where a file written by a 64-bit compiled app differs, from a file written by the very same app, compiled in 32-bit)
IOW, the data-files are NOT compatible with both app's (32/64).
Maybe I didn't read the code right, but where in the original poster's code is he reading an integer in binary format from a file? I can find code that parses a number from ascii digits, but not a binary read. So it indeed appears to be parsing. @D.J.Peters is correct that the OP can use integer without problems on any platform in this particular case which is using val(), and without regard to the platform that created the text file he or she is parsing. If you want to be pedantic, then yes you can't safely parse numbers over a certain size without choosing the appropriate data type in FB to parse the number into.
If he was doing a binary read of an integer field, then yes, you're absolutely correct. And also to be truly portable one would have to deal with endian-ness also. But none of that applies here, near as I can tell.
function cfgReader.lookfor_integer(byval label as string) as integer
It is failing because the compiler does not see it as part of the type cfgReader.
Thank you, this is the answer I was looking for.
Now I know most of you got into a debate about what my code is doing; don't worry, lol, I threw it out and am using a publicly available xml parser instead. I just wanted to know why the compiler (and later the linker) complained.