Property and type

M

Meelis

Hi

Can one property have different types?
For example, i need to property type to be dependend on second property.

Pirvate MyValue as Object
Private MyType as Integer

Public Property PType() as Integer
Get
Return MyType
End Get
Set (ByVal value As Integer)
MyType=value
If MyType=1 Then Value=Windows.Forms.CheckState
If MyType=2 Then Value=Boolean
End Set
End Property

Public Property Value() as Object
Get
Return MyValue
End Get
Set (ByVal value as Object)
MyValue=value
End Set
End Property

But on propertywindow property "Value" is "grayed" and i cant choose
values.


Regards;
Meelis
 
C

Chris

Meelis said:
Hi

Can one property have different types?
For example, i need to property type to be dependend on second property.

Pirvate MyValue as Object
Private MyType as Integer

Public Property PType() as Integer
Get
Return MyType
End Get
Set (ByVal value As Integer)
MyType=value
If MyType=1 Then Value=Windows.Forms.CheckState
If MyType=2 Then Value=Boolean
End Set
End Property

Public Property Value() as Object
Get
Return MyValue
End Get
Set (ByVal value as Object)
MyValue=value
End Set
End Property

But on propertywindow property "Value" is "grayed" and i cant choose
values.


Regards;
Meelis

You can not have a property be the same name as the variable.

Try:
Public Property Value() as Object
Get
Return MyValue
End Get
Set (ByVal InValue as Object)
MyValue=InValue
End Set
End Property

But you can not assign "MyValue" a type like you are trying. What are
you trying to accomplish?
 
M

Meelis

Hi Chris

Yes, in .NET you can have same names for property and value, but thats not
the probelm.
I'll try to explain what i want in my poor english
Lets say i have two properties p1 and p2

p1 is integer and value can be 1 or 2
if value is 1 then p2 type must be Windows.Forms.CheckState
and if value is2 then p2 type must me Boolean

now step by step

If user selects in property window p1 value to 1,
p2 type mus change to CheckState and user can select Checked, UnChecked aso.

If p1 value is selected to be 2
p2 type must be Boolean and use can select True or False.

Hope you understand me :)


Meelis
 
C

Carlos J. Quintero [VB MVP]

Hi Meelis,

A property can have only one type, but if you declare it as Object, it can
hold values of different types (Windows.Forms.CheckState or Boolean, as in
your case). However, a class with an Object type property in a PropertyGrid
(used by the properties window) won´t show the enum values for the type,
since the type is Object and the PropertyGrid looks at the property type,
not at the type of the actual value (which could be Nothing). So, for the
PropertyGrid to work as you expect, you need two properties, one Boolean and
other Windows.Forms.CheckState and the design-time architecture is powerful
enough (at least .NET 2.0) to dinamically allow hiding one or the other
property to the PropertyGrid depending on the value of a 3rd property. You
can do this adding or removing the Browsable attribute to the property of
component instance, not to the property of the type (it could affect other
instances).

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 

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