-
1 Attachment(s)
[RESOLVED] Problem when passing UDTs to subs
In a module I have the following declarations:
Code:
Public Type MyUDT
X As Single
Y() As Single
End Type
'
Public MyUDTInstance() As MyUDT
In a different module:
Code:
Sub MySub(DummyVarName)
'...
' Some operations are performed here
'with the quantities:
'DummyVarName(SomeIndex).X
'and
'DummyVarName(SomeIndex).Y(SomeOtherIndex)
'...
End Sub
Finally, in a form there's the calling statement:
Code:
Call MySub(MyUDTInstance)
Of course, MyUDTInstance and MyUDTInstance(SomeIndex).Y(SomeOtherIndex) have been previously ReDimensioned and given values.
Now, when the sub is called I get the error you can see in the attached picture which can be translated into English more or less as follows:
"Compiler error.
Only those user defined types from public obejct modules can be passed to functions linked at run time or forced to or from a variant"
Anyone knows the reason?
-
Re: Problem when passing UDTs to subs
try
Code:
Sub MySub(DummyVarName as MyUDT)
...
End Sub
-
1 Attachment(s)
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by robbedaya
try
Code:
Sub MySub(DummyVarName as MyUDT)
...
End Sub
Now I get
"Compiler error.
The type of argument of ByRef does not coincide"
-
Re: Problem when passing UDTs to subs
I've doing something like that last night (..when trying first steps in VB2005).
Didn't get any errors there?
Since your UDT is Public, did you try to declare the Sub without the handing over the UDT like:
Code:
Sub MySub()
..
End Sub
-
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by opus
I've doing something like that last night (..when trying first steps in VB2005).
Didn't get any errors there?
Since your UDT is Public, did you try to declare the Sub without the handing over the UDT like:
Code:
Sub MySub()
..
End Sub
OK, I've tried and it works. But I'd like to know what was wrong, I'd like my sub to be as general as possible.
-
Re: Problem when passing UDTs to subs
All right, after trying out various possibilities I've found the correct syntax that won't fire any error. This is it:
Code:
Public Type MyUDT
X As Single
Y() As Single
End Type
'
Public MyUDTInstance() As MyUDT
In a different module:
Code:
Sub MySub(DummyVarName() As MyUDT)
'...
'
End Sub
The calling statement in a form:
Code:
Call MySub(MyUDTInstance())
I hope some guru sets his/her eyes on this thread and provides a clear explanation that makes sense.
-
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by krtxmrtz
I hope some guru sets his/her eyes on this thread and provides a clear explanation that makes sense.
:confused: I always figured you as a guru, now I'm lost:confused:
-
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by opus
:confused: I always figured you as a guru, now I'm lost:confused:
No kidding !!!??? :lol:
Programming is just a hobby for me, I don't make a living out of it, I make programs for my own use and that of my workmates'.
-
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by krtxmrtz
All right, after trying out various possibilities I've found the correct syntax that won't fire any error. This is it:
Code:
Public Type MyUDT
X As Single
Y() As Single
End Type
'
Public MyUDTInstance() As MyUDT
In a different module:
Code:
Sub MySub(DummyVarName() As MyUDT)
'...
'
End Sub
The calling statement in a form:
Code:
Call MySub(MyUDTInstance())
I hope some guru sets his/her eyes on this thread and provides a clear explanation that makes sense.
MyUDT is not an array type. Your sub is using it as an array, so you have to declare it as an array in the subthe usual way of declaring an array. The caller has to pass the sub an array too.
-
Re: [RESOLVED] Problem when passing UDTs to subs
About your last code, you don't need to send MyUDTInstance() as parameter, since its scope is the whole project, it can be used anywhere.
but if you want to send it as parameter, something like this would be better..
Module:
Code:
Public Type MyUDT
X As Single
Y() As Single
End Type
Public Sub MySub(DummyVarName() As MyUDT)
'So something here
End Sub
Form:
Code:
Dim lUdt() As MyUDT
'Do something with lUdt
Call MySub(lUdt())
-
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by krtxmrtz
OK, I've tried and it works. But I'd like to know what was wrong, I'd like my sub to be as general as possible.
Then use classes/collections rather than UDTs, and receive it As Object.
-
Re: [RESOLVED] Problem when passing UDTs to subs
Quote:
Originally Posted by jcis
About your last code, you don't need to send MyUDTInstance() as parameter, since its scope is the whole project, it can be used anywhere.
...
Actually MyUDT was declared initially in the form, but then I started fooling around with the code to test the various possibilities and it ended up in the same module as the type declaration.
But thanks for the clarification anyway. If anything at least it's a reminder for me to restore the previous situation.
-
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by Al42
MyUDT is not an array type....
Is there such a thing as an array type, like
Type Whatever()
'...
'...
End Type
???
-
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by leinad31
Then use classes/collections rather than UDTs, and receive it As Object.
Believe it or not I have never used classes (of my own) just because I've never found the time to learn. All the VB I know I have learned either on my own or -to a large extent- in the forums. But I tend to learn new things only when I really need them and clearly see the benefit. And I don't see the benefit of using classes just because I'm hardly acquainted with them. Maybe I'd need a simple template to start with and learn gradually from that. Any hint, suggestion, tutorial, link in that direction will be most welcome.
-
Re: [RESOLVED] Problem when passing UDTs to subs
Actually the simplest class is a pure data class. You would define the elenments the same way you would in a module but you could reference them differently.
IE.
Code:
In a Class Module names clsData
Public sStr as String
Public lngOffset as Long
That's it!!!
In your program you would use it like this:
Code:
Dim myClass as clsData
Set myClass = New clsData
myClass.lngOffset = 5677
myClass.sStr = "Test"
That's it. You can pass myClass as an object and place it into arrays and collections and create as may to hold the data as you need. In my opinion this is more flexible that using a UDT.
-
Re: [RESOLVED] Problem when passing UDTs to subs
Quote:
Originally Posted by randem
Actually the simplest class is a pure data class. You would define the elenments the same way you would in a module but you could reference them differently.
IE.
Code:
In a Class Module names clsData
Public sStr as String
Public lngOffset as Long
That's it!!!
In your program you would use it like this:
Code:
Dim myClass as clsData
Set myClass = New clsData
myClass.lngOffset = 5677
myClass.sStr = "Test"
That's it. You can pass myClass as an object and place it into arrays and collections and create as may to hold the data as you need. In my opinion this is more flexible that using a UDT.
Thanks but I still don't see the advantage:
1. In which sense is it more flexible?
2. Can I make an array such as "Set myClass() = New clsData" and ReDim it at a later stage?
3. What do you exactly mean by passing it as an aobject? (To subs and functions?)
4. Do I have to destroy the class instance at program exit or at any other point in the program?
-
Re: [RESOLVED] Problem when passing UDTs to subs
1 - In every use
2 - No use dim myClass() as clsData, then redim it
3 - Since myClass is an object now, you can pass it like this.
Code:
Sub MySub(ArrayName() As Object)
'...
'
End Sub
This way you are passing an array of classes.
I believe you can even do this
Code:
Sub MySub(ArrayName() As clsData)
'...
'
End Sub
-
Re: [RESOLVED] Problem when passing UDTs to subs
You can also add it to a collection
Code:
colData.Add myClass, myClass.sStr ' or whatever key you would need
-
Re: [RESOLVED] Problem when passing UDTs to subs
Quote:
Originally Posted by randem
...
You sound very convincing...
How about my fourth?
"Do I have to destroy the class instance at program exit or at any other point in the program?"
-
Re: [RESOLVED] Problem when passing UDTs to subs
4 - Yes... Preferable when you are done with it.
Sorry.
-
Re: [RESOLVED] Problem when passing UDTs to subs
You can also code the class to enumerate thru the variables if you want. You can't do that with a UDT!
-
Re: [RESOLVED] Problem when passing UDTs to subs
Quote:
Originally Posted by randem
You can also code the class to enumerate thru the variables if you want. You can't do that with a UDT!
For example...? (Never done that!)
-
Re: [RESOLVED] Problem when passing UDTs to subs
You would then add a Emum function to your class that would return each data element name (type, length, etc...) from the class.
-
Re: [RESOLVED] Problem when passing UDTs to subs
Quote:
Originally Posted by randem
You would then add a Emum function to your class that would return each data element name (type, length, etc...) from the class.
I see. In your example above,
Code:
In a Class Module names clsData
Public sStr as String
Public lngOffset as Long
How would that work?
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
As far as I know Enum provides a useful way to remind you of constant names like intellisense. How can it be used to get the data information, as you say, like length, type, etc?
-
Re: [RESOLVED] Problem when passing UDTs to subs
I have tried on my own and had to quit right after I'd started for it does not allow me to declare the variables below as public (produces an error)
Code:
In a Class Module names clsData
Public sStr as String
Public lngOffset as Long
-
Re: Problem when passing UDTs to subs
Quote:
Originally Posted by krtxmrtz
Is there such a thing as an array type, like
Type Whatever()
'...
'...
End Type
???
Actually, no, it's an array of type.
Type Whatever
'...
'...
End Type
Dim myWhatever() as Whatever
But you declared your variable of your type as a single element, then used it as an array.
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
Code:
In a Class Module names clsData
This is a statement not code...
Sure it works, I use Pure Data Classes extensively.
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
Quote:
Originally Posted by randem
Code:
In a Class Module names clsData
This is a statement not code...
Sure it works, I use Pure Data Classes extensively.
All right, it works with simple data definitions, but not with UDTs:
Code:
Option Explicit
Public Start As Single
Public Finish As Single
Public Step As Single
Public Kounter As Long
Public Type Animal
Size As Single
CanFly As Boolean
CanSwim As Boolean
NumberOfLegs As Integer
End Type
It gives an error when run so, looks like it doesn't allow UDTs here. Shall I then dismiss using classes?
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
The point of using classes is to drop the use of UDTs (eg. such is the case with Java)
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
Quote:
Originally Posted by leinad31
The point of using classes is to drop the use of UDTs (eg. such is the case with Java)
Then is it possible to implement the following using classes?
VB Code:
Type Series
nPts As Integer
Nam As String
X() As Single
Y() As Single
End Type
Type DataChart
ID As String
nSeries As Integer
S() As Series
End Type
Public DC() As DataChart
Public nDDCs As Integer
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
Yes. You simply have to look around to see samples.
If I'm not mistaken you can retain the array of primitive data types (like the SubItems() property of listview listitem).
The array of UDT you will implement as a collection of objects (like the ListItems collection of listview). You can have a heirarchy (such as each ListItem in ListItems collection having ListSubItems collection).
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
Quote:
Originally Posted by leinad31
Yes. You simply have to look around to see samples.
If I'm not mistaken you can retain the array of primitive data types (like the SubItems() property of listview listitem).
The array of UDT you will implement as a collection of objects (like the ListItems collection of listview). You can have a heirarchy (such as each ListItem in ListItems collection having ListSubItems collection).
Great, but I've never worked with collections of my own. Could you provide a couple of lines to show how you'd declare/define a suitable collection for the example I've presented in my previous post?
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
If you have MSDN Library installed, look for "House of Straw"
-
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
Quote:
Originally Posted by leinad31
If you have MSDN Library installed, look for "House of Straw"
I'll take a look at it. Thanks.