Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile error help for a young Visual Basic programmer

Status
Not open for further replies.

ildenizen

Programmer
Feb 24, 2005
12
US
Good evening,

I just got the following error message:

Compile Error:
Only user-defined types defined in a public object module can be coerced to or from a variant or passed to late-bound functions.

My code is really simple.
I am defining a new data type (Complex number, with a Real and imaginary component).
But every time I try to use this new data type from a private procedure by setting a value
Code:
  TF = CF(J)
, where TF and the CF array are defined as my complex type, I get this pesky error.

I feel as though I am missing something so basic that I will be greatly embarassed when my error is pointed out. Even so, if you know where I am messing up, please help anyhow!
 
How are you defining your data type? How are you declaring your variables of the new data type?

zemp
 
Zemp,

My new data type is defined in the declaration section for my form (I only have one form). It is defined as
Code:
Type Complex
   Real as Double
   Imag as Double
End Type
I have a command button on my form, and for the click event, I perform some graphics initialization, data acquisition, data display. All these work like a charm. However, I then call an FFT procedure. Within the FFT procedure I define my variables as follows:

Code:
Dim CF() As Complex
Dim TF As Complex
...

ReDim CF(MaxValue) 'MaxValue is a global variable set to 1024 or some other integer power of 2
...

For J = 1 to MaxValue
   ...
   TF = CF(J)
   ...
Next

As you can see, pretty simple! The odd part is that if I re-write the code as follows:
Code:
For J = 1 to MaxValue
   ...
   TF.real = CF(J).real
   TF.imag = CF(J).imag
   ...
Next
everything works just fine. However this feels wrong. If I have to work with the elements of my custom type one at a time, then I might as well not use a custom type at all.

FYI - I have re-written the code without any custom typing, and everything looks great. I just am fairly new to Visual Basic and need to be able to create and use custom types successfully.
Even a good working example or two of someone else's similar use of custom typing would be helpful, to see if there is an obvious issue, possibly scope or such.

Thanks for your help!
 
Try putting the TYPE in a separate BAS module and see what happens.

Lee
 
Place the Type in a module and declare the type as Public.
Code:
Public Type Complex
   Real as Double
   Imag as Double
End Type

If you are using redim don't for get to specify the type. mainly a good habit to get into.
Code:
ReDim CF(MaxValue) as Complex

zemp
 
To One and all - many thanks! I just needed to get the type declared in a module, and PUBLIC! I also needed to move my functions from my module where I defined the type to the form where they were used.

Simple it was, but your help invaluable!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top