Public Shared Property

A

Al

I'd like to create Class Library in VB 2005, which has a property accessible
by external programs.
I decided to include 1 Class with 1 property in this project.
I placed this code in Class:
Public Class COM

Private mMyProp As String

Public Property MyProp() As String

Get

Return mMyProp

End Get

Set(ByVal value As String)

mMyProp = value

End Set

End Property

End Class

I compiled the project, created another project (Windows Forms) and added
this code to it:
Private obj As Object
Private Sub Command1_Click()
Set obj = CreateObject("SharedTestDLL.COM")
obj.MyProp = "Test"
MsgBox obj.MyProp
End Sub
Everything is fine so far.

But I wouldn't want to create an object, so I added for testing purposes
another Property as Public Shared:

Public Class COM

Private mMyProp As String

Private Shared mMyProp2 As String

Public Property MyProp() As String

Get

Return mMyProp

End Get

Set(ByVal value As String)

mMyProp = value

End Set

End Property

Public Shared Property MyProp2() As String

Get

Return mMyProp2

End Get

Set(ByVal value As String)

mMyProp2 = value

End Set

End Property

End Class

I also added the code to work with the second property in my client:
obj.MyProp2 = "Test2"
MsgBox obj.MyProp2
I'm getting an error:
Object doesn't support this property or method.

Obviously I do not understand Shared stuff. What am I doing wrong?
I tried the same code with Early Binding and it did not work too.

So, how do I create and use properties without creating an object in the
client?

Thank you
Al
 
M

Marina Levit [MVP]

The problem is you are still accessing the property through the object
reference 'obj', which you presumably never instantiate.

You are supposed to access shared member through the class name.

Like: COM.MyProp2

Additionally, I am not sure why you are calling CreateObject? This is just a
..NET class. Use a constructor if you want an instance of it.
 
V

vul

Thank you, Marina.
How do I access shared member from an external program?
I wanted to make the property Shared to avoid creating Objects inside of DLL
by other Classes and Windows Forms. And inside of DLL this approach works.
But I need to access those properties from outside of DLL as well.

Al
 
A

Al

Could you give me the code for accessing property in DLL from external
program (VB client), please. Because I'm still confused.
Thank you
Al
 
M

Marina Levit [MVP]

I did give you code. If your class name is COM, and the property is named
MySharedProp2, you would just say "COM.MySharedProp2".

That is all there is.

If you could create an instance of the class in the other program, then you
can certainly access a shared property.

It sounds to me like you are missing something else much more basic,
although what that is hard to say with the information you provided.
 
A

Al

I'm trying to use in client:
SharedTestDLL.COM.MyProp2 = "Test2"
MsgBox SharedTestDLL.COM.MyProp2
and get an error : Variable not defined.
I tried:
COM.MyProp2 = "Test2"
and
MyProp2 = "Test2"
with no succes either.
Maybe I need to declare some variable first then assign somehow the value,
but it looks like creating an object from my DLL class again. And you say I
should not do that (create an instance)
So, how the code in VB client should look in order to work with MyProp2
property wich is declared as Public Shared inside of DLL's Class?
Sorry for my stupidity.
Al
 
A

Al

Sorry, I misled you.
I'm using VB6 client. Probably this is the problem. I just tried to use VB
2005 client and after I added a reference to SharedTestDLL, I got an access
to COM class and its shared property.
Is Late Binding possible to access that shared property?
I have a reason to use VB6 with Late Binding (long explanation)
So I need somehow to find the way to work with properties of DLL from VB6
client.
I also have a reason to create DLL in VB 2005, not in VB6.

Al
 
H

Herfried K. Wagner [MVP]

Al said:
I'd like to create Class Library in VB 2005, which has a property
accessible by external programs.
I decided to include 1 Class with 1 property in this project.
I placed this code in Class:
Public Class COM

Private mMyProp As String

Public Property MyProp() As String

Get

Return mMyProp

End Get

Set(ByVal value As String)

mMyProp = value

End Set

End Property

End Class

I compiled the project, created another project (Windows Forms) and added
this code to it:
Private obj As Object
Private Sub Command1_Click()
Set obj = CreateObject("SharedTestDLL.COM")

I am wondering why you are using 'CreateObject' instead of 'New
SharedTestDLL.COM'.
But I wouldn't want to create an object, so I added for testing purposes
another Property as Public Shared:

Public Class COM

Private mMyProp As String

Private Shared mMyProp2 As String

Public Property MyProp() As String

Your property declaration is lacking the 'Shared' keyword.
I also added the code to work with the second property in my client:
obj.MyProp2 = "Test2"
MsgBox obj.MyProp2
I'm getting an error:
Object doesn't support this property or method.

Type 'obj' as 'COM' instead of 'Object'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top