|
-
Jul 21st, 2003, 09:52 AM
#1
Thread Starter
Hyperactive Member
User Defined type in Class Function
I write the following testing code for checking how I can pass user defined type in function running well with in module. But when I made the same the function in class it
Not works? Give me the Error
Only Public User Defined Types Defined In Public Object Module Can Be Used As Parameters Or Return Types For Public Procedures of Class Modules Or as Fields of public User Defined Types
How can I make the same function in Class?
VB Code:
Public Type MyType ' In Module
First As String
Second As Long
End Type
Public Function TestType(ByRef TypeIN() As MyType) 'In Module
Dim A As Integer
For A = 1 To 2
MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
TypeIN(A).First = "Test" & A 'Set Data
TypeIN(A).Second = A 'Set Data
Next A
End Function
Private Sub Command1_Click()
Dim TestingType(1 To 2) As MyType
TestingType(1).First = "First A" ' Set Data
TestingType(1).Second = 1 'Set Data
TestingType(2).First = "First B"
TestingType(2).Second = 2
Call TestType(TestingType())
MsgBox TestingType(1).First & TestingType(1).Second ' Get Data
MsgBox TestingType(2).First & TestingType(2).Second ' Get Data
End Sub
-
Jul 21st, 2003, 10:12 AM
#2
Addicted Member
Try changing
VB Code:
Public Function TestType(ByRef TypeIN() As MyType) 'In Module
Dim A As Integer
For A = 1 To 2
MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
TypeIN(A).First = "Test" & A 'Set Data
TypeIN(A).Second = A 'Set Data
Next A
End Function
To
VB Code:
Public Function TestType(ByVal TypeIN() As Variant) 'In Module
Dim A As Integer
For A = 1 To 2
MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
TypeIN(A).First = "Test" & A 'Set Data
TypeIN(A).Second = A 'Set Data
Next A
End Function
See MSDN KB Article: Q185700 for more information.
-
Jul 21st, 2003, 10:22 AM
#3
Not NoteMe
Re: User Defined type in Class Function
Originally posted by VB IT
Only Public User Defined Types Defined In Public Object Module Can Be Used As Parameters Or Return Types For Public Procedures of Class Modules Or as Fields of public User Defined Types
What this means, is that in your class module you defigned a custom type. Because the class module is the only thing that can 'see' that type it causes an error.
Keep the definition of your custom type in a module.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Jul 21st, 2003, 10:26 AM
#4
Thread Starter
Hyperactive Member
Err
Originally posted by rlwhealdon
Try changing
VB Code:
Public Function TestType(ByVal TypeIN() As Variant) 'In Module
Dim A As Integer
For A = 1 To 2
MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
TypeIN(A).First = "Test" & A 'Set Data
TypeIN(A).Second = A 'Set Data
Next A
End Function
I Get The Error
Type Mismach Array or user Defined Type Expected
-
Jul 21st, 2003, 10:53 AM
#5
Remove all the code from ya module and paste it into a class module, EXCEPT:
VB Code:
Public Type MyType ' In Module
First As String
Second As Long
End Type
Leave that in the module on it's own...
Woka
-
Jul 21st, 2003, 10:53 AM
#6
Addicted Member
Okay, here's how I got it to work.
VB Code:
' Module1.bas
Option Explicit
Public Type MyType ' In Module
First As String
Second As Long
End Type
Public Function TestType(ByRef TypeIN() As MyType) 'In Module
Dim A As Integer
For A = 1 To 2
MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
TypeIN(A).First = "Test" & A 'Set Data
TypeIN(A).Second = A 'Set Data
Next A
End Function
VB Code:
' Form1.frm
Option Explicit
Private Sub Command1_Click()
Dim TestingType(1 To 2) As MyType
TestingType(1).First = "First A" ' Set Data
TestingType(1).Second = 1 'Set Data
TestingType(2).First = "First B"
TestingType(2).Second = 2
TestType TestingType()
MsgBox TestingType(1).First & TestingType(1).Second ' Get Data
MsgBox TestingType(2).First & TestingType(2).Second ' Get Data
End Sub
-
Jul 21st, 2003, 11:07 AM
#7
Not NoteMe
Originally posted by Wokawidget
Remove all the code from ya module and paste it into a class module, EXCEPT:
VB Code:
Public Type MyType ' In Module
First As String
Second As Long
End Type
Leave that in the module on it's own...
Woka
That sounds familiar...
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Jul 21st, 2003, 12:47 PM
#8
You can also do this. Create a class called Class1 that looks like
VB Code:
Option Explicit
Public First As String
Public Second As Long
Then slightly change your code to
VB Code:
Option Explicit
Public Function TestType(ByRef TypeIN() As Class1) 'In Module
Dim A As Integer
For A = 1 To 2
MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
TypeIN(A).First = "Test" & A 'Set Data
TypeIN(A).Second = A 'Set Data
Next A
End Function
Private Sub Command1_Click()
Dim TestingType(1 To 2) As New Class1
TestingType(1).First = "First A" ' Set Data
TestingType(1).Second = 1 'Set Data
TestingType(2).First = "First B"
TestingType(2).Second = 2
Call TestType(TestingType())
MsgBox TestingType(1).First & TestingType(1).Second ' Get Data
MsgBox TestingType(2).First & TestingType(2).Second ' Get Data
End Sub
-
Jul 21st, 2003, 01:48 PM
#9
A bit over kill isn't it?
...and Public varibles in a class...
Woka
-
Jul 21st, 2003, 11:11 PM
#10
Thread Starter
Hyperactive Member
Thanks!
Originally posted by Wokawidget
Remove all the code from ya module and paste it into a class module, EXCEPT:
VB Code:
Public Type MyType ' In Module
First As String
Second As Long
End Type
Leave that in the module on it's own...
Woka
First Thanks to all For There Kind Replies
Wokawidget, I Get the Same Error By using Your method, may be I don’t understand it, here is the code.
Error:
Only Public User Defined Types Defined In Public Object Module Can Be Used As Parameters Or Return Types For Public Procedures of Class Modules Or as Fields of public User Defined Types.
VB Code:
'In Module
Public Type MyType
First As String
Second As Long
End Type
'Class1
Public Function TestType(ByRef TypeIN() As MyType)
Dim A As Integer
For A = 1 To 2
MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
TypeIN(A).First = "Test" & A 'Set Data
TypeIN(A).Second = A 'Set Data
Next A
End Function
'From1.Frm
Private Sub Command1_Click()
Dim TestingType(1 To 2) As MyType
Dim C As New Class1
TestingType(1).First = "First A" ' Set Data
TestingType(1).Second = 1 'Set Data
TestingType(2).First = "First B"
TestingType(2).Second = 2
Call C.TestType(TestingType())
MsgBox TestingType(1).First & TestingType(1).Second ' Get Data
MsgBox TestingType(2).First & TestingType(2).Second ' Get Data
End Sub
-
Jul 22nd, 2003, 03:06 AM
#11
Errrr...Hmmmm...that should work...I think 
Woka
PS: It should be "Sorry for my bad English"
-
Jul 22nd, 2003, 03:52 AM
#12
Thread Starter
Hyperactive Member
-
Jul 22nd, 2003, 08:56 AM
#13
Re: Exapmle!
Originally posted by VB IT
Can You Please Give The Working Example
Mine works
-
Jul 22nd, 2003, 09:00 AM
#14
Stop gloating 
Mine doesn't 
But why?!
Woka
-
Jul 22nd, 2003, 09:36 AM
#15
VB IT, Wokawidget objected to the Public variables in my class. You can of course do it the traditional way.
VB Code:
Option Explicit
Private mstrFirst As String
Private mlngSecond As Long
Public Property Get First() As String
First = mstrFirst
End Property
Public Property Let First(ByVal strFirst As String)
mstrFirst = strFirst
End Property
Public Property Get Second() As Long
Second = mlngSecond
End Property
Public Property Let Second(ByVal strSecond As Long)
mlngSecond = strSecond
End Property
-
Jul 22nd, 2003, 10:21 AM
#16
-
Jul 22nd, 2003, 11:29 PM
#17
Thread Starter
Hyperactive Member
Re: Re: Exapmle!
Originally posted by MartinLiss
Mine works
Thanks for Your Kind Reply
Your Example works but a little bit more complex as compare to pass user defined type but end result is same. You right it is the other way to make the same result. But I want to clear my mind
I can’t Pass User Define Type In Class Function!?
-
Jul 22nd, 2003, 11:40 PM
#18
I don't think you can. Do a search of the General VB Coding Questions forum for user AND type AND class. There are a number of threads on the subject.
-
Jul 23rd, 2003, 03:13 AM
#19
Thread Starter
Hyperactive Member
Originally posted by MartinLiss
I don't think you can. Do a search of the General VB Coding Questions forum for user AND type AND class. There are a number of threads on the subject.
Thank you for You Kind Reply
After reading Tread I Think I Can’t Pass UDT (User Define Type) In Class Function.
-
Jul 23rd, 2003, 03:47 PM
#20
VB IT
No, you can not get there from here!!!. There are numerous other way to attempt to achieve this but I have found NONE of them reliable in all cases and only asking for trouble.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|