Author Topic: Problem with Const variables in a class  (Read 228 times)

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Problem with Const variables in a class
« on: August 28, 2016, 05:45:56 pm »
I have been writing a library for the accelerometer. This uses lots of bitmasks.

Originally I defined these as Const - for example Const FS_MASK As Byte =0x03

What I found is that you can't refer to this constant outside the class (say from another class), it acts like a Private Constant, not a Shared Constant. Do you have to refer to the base class? Rather than an instance of the class?

Ie BASE.FS_MASK vs instance.FS_MASK. Not sure why this would matter, it's a constant so the two would be the same.

As a workaround I redefined them as Readonly variables (which works fine).

Is this how it works?

Thanks,

Share on Facebook Share on Twitter


mc-John

  • Global Moderator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: Problem with Const variables in a class
« Reply #1 on: August 28, 2016, 09:59:14 pm »
Const (constant) and Enum (enumerations) work properly as you can see in the code below.
A constant is always shared so you can address them by prefixing the class, just like all shared variables

-John-

Code: [Select]
Enum values As Integer
    v1 = 1
    v2 = 2
End Enum
Class test15
    Shared Event Boot()
        Dim y As Integer = test.yyyy
        y = values.v1
    End Event
End Class
Class test
    Const yyyy As Integer = 1   
End Class


Both Enum and Const are generated as literals as displayed in the listing below. (Extract from the generated .lst file in the project directory)

Code: [Select]
****** BLOCKS and VARIABLES for values
    Block Struct (Line 1:4)
        values          v1              (Literal=1)
        values          v2              (Literal=2)
        Total Size of Member data including base classes = 0.
    End Block

****** BLOCKS and VARIABLES for test
    Block Class (Line 11:13)
        test            Me              (ReadOnly, Offset=0)
        Object          MyBase          (ReadOnly, Offset=0)
        Integer         yyyy            (Literal=1)
        Total Size of Member data including base classes = 0.
    End Block

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Problem with Const variables in a class
« Reply #2 on: August 28, 2016, 10:10:47 pm »
What I'm saying is that this doesn't work:

Code: [Select]
Class test
    Const xxx As Integer =1
End Class

Class Main
    Shared yyy As test
    Shared Event Boot()
        yyy = new test()
        If yyy.xxx = 1 Then // error here
             //Stuff
        End If
    End Event
End Class
 

Readonly vs Const does though. Just not sure this is the way it is supposed to work.

Error is xxx is not a Shared variable.
« Last Edit: August 28, 2016, 10:13:21 pm by Nick_W »

mc-John

  • Global Moderator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: Problem with Const variables in a class
« Reply #3 on: August 28, 2016, 10:34:46 pm »
Yes it is suppose to work like this. Shared variable can only accessed by the class and not by an object (instantiated class)
By the way I the the error "Undefined function, indexer or variable or not visible."

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Problem with Const variables in a class
« Reply #4 on: August 28, 2016, 10:39:45 pm »
Yeah, sorry, typing from memory here.

Just good to know how it works.