Re: Problem with Public UDT: 'Only public user defined types......
neotechni,
It's really easy to convert a UDT into a Class. Let's say you have the following UDT:
Code:
Public Type MyUdtType
i1 As Long
i2 As Long
s1 As String
s2 As String
End Type
And then, somewhere in your code, you do this...
Code:
Dim MyUdt As MyUdtType
All you need to do is start a new class. We'll call it MyClass. And then take the elements of your UDT (cutting of the Public Type... and the End Type), and paste the into the top of your class, and declare them as Public, like so:
Code:
Public i1 As Long
Public i2 As Long
Public s1 As Long
Public s2 As Long
And then, where you originally declared a variable with your UDT, just do the following instead...
Code:
Dim MyUdt As New MyClass
Voila, MyUdt is now actually a class and not a true UDT, but it'll work exactly the same, with the added advantage that it'll pass into and out of other objects.
Regards,
Elroy
EDIT1: Another option is to leave it as a UDT, and then just make sure all procedures (in objects [forms and classes]) are declared as "Friend".
Re: Problem with Public UDT: 'Only public user defined types......
UDT is not class. For example:
Code:
Private Type tSomeType
a1 As Long
b2 As String
End Type
Dim z As tSomeType
Dim o As tSomeType
z.a1 = 5
o.b2 = "test"
z = o
In this case o will contains copy of all fields. If i change some members in o it doesn't affect to z. If i change UDT to a class then during accessing to o i'll change z members too because both references point to same object. Additionally you need to add Clone method too.
Re: Problem with Public UDT: 'Only public user defined types......
Hi Trick,
Yes, you're correct. Your "z = o" statement would not work quite as expected. You'd need to assign each member separately. But the concept is still there. And yes, a clone method might be nice.
Regards,
Elroy
Re: Problem with Public UDT: 'Only public user defined types......
hi , maybe it is too late but the answer is only add Friend Keyword before Sub or Function in class module and enjoy it
like this:
Friend Sub Test(ByRef m As MYTYPE)
End Sub
Re: Problem with Public UDT: 'Only public user defined types......
It can indeed also be done with a TLB definition, but not only does it have to be a typedef, it has to be a registered typedef with a unique uuid,
Code:
typedef [uuid(guid)] struct MyType
{
long var1,
long var2
} MyType;
And once you do that, you have to be more diligent about registration of the typelib... you can't just overwrite with an updated copy, you have to deregister, then overwrite, then re-register, then launch vb. I've not found it to be worthwhile.
Re: Problem with Public UDT: 'Only public user defined types......
Quote:
Originally Posted by
fafalone
I've not found it to be worthwhile.
I agree. At this point, nothing about my primary (very large) program requires registration or installation ... and that's inclusive of several ActiveX DLLs.
It'd sure be nice if we could figure out how to load these TypeLibs without registration, the same way we can load ActiveX DLLs without registration. I'm not even sure that'd help though if we're trying to use them with some third-party DLL to which we don't have the source code, and we're trying to pass one of these UDTs.
If we could load these TypeLibs (without registration), it would solve the problem of getting these UDTs into Variants and Collections, which would be nice. Also, the whole Public vs Friend problem would go away.
Re: Problem with Public UDT: 'Only public user defined types......
Quote:
Originally Posted by
Elroy
VB6 class modules allow a "Friend" declaration of variables and procedures (in addition to "Public" and "Private"). When a procedure is declared as "Friend", you can use UDTs as arguments. No need for all the above rigmarole. It's really that easy.
I had this very issue today, trying to pass a user-defined type array to a function (VB6) and hitting that long error message, but I remembered having used Friend to fix the problem years ago. I went back over my old programs and found it in half a dozen progs. From the preceding thread comments it seems not many people are aware of Friend!
Re: Problem with Public UDT: 'Only public user defined types......
Quote:
Originally Posted by
LittleTyke
I had this very issue today, trying to pass a user-defined type array to a function (VB6) and hitting that long error message, but I remembered having used Friend to fix the problem years ago. I went back over my old programs and found it in half a dozen progs. From the preceding thread comments it seems not many people are aware of Friend!
Microsoft really sort of confused things with the "scope" declaration of procedures in classes (FRM, CLS, CTL, PAG files). They did it to maintain some backward compatibility with older versions of VB and BASIC.
But, when used in a class, "Friend" is really analogous to the "Public" declaration when in a BAS module. That is, the scope is project wide, but not "exposed" to any outside process. However, when "Public" is used in a class, it is potentially exposed to other processes, and there's the catch.
When a procedure is potentially "exposed" to other processes (which isn't possible for BAS modules), and a UDT is used (either as an argument or the return), this must be done in such a way that the calling process "understands" that UDT ... and that's typically done with a TypeLib.
But, if we're willing to keep that UDT confined to our project (i.e., Friend declarations for classes, and Public declarations for BAS modules), then only our project needs to know about that UDT, and then the Public declaration of the UDT in a BAS module works perfectly.
Because of all of this, I have used (pretty much) only the "Friend" declaration (or Private) in class modules for years ... as again, it's equivalent to "Public" in a BAS module.
Also, if people don't know this stuff, they haven't been paying attention. Also, this is all well outlined in the help system (both VB6 F1 and online).
Re: [RESOLVED] Problem with Public UDT: 'Only public user defined types......
I'm recommending a free utility: Earlier in this thread I remarked that I had gone back over my old programs to find where I had used the Friend keyword. To do this I used AstroGrep, which is a brilliant program to search a folder/subfolder tree on Windows. I used it on Windows XP, but it probably works for all recent versions of Windows, too. The link is https://astrogrep.sourceforge.net/