473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Set property of a control without cast

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
Dec 4 '07 #1
8 1353
On Dec 4, 3:48 pm, "Skysurfer" <skysurfer72TOGL...@libero.itwrote:
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
Dec 4 '07 #2
"Skysurfer" <sk****************@libero.itschrieb
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

Dec 4 '07 #3
"Olie" <ow****@gmail.comha scritto nel messaggio
news:d5**********************************@l16g2000 hsf.googlegroups.com...
On Dec 4, 3:48 pm, "Skysurfer" <skysurfer72TOGL...@libero.itwrote:
>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
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
Dec 4 '07 #4
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.

Dec 4 '07 #5
On Tue, 04 Dec 2007 15:48:42 GMT, "Skysurfer"
<sk****************@libero.itwrote:
>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
Dec 4 '07 #6
Skysurfer wrote:
>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.
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.
Dec 5 '07 #7
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

Dec 6 '07 #8
"Cor Ligthert[MVP]" <no************@planet.nlha scritto nel messaggio
news:EE**********************************@microsof t.com...
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
Dec 10 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
1
by: uiranejeb | last post by:
Hi everybody, I have a control that is dragged on a form. Let's say that we have a textbox control named txt1. I want to dynamiclly set a property of this control. I know the name of the control at...
5
by: Nicolas | last post by:
How can I simply do this. string tAlign; tAlign="Left"; TextBox textbox1 = new TextBox(); textbox1.TextAlign = tAlign; I know that it shoul be like "textbox1.TextAlign =
15
by: Mark Goldin | last post by:
I have main aspx page with a number of user controls. How can I create a global property that will be visible in every user control? Thanks
5
by: han zhiyang | last post by:
Hi. I tried to design a custom web control which can flexibly and dynamicly let the control user ,for example the web page developer, customize its layout codes.This control derives from...
3
by: Tim_k | last post by:
Does anyone know how to set the text property for a control that is loaded into the Control class in a web form? The text property exists for Windows forms but not for web pages? I'd like to set...
1
by: Paul | last post by:
Hi all, I have a custom server control that can contain one or more buttons. I'm trying to expose an event handler as a property so that the buttons can fire a click event when clicked. However,...
2
by: letmefly | last post by:
Hello , i use a method that has a Control as parameter ex. public static void Method(Control c) This control can be RadioButtonList, DropDownList , CheckBoxList , ListBox ..All of these...
6
by: forest demon | last post by:
i have a custom control that gets loaded at runtime. i need to be able to access a property of a control thats part of the main form, through the clcik event of the custom control. i may be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.