This keyword suddenly not declared?

New to FreeBASIC? Post your questions here.
Post Reply
Kaleidio
Posts: 3
Joined: Jun 07, 2021 11:46

This keyword suddenly not declared?

Post by Kaleidio »

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.

Code: Select all

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
Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: This keyword suddenly not declared?

Post by Imortis »

This code:

Code: Select all

function lookfor_integer(byval label as string) as integer
Needs to be changed to this code:

Code: Select all

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.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: This keyword suddenly not declared?

Post by MrSwiss »

Important NOTE whenever dealing with files:

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
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: This keyword suddenly not declared?

Post by D.J.Peters »

@MrSwiss parsing the native integer in text files is OK independent of 32/64-bit.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: This keyword suddenly not declared?

Post by MrSwiss »

@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).
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: This keyword suddenly not declared?

Post by caseih »

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.
Kaleidio
Posts: 3
Joined: Jun 07, 2021 11:46

Re: This keyword suddenly not declared?

Post by Kaleidio »

Imortis wrote:This code:

Code: Select all

function lookfor_integer(byval label as string) as integer
Needs to be changed to this code:

Code: Select all

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.
Post Reply