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?
Last edited by krtxmrtz; Jun 28th, 2007 at 05:16 PM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button Wait, I'm too old to hurry!
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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
No kidding !!!???
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'.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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 sub
Code:
MyName() As MyType
the usual way of declaring an array. The caller has to pass the sub an array too.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read. Please Help Us To Save Ana
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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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.
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?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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?"
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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!)
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
But you declared your variable of your type as a single element, then used it as an array.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read. Please Help Us To Save Ana
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
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?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
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
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
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
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?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs
Originally Posted by leinad31
If you have MSDN Library installed, look for "House of Straw"
I'll take a look at it. Thanks.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)