Results 1 to 29 of 29

Thread: can someone explain classes and modules

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    can someone explain classes and modules

    will you help me learn and show me some sample code

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: can someone explain classes and modules

    Don't really need to show code to explain them really. Modules are used for you to store functions, subs, constants, API's, and variables publicly or privately, and is a good way to organize code. For example, lets say you are making a game. You can store all the 3D engine code in one module, physics in another, API's and constants in another, the game engine in another, etc.

    Class modules are used for making your own objects. You can store functions, subs, and variables in there like a module, but it has limitations. Like for example, you can't use arrays. Unlike regular modules, class modules can have properties, methods, and events, like you see with objects you would normally see in a form, like command buttons, textboxes, etc. After writing some code to a class module, you would need to do either this, which is called Early Binding:

    VB Code:
    1. Dim MyObject As MyClassModule
    2.  
    3.      Private Sub Form_Load()
    4.  
    5.           Set MyObject = New MyClassModule
    6.  
    7.      End Sub

    Or this, which is called loose binding:

    VB Code:
    1. Dim MyObject As New MyClassModule

    Or this which is called Late Binding:

    VB Code:
    1. Private Sub Test()
    2.  
    3.           Dim MyObject As MyClassModule
    4.  
    5.           Set MyObject = New MyClassModule
    6.  
    7.      End Sub


    Now MyObject can access the code within a class module. You would do something like so to do it:

    VB Code:
    1. Private Sub Test()
    2.  
    3.           MyObject.MyFunction
    4.  
    5.      End Sub

    Hope this helps.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    so lets say i was making a game and i wanted enemy arrays in a module i could just load them like this
    VB Code:
    1. dim enemyHP as integer
    and then i can call them and if so how.

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: can someone explain classes and modules

    You just need a type for enemies, I would think. That way you could link the description, the Mode of Operation MO, and any other relevant info in the base type. Then you could use something like this;
    VB Code:
    1. Dim Enemies(10) as Enemy


    Then you could use Enemies(1).Description

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    could i like enemy1.hp = 25

  6. #6
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: can someone explain classes and modules

    Quote Originally Posted by xypherx
    could i like enemy1.hp = 25
    Yes, but you would need to set up a Class Module or Type first. It seems like you have the idea of how to use it down though. Here is a decent tutorial on custom Classes: http://msdn.microsoft.com/library/de...sesobjects.asp

    Its for Microsoft Office and VBA but you can totally apply it to VB6. It is slightly hard to understand, but I used it as a starting point for making my first Class (which I am in the middle of making). Also when you go to Add a class module try using the Class Wizard. Let me know if you need help getting started with it.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    would you help me more i really couldnt understand it...

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: can someone explain classes and modules

    Maybe something like this?

    VB Code:
    1. Option Explicit
    2.  
    3.   Private Type Enemy
    4.     tName As String
    5.     tHP As Integer
    6.     tPower As Integer
    7.     tDead As Boolean
    8.   End Type
    9.  
    10.  
    11. Private Sub Form_Load()
    12.  
    13.   Dim Enemies(10) As Enemy
    14.   Enemies(0).tDead = False
    15.   Enemies(0).tName = "David"
    16.   Enemies(0).tHP = 80
    17.   Enemies(0).tPower = 100
    18.  
    19. End Sub

  9. #9
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: can someone explain classes and modules

    Yeah, User Defined Types are somewhat like class modules, except that you can only store variables and arrays.

    Here's one you can use on sprites:

    VB Code:
    1. Public Type Sprite_Type
    2.  
    3.      X As Single
    4.      Y As Single
    5.      Width As Single
    6.      Height As Single
    7.      Velocity_X As Single
    8.      Velocity_Y As Single
    9.      State As Long
    10.      
    11. End Type

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: can someone explain classes and modules

    Quote Originally Posted by Jacob Roman
    VB Code:
    1. Dim MyObject As MyClassModule
    2.  
    3.      Private Sub Form_Load()
    4.  
    5.           Set MyObject = New MyClassModule
    6.  
    7.      End Sub
    ^^ Early binding = Always good

    VB Code:
    1. Dim MyObject As Object
    2. Set MyObject = New MyClassModule
    ^^ Late binding = Semi-OK but don't use if you can help it

    Quote Originally Posted by Jacob Roman
    VB Code:
    1. Dim MyObject As New MyClassModule
    ^^ Loose Binding = Very Bad

    A user-defined type (Hate that name, it is a structure really) is just a block of data. Whereas a class module is a full-blown object with its own executable code contained within.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    i see what you are saying but how would i call it into my program...

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: can someone explain classes and modules

    Well basically if you have a class module you need to instantiate it in order to be able to use it. Methods of instatiation are shown above

    Then, you use its properties, methods, in the normal VB way: Object.Member.

    Can you be a bit more specific as to your problem?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    i was told it was best to know classes for many things like i wanted to do this

    dim enemy(10) as enemy
    dim enemyMaxHp(10) as EnemyMaxHp

    would i have to do that set enemy as new

    and then could i do enemy(0).name = Slime

  14. #14
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: can someone explain classes and modules

    Yes. Your class CEnemy would need a Name property, however that is reserved by VB, so it would have to be EnemyName or something like that. And yes you need to do Set Enemy(0) = New CEnemy.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    so would i have to do it like set enemy(0) to (10) as = cEnemy


    and name would be like EnemyName(10) as EnemyName

    then set enemyname(0) = CEnemyName

    then CEnemyName1 = slime

  16. #16
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: can someone explain classes and modules

    No no. You only need the one CEnemy class, that class has the properties EnemyName and MaxHP or whatever.

    Like this:
    VB Code:
    1. ' In CEnemy
    2.  
    3. Private mstrEnemyName As String
    4.  
    5. Public Property Get MaxHP() As Long
    6.     MaxHP = 200  ' or whatever
    7. End Property
    8.  
    9. Public Property Get EnemyName() As String
    10.     EnemyName = mstrEnemyName
    11. End Property
    12.  
    13. Public Property Let EnemyName(ByVal pstrNewName As String)
    14.     mstrEnemyName = pstrNewName
    15. End Property

    And then you would declare it like this
    VB Code:
    1. Dim objEnemy As CEnemy
    2.  
    3. ' And instantiate it:
    4. Set objEnemy = New CEnemy
    5.  
    6. ' Give it a name
    7. objEnemy.EnemyName = "Fred"
    8.  
    9. ' Get its max hit points
    10. Msgbox objEnemy.EnemyName & "'[color=black]s maximum hit points is: " & objEnemy.MaxHP[/color]
    11.  
    12. ' And when it needs to be killed off (from the program's point of view anyway)
    13. Set objEnemy = Nothing

    HTH

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    can one of you write a tutorial in vb to show me so i can understand it ?

  18. #18
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: can someone explain classes and modules

    Xypherx, I strongly reccomend opening a new project, adding a class module, and when the window pops up instad of select Blank Class Module, select VB Class Builder. Then play around with adding methods, and properties, then look at the code that VB writes for you.

    Someone will still have to write/link a tutorial for you or you can use the confusing one I mentioned early to understand the property Let Get part. I actually hope someone has a good tutorial, I could use one too.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    that tutorial you sent me confused me so much for some reason. but ill try what you said, and yes i really do hope somebody writes/links a tutorial for us

  20. #20
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: can someone explain classes and modules

    Your right, it uses very complex language (for no reason at all, some authors are just weird). But its the only one I know of (obviously there are others).

    The part of property let get set explains it enough that after you use the VB Class builder you should be good to go.

    By the way, after you use the class builder to make the original file you can go back to it (go to make a new class then select it just like you did before) and edit/add things to the class you made and VB will pop the code in at the proper spot.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    could i just use a regular module and do

    VB Code:
    1. public enum enemy
    2. dim hp as integer
    3. dim mp as integer
    4. dim name as string
    5. dim weapon as string
    6. dim strength as integer
    7.  
    8.  
    9. enemy.hp = "25"
    10. enemy.mp = "10"
    11. enemy.name = "Slime"
    12. enemy.weapon = "ooze"
    13. enemy.strength = "5"

  22. #22
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: can someone explain classes and modules

    Nope. If you tried, you would have gotten an error. From MSDN:

    Enumeration variables are variables declared with an Enum type. Both variables and parameters can be declared with an Enum type. The elements of the Enum type are initialized to constant values within the Enum statement. The assigned values can't be modified atrun time and can include both positive and negative numbers. For example:

    Enum SecurityLevel
    IllegalEntry = -1
    SecurityLevel1 = 0
    SecurityLevel2 = 1
    End Enum

    An Enum statement can appear only atmodule level. Once the Enum type is defined, it can be used to declare variables, parameters, orprocedures returning its type. You can't qualify an Enum type name with a module name. Public Enum types in aclass module are not members of the class; however, they are written to thetype library. Enum types defined instandard modules aren’t written to type libraries. Public Enum types of the same name can't be defined in both standard modules and class modules, since they share the same name space. When two Enum types in different type libraries have the same name, but different elements, a reference to a variable of the type depends on which type library has higher priority in the References.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    so that means what?? i'm confused

  24. #24
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: can someone explain classes and modules

    You cannot use DIM in an enum. They are only for like info, like Play=1 and Stop = 2. Then you can refer to Play, and it gets returned as 1.

    You need a class, and you need to instantiate an array of them to have more than one. Use the examples from above. That's the best way to do it.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    i still dont get how to use them in a form

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: can someone explain classes and modules

    Take a look at Woka's class example:
    Attached Files Attached Files

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    one more quick question how would i get my arrays from a regular module.

  28. #28
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: can someone explain classes and modules

    Usually, I declare public arrays in a module
    VB Code:
    1. Dim MyArray() as String

    and then in the form, redim it to give it a value
    VB Code:
    1. ReDim MyArray(4)

    Then, any routine can assign or read values from MyArray()

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: can someone explain classes and modules

    can i like dim enemy(10) as string

    enemy(0).name = "Slime"
    enemy(0).hp = "25"

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