473,545 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("M yProp")

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 1366
On Dec 4, 3:48 pm, "Skysurfer" <skysurfer72TOG L...@libero.itw rote:
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("M yProp")

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,B utton).MyProp = 3
End If
Dec 4 '07 #2
"Skysurfer" <sk************ ****@libero.its chrieb
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("M yProp")

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(cont rol, A).propertyName ...
elseif typeof control is B then
directcast(cont rol, B).propertyName ...
elseif typeof control is C then
directcast(cont rol, C).propertyName ...
end if

Armin

Dec 4 '07 #3
"Olie" <ow****@gmail.c omha scritto nel messaggio
news:d5******** *************** ***********@l16 g2000hsf.google groups.com...
On Dec 4, 3:48 pm, "Skysurfer" <skysurfer72TOG L...@libero.itw rote:
>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.GetTyp e.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,B utton).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.WriteLi ne("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.itw rote:
>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.GetTyp e.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,B utton).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******** *************** ***********@mic rosoft.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
25398
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
1
1939
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 runtime (not at design time). How can I do this? Thanks.
5
1443
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
2499
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
4227
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 System.Web.UI.Control,and for my purpose,I define a delegate property in the control's class definition, so control user can define his(or her) method out of...
3
1407
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 the control.text property as below. Thanks, Tim foreach (Control ctrl in MultiView1.Views.Controls) {
1
3395
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, I'm getting the following error: Cannot create an object of type 'System.EventHandler' from its string representation 'OnSubmit' for the...
2
3617
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 controls return a ListItemCollections collection through the property .Items (ex. DropDownList1.Items in case of a dropdownlist) I need to find a way...
6
2303
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 making this harder than it needs to be, but seem to be buffaloed at this point. thanks to all.... -
0
7408
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7661
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7433
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7763
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1891
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
712
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.