DoobieKeebler Moderator

Joined: 17 Jun 2005 Posts: 254 Location: 181°15'2.003"W, 93°5'16.956"N
|
Posted: Nov 17th, 2005 02:22 PM Post subject: FAQs For User-Defined Types |
|
|
Q1) What is a User-Defined Type?
A) A User-Defined Type (UDT) is a variable that combines two or more variables. I find it easiest to think of it as an object.
Q2) How do you create a user-defined type?
A) | Code: | Private Type {typename}
{element} As {variable type}
{element} As {variable type}
{element} As {variable type}
End Type |
Q3) Where do I put that?
A) In the General declarations section of a form or a module.
Q4) Can user-defined types only be Private?
A) That depends on where you declare them. If you declare them in a form then they can only be Private. If you declare them in a standard module or a class module then they can be either Private or Public.
Q5) Can you show me an example of a user-defined type?
A) Sure. Here's a UDT for a player in a game.
| Code: | Private Type Player
Name As String
Level As Integer
Points As Integer
Cash As Currency
IsInvincible As Boolean
End Type |
Q6) Are there any restrictions on what I can name the type?
A) Yes. Since a UDT is a variable, you have to follow the same rules as when you name any variable.
Q7) Are there any restrictions on what I can name the elements in a user-defined type?
A) They can't start with a number, but otherwise there don't seem to be any restrictions. You can even use reserved words, like Long or Type.
Q8) What kind of variable types can I use for the elements?
A) Anything, including objects (like Forms) and even other user-defined types.
Q9) When I create a user-defined type, do I just go ahead and use it?
A) No. What you've created is a type of variable, just like a Long or a String. You need to create an actual variable of that user-defined type. In other words, you have to say something like:
| Code: | | Dim udtHero As Player |
I use "udt" to remind myself that Player is a user-defined type.
Q10) How do I set or read the values of the elements? For example, if I want to set Name for udtHero to "Aragorn", how do I do that?
A) You use the Variable.Element notation. So in your example you'd say:
| Code: | | udtHero.Name = "Aragorn" |
To display the player's level you could say;
| Code: | | Label1.Caption = "Level: " & udtHero.Level |
Q11) If I have two Integers, Int1 and Int2, I can say Int2 = Int1. Can I do that with user-defined types?
A) Sure. If you have two Players, udtHero and udtClone, you can say udtClone = udtHero. Every element of udtClone is set to the same value as the corresponding element of udtHero. In other words, udtClone.Points is now the same value as udtHero.Points.
Q12) But what if I only want to make one element of udtClone equal to the same element of udtHero?
A) Then you'd say:
| Code: | | udtClone.Cash = udtHero.Cash |
Q13) Can one or more of the parameters in a Sub or Function be a user-defined type?
A) Yes, but you have to pass it ByRef instead of ByVal, like so:
| Code: | | Private Sub LevelUp(Character As Player) |
Q14) What if I have 4 players in the game? Can I have an array of type Player?
A) Yes. Your Dim statement would be:
| Code: | | Dim GoodGuys(0 To 3) As Player |
Q15) So how would I make the first player's Name be "Frodo"?
A) GoodGuy(0).Name = "Frodo"
Q16) Can any of the elements of a user-defined type be arrays?
A) Yes. The following code shows how you can have up to 10 Nicknames for any Player:
| Code: | Private Type Player
Name As String
Nicknames (0 To 9) As String
Level As Integer
Points As Integer
Cash As Currency
IsInvincible As Boolean
End Type |
Q17) Does Nicknames have to be a fixed array or can it be dynamic?
A) It can be a dynamic array. Just change it to:
| Code: | | Nicknames() As String |
Q18) How do I set the third nickname of udtHero to "Smiley?"
A) You'd say:
| Code: | | udtHero.Nicknames(2) = "Smiley" |
Q19) If Nicknames is an array, can I also have an array of players, like in Q14?
A) Yes. Anything anywhere can be an array.
Q20) So how do I set the fifth nickname of the third player to "Butch"?
A) You simply combine the answer to Q15 with the answer to Q18 and say:
| Code: | | GoodGuy(2).Nicknames(4) = "Butch" |
Q21) Back in Q8 you said that the variable type for an element can be a user-defined type. How does that work?
A) You have to create the first user-defined type before you can use it as the variable type of a second UDT. Here's an example:
| Code: | Private Type Weapon
Name As String
Cost As Currency
End Type
Private Type Player
Name As String
Nicknames(0 To 9) as String
Level As Integer
Points As Integer
Cash As Currency
ArmedWeapon As Weapon
IsInvincible As Boolean
End Type |
In the user-defined type Player the variable type for ArmedWeapon is the user-defined type Weapon that we created first.
Q22) So how do I set anything for ArmedWeapon?
A) "ArmedWeapon As Weapon" is really just "Dim ArmedWeapon As Weapon". So you now have ArmedWeapon.Type and ArmedWeapon.Cost. We can say:
| Code: | udtHero.ArmedWeapon.Type = "Sword"
udtHero.ArmedWeapon.Cost = 100 |
Q23) But since the variable type for Player.ArmedWeapon is a user-defined type, each of my players can only have one weapon, right?
A) No. You can make ArmedWeapon an array, either fixed or dynamic. Just create it like this:
| Code: | Private Type Player
Name As String
Nicknames(0 To 9) As String
Level As Integer
Points As Integer
Cash As Currency
ArmedWeapon(0 To 3) As Weapon
IsInvincible As Boolean
End Type |
Q24) How do I set the name of the second weapon for udtHero?
A) udtHero.ArmedWeapon(1).Name = "Bow"
Q25) How do I set the name of the third weapon for the fourth player?
A) GoodGuy(3).ArmedWeapon(2).Name = "Crossbow"
Q26) If I use Weapon as the variable type for an elements of Player, can an element of Weapon be an array?
A) Yes. You have to pay close attention to all those indexes though.
Q27) Can the variable type of an element in the user-defined type Weapon be a user-defined type?
A) Yes. Be sure to define a user-defined type before you use it as the variable type of an element in another UDT. The Types might look like this:
| Code: | Type Creator
Name As String
City As String
End Type
Private Type Weapon
Name As String
Cost As Currency
Maker As Creator
End Type
Private Type Player
Name As String
Nicknames(0 To 9) As String
Level As Integer
Points As Integer
Cash As Currency
ArmedWeapon(0 To 3) As Weapon
IsInvincible As Boolean
End Type |
Q28) And that third user-defined element can have an element that's an array, right?
A) Yes. Of course, you'll start going crazy keeping all those index straight. But you certainly can end up with something as complicated as:
| Code: | | GoodGuy(2).Weapon(1).CoatOfArms(3).Panel(1).Symbol(0).ForeColor |
Q29) Creator, Weapon, and Player all have an element named Name. How will VB know which one I mean?
A) Let's say that udtHero is a Player, udtExcaliber is a Weapon, and udtInigoMontoya is a Creator. You actually have the following items, each of which is distinct:
| Code: | udtHero.Name
udtHero.ArmedWeapon(0).Name
udtHero.ArmedWeapon(0).Maker.Name
udtExcaliber.Name
udtExcaliber.Maker.Name
udtInigoMontoya.Name |
_________________ Always take into account what a user would never ever in a million years do, because someone will.
"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra |
|