will you help me learn and show me some sample code
Printable View
will you help me learn and show me some sample code
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:
Dim MyObject As MyClassModule Private Sub Form_Load() Set MyObject = New MyClassModule End Sub
Or this, which is called loose binding:
VB Code:
Dim MyObject As New MyClassModule
Or this which is called Late Binding:
VB Code:
Private Sub Test() Dim MyObject As MyClassModule Set MyObject = New MyClassModule End Sub
Now MyObject can access the code within a class module. You would do something like so to do it:
VB Code:
Private Sub Test() MyObject.MyFunction End Sub
Hope this helps.
so lets say i was making a game and i wanted enemy arrays in a module i could just load them like thisand then i can call them and if so how.VB Code:
dim enemyHP as integer
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:
Dim Enemies(10) as Enemy
Then you could use Enemies(1).Description
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.aspQuote:
Originally Posted by xypherx
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.
would you help me more i really couldnt understand it...
Maybe something like this?
VB Code:
Option Explicit Private Type Enemy tName As String tHP As Integer tPower As Integer tDead As Boolean End Type Private Sub Form_Load() Dim Enemies(10) As Enemy Enemies(0).tDead = False Enemies(0).tName = "David" Enemies(0).tHP = 80 Enemies(0).tPower = 100 End Sub
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:
Public Type Sprite_Type X As Single Y As Single Width As Single Height As Single Velocity_X As Single Velocity_Y As Single State As Long End Type
^^ Early binding = Always goodQuote:
Originally Posted by Jacob Roman
^^ Late binding = Semi-OK but don't use if you can help itVB Code:
Dim MyObject As Object Set MyObject = New MyClassModule
^^ Loose Binding = Very BadQuote:
Originally Posted by Jacob Roman
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.
i see what you are saying but how would i call it into my program...
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?
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
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.
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
No no. You only need the one CEnemy class, that class has the properties EnemyName and MaxHP or whatever.
Like this:
VB Code:
' In CEnemy Private mstrEnemyName As String Public Property Get MaxHP() As Long MaxHP = 200 ' or whatever End Property Public Property Get EnemyName() As String EnemyName = mstrEnemyName End Property Public Property Let EnemyName(ByVal pstrNewName As String) mstrEnemyName = pstrNewName End Property
And then you would declare it like this
VB Code:
Dim objEnemy As CEnemy ' And instantiate it: Set objEnemy = New CEnemy ' Give it a name objEnemy.EnemyName = "Fred" ' Get its max hit points Msgbox objEnemy.EnemyName & "'[color=black]s maximum hit points is: " & objEnemy.MaxHP[/color] ' And when it needs to be killed off (from the program's point of view anyway) Set objEnemy = Nothing
HTH :)
can one of you write a tutorial in vb to show me so i can understand it ?
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.
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
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.
could i just use a regular module and do
VB Code:
public enum enemy dim hp as integer dim mp as integer dim name as string dim weapon as string dim strength as integer enemy.hp = "25" enemy.mp = "10" enemy.name = "Slime" enemy.weapon = "ooze" enemy.strength = "5"
Nope. If you tried, you would have gotten an error. From MSDN:
Quote:
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.
so that means what?? i'm confused
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.
i still dont get how to use them in a form
Take a look at Woka's class example:
one more quick question how would i get my arrays from a regular module.
Usually, I declare public arrays in a module
VB Code:
Dim MyArray() as String
and then in the form, redim it to give it a value
VB Code:
ReDim MyArray(4)
Then, any routine can assign or read values from MyArray()
can i like dim enemy(10) as string
enemy(0).name = "Slime"
enemy(0).hp = "25"