Set property of a control without cast

S

Skysurfer

Hi.
I have some control of different type which have a particolar property, for
instance MyProp.
Some other controls don't have this property.
How can i make a generic sub that set this property without cast?
I can test if the control has the property with this line:

Control.GetType.GetProperty("MyProp")

But... how can i set this property?
I don't want to cast Control, because it could be of different types.

Thanks

Skysurfer
 
O

Olie

Hi.
I have some control of different type which have a particolar property, for
instance MyProp.
Some other controls don't have this property.
How can i make a generic sub that set this property without cast?
I can test if the control has the property with this line:

Control.GetType.GetProperty("MyProp")

But... how can i set this property?
I don't want to cast Control, because it could be of different types.

Thanks

Skysurfer

You should cast it but you can check whether it is the correct type
first. Use the following code, casting is good practice as it stops
you making mistakes.

If TypeOf Control Is Button Then
CType(Control,Button).MyProp = 3
End If
 
A

Armin Zingler

Skysurfer said:
Hi.
I have some control of different type which have a particolar
property, for instance MyProp.
Some other controls don't have this property.
How can i make a generic sub that set this property without cast?
I can test if the control has the property with this line:

Control.GetType.GetProperty("MyProp")

But... how can i set this property?
I don't want to cast Control, because it could be of different
types.


If possible, reflection should be avoided. The compiler can not check the
availability of the property and the execution is slow. The method to be
used for this purpose is usually inheritance or interface implementation.

Casting is still possible:

if typeof control is A then
directcast(control, A).propertyName ...
elseif typeof control is B then
directcast(control, B).propertyName ...
elseif typeof control is C then
directcast(control, C).propertyName ...
end if



Armin
 
S

Skysurfer

Olie said:
You should cast it but you can check whether it is the correct type
first. Use the following code, casting is good practice as it stops
you making mistakes.

If TypeOf Control Is Button Then
CType(Control,Button).MyProp = 3
End If

Thanks for your answer.
But this is what i should not to do.
In fact, myControl could be of 10 different types... and I don't want to
test and cast 10 different situations.

Thaks.

Skysurfer
 
O

Olie

You are missing the point of inheritance. If you have 10 different
types all with the same property then they should all be derived from
the same base type.

e.g.

Class A
Something()
End Class

Class B derived from A

Dim object As New B

If TypeOf object is B Then
Console.WriteLine("object is derived from A");
CType(object,A).Something()
End If

You see even though the object was instantiated with class B it will
still pass the typeof condition for class A as it was derved from A.
 
J

Jack Jackson

Hi.
I have some control of different type which have a particolar property, for
instance MyProp.
Some other controls don't have this property.
How can i make a generic sub that set this property without cast?
I can test if the control has the property with this line:

Control.GetType.GetProperty("MyProp")

But... how can i set this property?
I don't want to cast Control, because it could be of different types.

The easiest way is to use an Interface. The following code has not
been syntax checked.

Public Interface IMyProp
Public Property MyProp() as String
.....
End Interface

In your controls that have this property, add right after the Class
statement:
Implements IMyProp

On the line that defines the MyProp property, add:
Implements IMyProp.MyProp

In your code that looks at objects:

Dim imyprop as IMyProp = TryCast(control, IMyProp)
If imyprop Is Not Nothing Then
xx = imyprop.MyProp
End If
 
P

Phill W.

Thanks for your answer.
But this is what i should not to do.
In fact, myControl could be of 10 different types... and I don't want to
test and cast 10 different situations.

Welcome to the Wonderful World of Strong Typing.

If you want to handle ten different Types of Control then this (casting)
is /exactly/ what you're going to have to do.

Unless you can locate a common, base class or Interface that all your
Controls share (and which has your extra Property on it) then you're
going to have this sort of "down-casting" all over the place.

There's /nothing/ wrong with using it and it doesn't have /any/ overhead
at run-time so you've nothing to lose by doing it.

Regards,
Phill W.
 
C

Cor Ligthert[MVP]

Skysurver,

You can of course try of use late binding, in C# you have to use reflection
for that. In VB you can set option strict to Off, you get a lot of the minor
problems VB6 had including that your programs will run slower.

However who cares about that.

Cor
 
S

Skysurfer

Cor Ligthert said:
Skysurver,

You can of course try of use late binding, in C# you have to use
reflection for that. In VB you can set option strict to Off, you get a lot
of the minor problems VB6 had including that your programs will run
slower.

However who cares about that.

Cor

Thanks all, for your answers.

Skysurfer
 

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