Results 1 to 27 of 27

Thread: just a question about classes

  1. #1

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

    just a question about classes

    I'm making a class and I will be making about 30 objects from that class. Would it be bad to declare all the constants of my class as shared? like this:

    Code:
    Public Class FFUser
           Shared ReadOnly TAG_ADMIN As String = "isAdmin"
           Shared ReadOnly TAG_USERNAME As String = "UserName"
           ....
           Public Function ...
    End Class

    I was just thinking that this would save some memory if I create 30 or more instances of this class
    Am I wrong

    thanks in advance
    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
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    I think a shared contant would be pointless as I beleive shared persists the data in the variable between class instances but since the vlaues of those variables dont change..what would be the point?

    Am I right?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  4. #4
    Tygur
    Guest
    Originally posted by Cander
    I think a shared contant would be pointless as I beleive shared persists the data in the variable between class instances but since the vlaues of those variables dont change..what would be the point?

    Am I right?
    It's not pointless, because the values in readonly variables can possibly be different for different instances of the class, because their values can be changed in the constructor of the class.

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by Tygur
    It's not pointless, because the values in readonly variables can possibly be different for different instances of the class, because their values can be changed in the constructor of the class.
    Once he sets an initial value to a ReadOnly variable i think that it cannot be changed. For instance if you had a read only property
    such as the following you would get a syntax error if you attempt to implement a Set block. From what i know of .Net so far declaring a variable ReadOnly is like using the final keyword in Java.
    Code:
    Public ReadOnly Property Age() As Integer
       Get 
          Return DateDiff(DateInterval.Year, BirthDate, Now())
       End Get
    End Property

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yes Cander is right about variables declared as Shared having persistent data across multiple instances. I think Mr Polite. might be right in respect to saving some memory since i think that a shared variable within multiple instances of a class share the same memory address.

  7. #7
    Tygur
    Guest

    Gotta clear a few things up..

    Constants are not the same thing as ReadOnly variables. After rereading posts, I found that the original question was about readonly variables, Candor's reply was for constants, and I replied to Candor's post, assuming that the was talking about readonly variables (which still might've been his intention). Then Dilenger4 popped in with one almost true statement and then some stuff that confused me in his second post.

    Here are the facts:
    ReadOnly variables can be set from within the constructor of the class, and nowhere else. If this ReadOnly variable is Shared, then the constructor with the code that sets it must also be Shared. By the way, this shared constructor will only run once, instead of every time a new instance is created. Unlike with the Java final keyword, you can set the ReadOnly variable as many times as you like, as long as you're doing it within a constructor of the class. ReadOnly variables are very different from constants, which cannot be set from anywhere at all.

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I dont think you want to declare those constants as shared if you want to use them in the objects that you instantiated from the class.

    Anything declared as shared in a class will not be seen if that class is instantiated.

    MrPolite that is ok, if you intended to access those readonly variables using public properties.

  9. #9
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Gotta clear a few things up..

    Originally posted by Tygur
    Constants are not the same thing as ReadOnly variables. After rereading posts, I found that the original question was about readonly variables, Candor's reply was for constants, and I replied to Candor's post, assuming that the was talking about readonly variables (which still might've been his intention). Then Dilenger4 popped in with one almost true statement and then some stuff that confused me in his second post.
    well he did mention contants in his original post which is what I was going by. My reply was mostly a guess as I havent used shared yet and havent read much about it. Seems like I was fairly close though!

    So much new stuff to learn. Got three, very thick .NET books to finish reading! :P
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    I do have 1 question about shared. Is it that when a new instance is created it gets the values from the beggining isntance but if 1 of the instances changes the value of the variable, will all other isntances change? Did that make sense?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11
    Tygur
    Guest
    Originally posted by DevGrp
    Anything declared as shared in a class will not be seen if that class is instantiated.
    That's not true. A function, sub, or property that isn't shared can access a shared variable without a problem.

  12. #12
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Hey Tygur, I think you should stop reading so fast . What I mean is, if a variable is declared as shared in a class, when that class is instantiated, that shared variable will not be seen.

    That does not mean you cant get to it. It just means that, if you type the dot after the instantiated class, and the intellisense pops up with all the private and public fields, methods and what ever else, you will not see that shared variable.

    Now to access that shared variable, you can do one of two things, either using the class if its also declared as shared and not instantiating it, or you can have special accessor methods or properties to access those shared variables.

    I said that, but you was probably reading too fast.

    MrPolite that is ok, if you intended to access those readonly variables using public properties.
    Slow down.

  13. #13
    Tygur
    Guest
    While I still might've misread your post, I do believe that you might be wrong. This code works:
    Code:
    Module Module1
        Sub Main()
            Dim T As New TestClass()
            MsgBox(T.PubSharedVar)
            MsgBox(T.GetSomething)
        End Sub
        Class TestClass
            Public Shared PubSharedVar As String = 1
            Private Shared PrivSharedVar As Integer = 2
            Public Function GetSomething() As Integer
                Return PrivSharedVar + PubSharedVar
            End Function
        End Class
    End Module
    Does that agree with what you're saying?

  14. #14
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Yes it does.

  15. #15
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    It does make sense though to declare a read-only shared variable if one is needed with every instance that needs to be created.

  16. #16
    Tygur
    Guest
    Originally posted by DevGrp
    Yes it does.
    Okay, then it looks like I did misread your post.

  17. #17

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    At the end sounds like everyone misread everything
    I guess I'll go with Private Shared ReadOnly

    Thanks everyone
    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!!

  18. #18
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Im illiterate!
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  19. #19

  20. #20
    Addicted Member wolfofthenorth's Avatar
    Join Date
    Jan 2001
    Location
    Tatooine
    Posts
    169
    I think that if you declare a variable as

    Shared SharedVar as String = "1"

    By default is has Friend level scope and won't be visible in intellisence outside the class, but as we can see from the code Tygur posted (with a small change)... if you declare a variable as Public Shared it can be seen outside the class with intellisence.

    Module Module1
    Sub Main()
    Dim T As New TestClass()

    ''''''''''''''''''''''''''''''''''''''''''''''
    T.PubSharedVar = 10 'Intellisence works.
    ''''''''''''''''''''''''''''''''''''''''''''''

    MsgBox(T.PubSharedVar)
    MsgBox(T.GetSomething)
    End Sub
    Class TestClass
    Public Shared PubSharedVar As String = 1
    Private Shared PrivSharedVar As Integer = 2
    Public Function GetSomething() As Integer
    Return PrivSharedVar + PubSharedVar
    End Function
    End Class
    End Module
    That which does not kill us, only makes us stronger.

  21. #21
    Tygur
    Guest
    Originally posted by wolfofthenorth
    By default is has Friend level scope and won't be visible in intellisence outside the class
    Actually it's Private by default, which works in the way you described. If Friend was the default, it would be visible outside the class.

  22. #22
    hellswraith
    Guest
    Just to throw a wrench into the conversation, what about the keyword Static? Or is this only in C#? Cause if you declared some static variables there would only be one instance of those variables for each object created from the class, unless I just have no clue of what I am talking about....lol....which is often when it comes to .Net.....tell me if/how I am wrong if you have the time...I would like to know for myself so later I don't look like a fool.

  23. #23
    Tygur
    Guest
    Static in C# is Shared in VB.NET. We've been talking about it all along.

  24. #24
    hellswraith
    Guest
    Thanks....I haven't touched VB yet....lol. Now I feel stupid..jk.

    Again, thanks for giving me a clue.

  25. #25
    Addicted Member wolfofthenorth's Avatar
    Join Date
    Jan 2001
    Location
    Tatooine
    Posts
    169
    Tygur,

    Thanks for the correction.

    I meant to say private.
    That which does not kill us, only makes us stronger.

  26. #26

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Tygur
    Static in C# is Shared in VB.NET. We've been talking about it all along.
    umm so what's the difference when you declare a variable as shared or static in a function? no difference? or are you just talking about the function declaration?
    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!!

  27. #27
    Tygur
    Guest
    Originally posted by MrPolite

    umm so what's the difference when you declare a variable as shared or static in a function? no difference? or are you just talking about the function declaration?
    Well, you can't declare a Shared variable in a function.

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