|
-
May 5th, 2002, 02:56 PM
#1
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!!
-
May 5th, 2002, 03:22 PM
#2
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?
-
May 5th, 2002, 03:38 PM
#3
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!!
-
May 5th, 2002, 07:19 PM
#4
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.
-
May 5th, 2002, 07:44 PM
#5
Dazed Member
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
-
May 5th, 2002, 07:58 PM
#6
Dazed Member
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.
Last edited by Dilenger4; May 5th, 2002 at 08:04 PM.
-
May 5th, 2002, 11:03 PM
#7
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.
-
May 6th, 2002, 08:03 AM
#8
Frenzied Member
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.
-
May 6th, 2002, 08:40 AM
#9
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
-
May 6th, 2002, 09:24 AM
#10
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?
-
May 6th, 2002, 12:46 PM
#11
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.
-
May 6th, 2002, 01:36 PM
#12
Frenzied Member
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.
-
May 6th, 2002, 02:00 PM
#13
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?
-
May 6th, 2002, 03:00 PM
#14
Frenzied Member
-
May 6th, 2002, 11:47 PM
#15
Dazed Member
It does make sense though to declare a read-only shared variable if one is needed with every instance that needs to be created.
-
May 6th, 2002, 11:52 PM
#16
Originally posted by DevGrp
Yes it does.
Okay, then it looks like I did misread your post.
-
May 7th, 2002, 12:06 PM
#17
-
May 7th, 2002, 12:12 PM
#18
Im illiterate!
-
May 7th, 2002, 11:04 PM
#19
Dazed Member
Im just retarded.
-
May 7th, 2002, 11:39 PM
#20
Addicted Member
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. 
-
May 8th, 2002, 06:09 PM
#21
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.
-
May 8th, 2002, 08:24 PM
#22
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.
-
May 8th, 2002, 08:30 PM
#23
Static in C# is Shared in VB.NET. We've been talking about it all along.
-
May 8th, 2002, 08:41 PM
#24
Thanks....I haven't touched VB yet....lol. Now I feel stupid..jk.
Again, thanks for giving me a clue.
-
May 8th, 2002, 09:18 PM
#25
-
May 8th, 2002, 11:58 PM
#26
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!!
-
May 9th, 2002, 01:28 AM
#27
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|