?Pass a Class as a Property

Does any one know how i would
pass a class to a class as a Set and Get property?

kaiser18auAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GordonpCommented:
You just use the standard property Set/Get

Private m_MyClass as Class1

Property Get MyClass() as Class1
   Set MyClass = m_MyClass
End Property

Property Set MyClass(newValue as Class1)
   Set m_MyClass = newValue
End Property

You'll also need to initialise the class somehow. ie.

Private Sub Class_Initialize()
   Set m_MyClass = New Class1
End Sub

then destroy the object at the end

Private Sub Class_Terminate()
   Set m_MyClass = Nothing
End Sub

0
caraf_gCommented:
"You'll also need to initialise the class somehow"

No, you don't, but if you don't do it, be aware that your Property Get procedure may return "Nothing" so the calling code should have awareness for that coded into it.

Apart from that minor observation Gordonp's answer is excellent.
0
kaiser18auAuthor Commented:
Sorry guys

Let me explain.

I have an exe called RR5.exe and it calls a DLL (RPT.DLL)
RR5.exe has a Class called Projections.Class.

I am trying to include this Class in the DLL as well, hence the question.

The above solution I have already tried, but it comes back with the Error message.

Compile Error:
    Private object modules can not be used in public object modules as parameters or return types for public procedures as public data members or as fields of public user defined types.

This is my actual problem.

Any suggestions to overcome this would be appreciated.

Thank in advance.

Kaiser
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

anthonycCommented:
Kaiser:

private m_class as Class1

public property set PassClass(cls as object)
  set m_class = cls
end property

public property get PassClass() as Object
  set PassClass = cls
end property

This will allow you to pass the class, and also not require your dll to have a reference to the class.  This will use late binding.
0
GordonpCommented:
Use my original answer but you'll need your exe to be an activex exe and set Class1 to be PublicNonCreatable or MultiUse.

You need it to be an ActiveX exe to set up the COM data the dll needs to use the class.


Gordon
0
caraf_gCommented:
It can also be an ActiveX DLL...
0
kaiser18auAuthor Commented:
Hi AnthonyC

I tried your method, and it compiles, but when i get to pass the Class thru, it comes up with a Type mismatch between the Class in the EXE file and the one in the DLL.

Is it because the class in the DLL is passed in as an object?

I dont know how to fix it.


GordanP -
   I tried as you said and it still wont work, and anyway I can not change the program from its Standard EXE start up. Its out of my control so I have to stay as it is.

Thanks
0
caraf_gCommented:
kaiser,

What did you call your class?

Let's say you named the class clsYourClass. This class is defined in your DLL. Make it public and multiuse (see the class properties).

You do NOT need to include the class in your .exe's VB Project. The only thing you need to have in your .exe's VB project is a reference to your DLL (see Project-references)

Once you've got a reference to your DLL in your .exe's project, you can do this:

Dim anObjectVariable As clsYourClass

Or, which is better:
Dim anObjectVariable As YourDLL.clsYourClass - just in case you've got references to multiple DLLs that expose a class named clsYourClass

Anyway, you can then use anObjectVariable exactly the same way as you would if clsYourClass were actually defined in your .exe project.

If you include clsYourClass in your .exe project, your .exe project will get confused:

If you do
Dim anObjectVariable As clsYourClass, it will create a variable which type is clsYourClass AS DEFINED IN YOUR .EXE PROJECT

If you then do, for example:
Call SomeOtherObjectVariableFromYourDLL.SomeMethod(anObjectVariable) 'where the dll expects a variable of type clsYourClass, the method SomeMethod will expect a variable of type clsYourClass AS DEFINED IN YOUR DLL

You know and I know that clsYourClass from the .exe and clsYourClass from the DLL came from the same source code and are in fact identical, but VB doesn't know that. As far as it's concerned it's dealing with two different variable types. Hence the Type Mismatch error.

So, remove the class module from your .exe. That should help.
0
GordonpCommented:
If you cant have the exe as an ActiveX Exe then your pretty much stuck for passing your class.

Even passing the property as an object wont suffice, as the class being passed is not public. And the class needs to be public for other applications to access it. I had this problem about a year ago on a project I was doing.

See Error 98 in the VB help

extract

"A property or method call cannot include a reference to a private object, either as an argument or as a return value (Error 98)

@A client invoked a property or method of an out-of-process component and attempted to pass a reference to a private object as one of the arguments.  A client invoked a property or method of an out-of-process component and the component attempted to return a reference to a private object, or to assign such a reference to a ByRef argument."

If you havent got control over the exe then I'm afraid your probably stuck.

However, if you provide some details on why your trying to pass the class, there may be an other way round the problem




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
caraf_gCommented:
Gordon,

"If you cant have the exe as an ActiveX Exe then your pretty much stuck for passing your class. "

That's not quite true...

kaiser can work around the problem by defining the Projections Class in the DLL (as a public, multiuse class) instead of in the .exe, like I said in my last comment.
0
GordonpCommented:
When I first read Kaisers comments I thought he meant he had no control over the Exe.

If he does then your answer is great, otherwise hes stuffed.

Gordon
0
caraf_gCommented:
:-) Yes, I agree.
0
GordonpCommented:
When I first read Kaisers comments I thought he meant he had no control over the Exe.

If he does then your answer is great, otherwise hes stuffed.

Gordon
0
GordonpCommented:
When I first read Kaisers comments I thought he meant he had no control over the Exe.

If he does then your answer is great, otherwise hes stuffed.

Gordon
0
caraf_gCommented:
Tip: If you think your connection has died and your comment didn't go through, hit refresh or reload ? first to see whether it did or not <grin>
0
amebaCommented:
>"pass a class"
Correction: we normally pass objects, not classes. So, use: "Pass a class instance"
0
caraf_gCommented:
Absolutely
0
kaiser18auAuthor Commented:
Thanks gordan.

Now at least I know why I am stuffed.
I am held up by some limitations I have no control over.
1. I cant change the EXE mode.
2. I can not remove any class's from my exe.

That has made my task a HEll luv alot harder.

Like your comment. I am stuffed.

Thanks guys for all your useful input.

Best regards.
Kaiser
0
GordonpCommented:
Kaiser,
       Is there any particular reason you cannot change the EXE Mode, as it sounds from your comment you have access to the code of the EXE.

The EXE will run as before it just means you need to start in Sub Main rather than a form, and your classes can be public.

IF the exe currently starts in Form1
sub Main just needs to be

Private Sub Main()
    Form1.Show
End Sub

Thanks for the points anyway

Gordon
0
kaiser18auAuthor Commented:
Hello experts.

I can have control over the EXE now, so is it too much to ask for your assistance in overcoming the passing of a class.

The question I posted is titled
"DLL Experts required", but you guys are ok :)
Its question Q10242835.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.