Hi,
are there structures in VB?how can i create one if it is availbale?i need to have 2 integer varaible and a string variable inside a structure.......and create a structure array....is it possible?
Thanks and Regards
vivek.s
Printable View
Hi,
are there structures in VB?how can i create one if it is availbale?i need to have 2 integer varaible and a string variable inside a structure.......and create a structure array....is it possible?
Thanks and Regards
vivek.s
Type NAME_OF_TYPE
int1 as integer
int2 as integer
str1 as string
End Type
array(6, 6, 6) as NAME_OF_TYPE
Thanks a lot!
Regards
vivek.s
Before you get too far into your program consider if you'll need to pass the structure to a Sub or Function. If the answer is yes then you'll need to use a class instead.
Why? There is no reason why you can't pass a user defined type to a sub or function!?Quote:
Originally Posted by MartinLiss
I forgot the technical reason, but you get an error if you try.Quote:
Originally Posted by Blade
Quote:
Compile error:
Private Enum and user defined types cannot be used as parameters or return types for public procedures, public data members, or fields of public user defined types
You just can't. Although it IS possible to convert it to bytes, send to bytes to the sub/function, then convert it back to a UDT.
Or just use a class, which is easier if you use the Class Builder add-in. (look into your add-in's for that).
Try it - sadly you get a compile error. You will probably be as dissapointed as I was when I found that out.
The quick and dirty get around is to define your type as public in a module making it global n scope. That gets messy when you have a few of them dotted around though
The slicker get around is as Marty says, use a class. It's not as messy as you would think. Consider this code example. Throwing a few public variables into a class eg ..
Option Explicit
and because variables declared as Public in a class are visable at the interface, you have your portable type. But now BECAUSE it is in a class you can do all of the other wizzy class things. You can addd subs and functions. So all of a sudden you type is on setroids.Code:
Option Explicit
GRID 1 VARIABLES
' This is the grid is the main descriptor grid.
Public Assessment As Long
Public AssCalculated As Long the descriptors
Public Subject As String
Public YG As String
Public UPN As String
Public Forename As String
Public Surname As String
Public aAssessInfo As Variant
I personaly found that my class Type quickly became the container for all of the variables I needed in one code area. This ringfenced my code and made it very portable.
There was one downside though. You can't use this approach to expose Arrays directly. You have to get at them though methods. So it is a type replacement but not an exact replacememt.
HTH
.
oops i was doin it the messy way........i declared a global structure and since i am using only 2 functions i used that......maybe i shud condier using class.....but unfortunatley i havnt dwelled into object oriented features of VB.......
regards
vivek.s
It all depends on how far you want to take your coding. For me object oriented programming is nervanah. I don't think I will ever have the luxury of getting there. I program, small to medium apps for myself and have no need to share modules. Hence true OO is not my personal goal.
I have recently found classes to be a great way of
1. Containing local code variables / routines (as above)
2. Wrapping groups of similar soubroutines and functions.
3. Holding all of the business logic and providing a wrapper for a database.
In non of these cases was I codeing pure OO. I always have the odd variable that I have wandering about globally, but the code structure reflects how I think and so it is easy for me to maintain.
In short, Classes allow me to pidgeonhole code.
In terms of portability, within code I can drag and drop classes between projects. That is good enough for me.
If you want to know more from a class nebies perspective (which is definitely me) post again or ping me direct. Good luck
.