I looked and an example, and they used public constants to do something. What do they do? Can someone give me a link to a tutorial or explain it?
Printable View
I looked and an example, and they used public constants to do something. What do they do? Can someone give me a link to a tutorial or explain it?
See if this FAQ topic answers your question.
Edited: Just because you saw some code written a certain way does not necessarily mean it was correctly written, nor that it was necessary. Always keep that in mind.
Well. Sorta. I remeber there being something like
Public Wall
Public Const Width
Public Const Height
And stuff like that. Well, sorta like that.... Do you know of an example with making walls and stuff?
Oh w8! I remember now! It was Public Types! That was it! If i used thoes, how would i make it like:
Public Type Wall
lngX as Long
lngY as Long
lngWidth as Long
lngHeight as Long
End Type
How would i get collision, bitblt, and have more than one wall?
The Public/Private keywords just specify where something can be used, as explained by the FAQ article. In the same way you can have a Private Sub and a Public Sub, you can have a Private Type and a Public Type (and in both cases you can leave Public/Private out, and guess which one VB will use).
A Type is just a way of grouping multiple variables together - in that Wall example it is the various properties that are needed for a Wall (X/Y/Width/Height).
To use it you would declare a variable of that data type (Wall) rather than the usual ones (String/Integer/etc), and set the values. eg:
The alternative would be multiple variables with similar names, eg:Code:Dim MyWall as Wall
MyWall.lngX = 3
MyWall.lngY = 2
MyWall.lngWidth = 7
MyWall.lngHeight = 14
Using a Type means that you have fewer variables, and generally the code is easier to read/write (eg: at the . you get a drop-down list).Code:Dim MyWall_lngX as Long, MyWall_lngY as Long, MyWall_lngWidth as Long, MyWall_lngHeight as Long
MyWall_lngX = 3
MyWall_lngY = 2
MyWall_lngWidth = 7
MyWall_lngHeight = 14
Exactly the same as you would with individual variables.Quote:
How would i get collision, bitblt,
As with the usual data types, you use an array.Quote:
and have more than one wall?
Using an array of a user-defined-Type is much better than using array(s) of basic data types, because with basic data types you have two options:
- Use different arrays for each property (X/Y/Width/Height). Compared to a Type, this means more typing and more variables, and increased chances of problems (eg: you need to make sure each array has the same amount of items as the others, and that element 1 in one array refers to the same item as element 1 in the others).
- Use a multidimensional array, with one 'column' per property. Not only do you need to remember what each 'column' number means (eg: is 3 the Width?), but it is also very bad if the properties should have different data types (because in a multidimensional array everything has the same data type).
So there isn't really a plus to using types than just plain ol' variables?
:confused:
My post explained 3 pluses for using Types for non-array variables, and several more (which are much more significant) for array variables.
Oh. Ik that. rofl. im just thinking..... I guess it would be easier... What am i doing wrong though? Its not working and whenever i type the name of the object and the the . to get properties, it doesn't show them up like it should.....
Public Type Examples
Width as Long
Height as Long
Top as Long
Left as Long
End Type
????
That creates the Type, but why aren't you showing us the part that has problems - the code to use it? :confused:
As in my example in post #5, you should be declaring a variable of that type, and then using the variable.
Here is another example based on your latest Type:
Code:Public Type Examples
Width as Long
Height as Long
Top as Long
Left as Long
End Type
Private MyVar as Examples
...
MyVar.Width = 3
So.... you have to have something be as the Example? then whenever you type MyVar then the . all of the properties would show up in the window? (like width, height, left, top, etc.)
That is correct.
Yea, types/structs are extremely useful, but they take a while to get used to.
Okay. That makes sense. Thanks everyone!