Prev: Temp File -- got me stumped
Next: Cleanup remnants
From: Larry Serflaten on 7 Aug 2010 15:17 "mp" <nospam(a)Thanks.com> wrote > initially i thought i'd try a udt since there were only 3 properties and I > thought since it was all private in the same class i wouldn't get the > private/public compile error i get whenever I try to (mis-)use a udt. If you can keep it all private, you should not see a problem. EG: A UDT declared as Private in a class can be passed to Private routines within the class. > i have had almost no luck trying to use a simple udt if it has to be passed > anywhere due to all the strange(to me) limitations (really the limitations > of my knowledge) > it's just always been easier to create a class instead than try to figure > out what hoops to jump through to use a simpler(i would have thought) > construct. I find classes are the easier of the two as well. With a class you get to include a (sort of) constructor, (more like initialization...) and can control the Read/Write access of the variables. Classes can be passed hither and yon, and you have the otpion to use an array or collection, whichever fits the need. LFS
From: Tom Shelton on 7 Aug 2010 20:37 After serious thinking ralph wrote : > On Sat, 7 Aug 2010 10:45:21 -0500, "mp" <nospam(a)Thanks.com> wrote: > >> more an observation than a question, but I welcome any discussion that >> follows.... >> >> seems strange i can't define a private udt inside a private class and pass >> it to private/or public proceedures inside the class itself. >> >> compile error >> Only user-defined types defined in public object modules can be coerced to >> or from a variant or passed to late-bound functions >> >> the error occurs because i tried to add a udt to a collection(thus casting >> to variant) >> 'Private Type tDims >> ' DimStartPoint As Variant >> ' DimEndPoint As Variant >> ' DimTextOverride As String >> 'End Type >> 'Private Sub AddToDimsCollection(otDim As tDims) >> mColDims.Add otDim >> > > You are sort of hammering on two separate problems. > To answer the last one first - you can not store a UDT in a VB > Collection. > Yes you can. It's all about how you define it - it needs to be in a typelib. And the way you do that? You define it in a public class module in an activex dll or exe. ' in a class1.cls in an activex dll (Instancing = MultiUse) Public Type myType1 i As Integer End Type Public Sub Bar(ByRef z As myType1) MsgBox z.i End Sub ' form1 of a standard exe - referencing the above: Private Sub Form_Load() Dim coll As Collection Dim st As myType1 Set coll = New Collection st.i = 10 coll.Add st MsgBox coll(1).i End Sub > One way to get around that is to write your own Collection based on an > Array. You can store UDTs in VB Arrays. > I generally did that anyway. The collection object is sort of a hog... > There are several examples on the web. Here is one to get you started. > http://www.mvps.org/vbvision/collections.htm > > As for the first question - you need to declare/define the UDT in a > Public Module. That means using a Public Base module, or Friend, or > writing a Type Library for the UDT (the latter essentially serves as a > "Public Module". > > Or you might consider combining your custom collection with an Object > in an ActiveX Dll which you could use with your project. > http://support.microsoft.com/default.aspx?scid=kb;EN-US;q185700 If you define your type in a activex dll, then you can use the regular collection object if you like. I prefer a custom collection usually because I could make it faster anyway... -- Tom Shelton
From: Kevin Provance on 7 Aug 2010 20:48 : : There are several examples on the web. Here is one to get you started. : http://www.mvps.org/vbvision/collections.htm Bryan, um...Spafford? There was a guy who knew his stuff. Ken Halter too. Two names I miss around these parts.
From: ralph on 8 Aug 2010 01:19 On Sat, 07 Aug 2010 18:37:22 -0600, Tom Shelton <tom_shelton(a)comcast.invalid> wrote: >After serious thinking ralph wrote : >> On Sat, 7 Aug 2010 10:45:21 -0500, "mp" <nospam(a)Thanks.com> wrote: >> >>> more an observation than a question, but I welcome any discussion that >>> follows.... >>> >>> seems strange i can't define a private udt inside a private class and pass >>> it to private/or public proceedures inside the class itself. >>> >>> compile error >>> Only user-defined types defined in public object modules can be coerced to >>> or from a variant or passed to late-bound functions >>> >>> the error occurs because i tried to add a udt to a collection(thus casting >>> to variant) >>> 'Private Type tDims >>> ' DimStartPoint As Variant >>> ' DimEndPoint As Variant >>> ' DimTextOverride As String >>> 'End Type >>> 'Private Sub AddToDimsCollection(otDim As tDims) >>> mColDims.Add otDim >>> >> >> You are sort of hammering on two separate problems. >> To answer the last one first - you can not store a UDT in a VB >> Collection. >> > >Yes you can. Not really. Or rather not in the way the OP was trying to do it as demostrated or noted in the title. The OP made it known he already knew where the problem was - "the error is still trying to put the udt into a collection" and that is because (like the error says) You can't coerce a UDT to Variant unless it is defined in a Public Object, therefore he couldn't store the UDT in a VB Collection. I expanded on that, but went back and deleted it to make the post smaller, also unfortunately destroying the sense of what I was trying to say. > ... It's all about how you define it - it needs to be in a >typelib. And the way you do that? You define it in a public class >module in an activex dll or exe. > Ta Dah! -ralph
From: Larry Serflaten on 8 Aug 2010 03:30
"ralph" <nt_consulting64(a)yahoo.net> wrote > > ... It's all about how you define it - it needs to be in a > >typelib. And the way you do that? You define it in a public class > >module in an activex dll or exe. > > > > Ta Dah! .... more Registry refuse.... <g> LFS |