Results 1 to 8 of 8

Thread: readonly variable?!!!

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Exclamation readonly variable?!!!

    I just wrote this and compiler didnt underline it, do we have such a thing!!!?!!

    Public ReadOnly curX As Integer

    This is in a class. So is that the same as this?
    Code:
        Public ReadOnly Property CurX() As Integer
            Get
                Return mCurX
            End Get
        End Property
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Tygur
    Guest
    Yep, it exists. The variable basically becomes a constant.

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    It isn't really a constant, because the variable mCurX (assuming is is declared Private) can be changed inside the class. it just can't be changed publicly.

  4. #4
    Tygur
    Guest
    Originally posted by Frans C
    It isn't really a constant, because the variable mCurX (assuming is is declared Private) can be changed inside the class. it just can't be changed publicly.
    I'm not sure what you're saying, but this variable:
    Public ReadOnly MyInt As Integer

    ...and this variable:
    Private ReadOnly MyInt As Integer

    ...cannot be changed anywhere, and they are effectively constants. So I'm not sure what you're referring to.

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I was referring to the code example MrPolite posted:
    VB Code:
    1. Public ReadOnly Property CurX() As Integer
    2.         Get
    3.             Return mCurX
    4.         End Get
    5.     End Property

    Assuming the variable mCurX was defined like this:
    Private mCurX as Integer

    The property CurX is read only, but because this property returns the value of mCurX, it can return variable results, because the variable mCurX can be changed inside the class.

  6. #6
    Tygur
    Guest
    Originally posted by Frans C
    I was referring to the code example MrPolite posted
    Oh. I was referring to the ReadOnly variable he was asking about. I believe you misread his post (and therefore mine too). He was asking if ReadOnly variables existed. I believe he already knew about ReadOnly properties. So I was saying that ReadOnly variables (not properties) are effectively constants.

  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    You're absolutely right tygur, on both accounts.
    I misread the question, and ReadOnly variables act like constants.

    There are only a few differences between contants and ReadOnly variables.

    Next to initializing the variable inline (Public ReadOnly curX As Integer = 5), you can also give it a value in the classes constructor.
    VB Code:
    1. Public ReadOnly curX As Integer
    2.  
    3.     Public Sub New()
    4.         curX = 6
    5.     End Sub
    6.     Public Sub New(ByVal NewX As Integer)
    7.         curX = Newx
    8.     End Sub

    ReadOnly variables can be of any type, including reference type variables, where constants can only be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, String, or the name of an enumeration.
    Last edited by Frans C; Apr 22nd, 2002 at 10:58 AM.

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    tnx all for the info
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width