Results 1 to 14 of 14

Thread: Class variables in VB? (no, I won't use global vars!)

  1. #1

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    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

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    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,

  3. #3

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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:
    1. Dim E1 as cEnemy
    2. Dim E2 as cEnemy
    3.  
    4. 'Instance variables
    5. E1.x = 10
    6. E2.x = 22
    7.  
    8. 'Class variables
    9. E1.Desription = "An Enemy"
    10. [b]E2.Desription[/b] = [b]"Someone"[/b]
    11.  
    12. MsgBox [b]E1.Desription[/b] 'Should show [b]"Someone"[/b]
    Last edited by Fox; Nov 22nd, 2002 at 02:04 PM.

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    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.

  5. #5

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    It's called 'class variable' and you know it from C/Java/etc.

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    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.

  7. #7

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    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.

  9. #9

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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 ^^*

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    How do static variables behave within a class in VB? I haven't used them, but I see they are there.

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  12. #12
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391
    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:
    1. ' Place this code in Module1
    2.  
    3. Private lngMyVariable As Long
    4.  
    5. Property Let MyVariable(NewVal As Long)
    6.     lngMyVariable = NewVal
    7. End Property
    8.  
    9. Property Get MyVariable As Long
    10.     MyVariable = lngMyVariable
    11. End Property
    12.  
    13. ' Then, anywhere else in your program you can use
    14. MsgBox Module1.MyVariable
    15. ' or
    16. Module1.MyVariable = 7
    17.  
    18. ' 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.

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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
  •  



Click Here to Expand Forum to Full Width