|
-
Nov 22nd, 2002, 01:43 PM
#1
Thread Starter
PowerPoster
Class variables in VB? (no, I won't use global vars!)
Hello,
Is there any way to do class variables in VB? Here's the problem:
I want to make 1 (template) class, let's name it cEnemy. Now what I need is a variable which every instance of cEnemy can access. In C you'd just declare a static variable inside the class, so is there any way in VB? I repeat, I don't want to use global variables or public variables in modules!
thanks,
Fox
-
Nov 22nd, 2002, 01:54 PM
#2
How about creating a class called cEnemies, it contains a collection of class enemies, which you can add and remove from, as well as containing any variables that all cEnemy classes would use.
Hope this helps,
-
Nov 22nd, 2002, 01:59 PM
#3
Thread Starter
PowerPoster
Not really, the array (collections are too slow) which holds all enemies should be a global variable.. Also I don't have all enemies in that array, so this also has to work:
VB Code:
Dim E1 as cEnemy
Dim E2 as cEnemy
'Instance variables
E1.x = 10
E2.x = 22
'Class variables
E1.Desription = "An Enemy"
[b]E2.Desription[/b] = [b]"Someone"[/b]
MsgBox [b]E1.Desription[/b] 'Should show [b]"Someone"[/b]
Last edited by Fox; Nov 22nd, 2002 at 02:04 PM.
-
Nov 22nd, 2002, 02:08 PM
#4
So what you are asking for is a global variable, but you dont want to use global variables..... hmmm... I'll have to think about this one.
-
Nov 22nd, 2002, 02:11 PM
#5
Thread Starter
PowerPoster
It's called 'class variable' and you know it from C/Java/etc.
-
Nov 22nd, 2002, 02:18 PM
#6
What is your objection to a global variable in this case. A class variable and a global variable would not be stored any differently in memory.
-
Nov 22nd, 2002, 02:26 PM
#7
Thread Starter
PowerPoster
I can't use Globals because...
- Creating a class template I want to use later - don't want to have a class modules and a class for each template
- I want to access both kinds of variables by Object.*
- And IMHO that'd be crappy program design.
-
Nov 22nd, 2002, 03:04 PM
#8
I suppose .NET is out of the question.
You want the persistance of a static variable, but with local scope only. Could you imitate this by setting up your "class" in a single code module such that you can make module scope variables, and expose access to them only through a handful of public Get/Set routines?
This is not a programatic solution, but rather an organizational one. However, VB isn't a true OO language, so a few limitations must be expected.
-
Nov 22nd, 2002, 03:17 PM
#9
Thread Starter
PowerPoster
Just what I excepted :-/ Well I have to use classes, else I'd have to re-organize the whole program.. hm, think I just have to live with global vars *sigh* .. or switch to dotNet .. yeah, why not ^^*
-
Nov 22nd, 2002, 03:23 PM
#10
How do static variables behave within a class in VB? I haven't used them, but I see they are there.
-
Nov 22nd, 2002, 03:36 PM
#11
A static variable would still be instance specific.
You could use two classes one that holds the array as well as any 'global' variables for them then the other one that has the actual class for the 'items'. Basically the same way a strong typed collection works.
-
Nov 22nd, 2002, 03:41 PM
#12
Hyperactive Member
Fox, declare a Private variable in a module, and then create a Property Let and a Property Get for that variable inside your module.
VB Code:
' Place this code in Module1
Private lngMyVariable As Long
Property Let MyVariable(NewVal As Long)
lngMyVariable = NewVal
End Property
Property Get MyVariable As Long
MyVariable = lngMyVariable
End Property
' Then, anywhere else in your program you can use
MsgBox Module1.MyVariable
' or
Module1.MyVariable = 7
' to access the variable, without it being Global.
And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.
-
Nov 22nd, 2002, 03:48 PM
#13
That isn't quite what he wants brenaaro, he wants a "variable" that ... oh heck....
Given a class that has two properties, xNum (long) and Description (string)
If I dim two variables as this type, obj1 and obj2, I can set xNum and Description on each one and they would be separate. What Fox is looking for is a way to say, "Hey, keep xNum indexpendant, but if I change Description on obj1, obj2 needs to update AUTOMATICALLY."
It's basically a shared variable among ALL instances of the class. I've heard about it done in other languages, but since VB isn't a true OOP language, it doesn't really support this.
But, what you could do is a "wrapper" class that you would use to expose the "shared" property and then loop through the internal array of objects and set the property. It would take some doing, but can be done, and w/o a collection.
-
Nov 23rd, 2002, 07:57 PM
#14
Thread Starter
PowerPoster
Exactly, techgnome.. well this would become a little unhandy - however, thanks for the input..
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
|