Click to See Complete Forum and Search --> : just a question about classes
MrPolite
May 5th, 2002, 02:56 PM
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:
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:rolleyes:
thanks in advance
Cander
May 5th, 2002, 03:22 PM
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?
MrPolite
May 5th, 2002, 03:38 PM
:D
Tygur
May 5th, 2002, 07:19 PM
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.
Dillinger4
May 5th, 2002, 07:44 PM
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.
Public ReadOnly Property Age() As Integer
Get
Return DateDiff(DateInterval.Year, BirthDate, Now())
End Get
End Property
Dillinger4
May 5th, 2002, 07:58 PM
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.
Tygur
May 5th, 2002, 11:03 PM
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.
DevGrp
May 6th, 2002, 08:03 AM
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.
Cander
May 6th, 2002, 08:40 AM
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
Cander
May 6th, 2002, 09:24 AM
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?
Tygur
May 6th, 2002, 12:46 PM
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.
DevGrp
May 6th, 2002, 01:36 PM
Hey Tygur, I think you should stop reading so fast :D . 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. :D
Tygur
May 6th, 2002, 02:00 PM
While I still might've misread your post, I do believe that you might be wrong. This code works:
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?
DevGrp
May 6th, 2002, 03:00 PM
Yes it does.
Dillinger4
May 6th, 2002, 11:47 PM
It does make sense though to declare a read-only shared variable if one is needed with every instance that needs to be created. :p
Tygur
May 6th, 2002, 11:52 PM
Originally posted by DevGrp
Yes it does.
Okay, then it looks like I did misread your post.
MrPolite
May 7th, 2002, 12:06 PM
At the end sounds like everyone misread everything:p
I guess I'll go with Private Shared ReadOnly
Thanks everyone:cool:
Cander
May 7th, 2002, 12:12 PM
Im illiterate!:p
Dillinger4
May 7th, 2002, 11:04 PM
Im just retarded. :p
wolfofthenorth
May 7th, 2002, 11:39 PM
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.:D
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
Tygur
May 8th, 2002, 06:09 PM
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.
hellswraith
May 8th, 2002, 08:24 PM
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.
Tygur
May 8th, 2002, 08:30 PM
Static in C# is Shared in VB.NET. We've been talking about it all along. :)
hellswraith
May 8th, 2002, 08:41 PM
Thanks....I haven't touched VB yet....lol. Now I feel stupid..jk.
Again, thanks for giving me a clue.
wolfofthenorth
May 8th, 2002, 09:18 PM
Tygur,
Thanks for the correction.
I meant to say private. :D :D
MrPolite
May 8th, 2002, 11:58 PM
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?
Tygur
May 9th, 2002, 01:28 AM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.