Casting question

D

Dan Tallent

I have code here that checks the type of an object and then cast it so I can
access its properties. In the simple example below I am accessing the
ReadOnly property that is unique to textbox controls. This code works
however I wanted to find out if its possible to write code using a
collection or array.. see below


Type oType = oControl.GetType();
if (oType == typeof(System.Windows.Forms.TextBox))

{

((System.Windows.Forms.TextBox)oControl).ReadOnly == true;

}

//==============================================

So the code below is kinda what I have in mind. Notice the line where I
am attempting to cast. I know this code

doesn't work, but I am wondering if there is a way to code such a routine.

//==============================================

private ArrayList DevControls;

DevControls = new ArrayList();

DevControls.Add(typeof(System.Windows.Forms.TextBox));

DevControls.Add(typeof(System.Windows.Forms.ComboBox));

Type oType = oControl.GetType();

for (int cnt = 0; cnt<DevControls.Count; cnt++)

{

if (DevControls[cnt] == oType)

{

// This line below is the question... is there a way to code
this ?

( (DevControls[cnt]) oControl).ReadOnly = true;

}

}





//==============================================
 
S

Stefan Hoffmann

hi Dan,

Dan said:
private ArrayList DevControls;
DevControls = new ArrayList();
DevControls.Add(typeof(System.Windows.Forms.TextBox));
DevControls.Add(typeof(System.Windows.Forms.ComboBox));
Type oType = oControl.GetType();
for (int cnt = 0; cnt<DevControls.Count; cnt++)
{
if (DevControls[cnt] == oType)
{
( (DevControls[cnt]) oControl).ReadOnly = true;
(DevControls[cnt]) doesn't contains a control.
Using

DevControls.Add(new ComboBox());

should work.


mfG
--> stefan <--
 
A

Alberto Poblacion

It's not exactly what you were asking, but you can achieve a similar result
by means of System.Reflection:

Type t = oControl.GetType();
PropertyInfo pi = t.GetProperty("ReadOnly");
if (pi!=null)
{
pi.SetValue(oControl, true, null);
}
 
D

Dan Tallent

I thought about using reflection, the problem I am having there is the some
of the controls I'm working with have "sub-properites".

Example: The reach the actual property I would write code like this.
TextBox1.Properties.ReadOnly = true;

I'm not sure how to get to the sub-properties using reflection.

Thanks
Dan



Alberto Poblacion said:
It's not exactly what you were asking, but you can achieve a similar
result by means of System.Reflection:

Type t = oControl.GetType();
PropertyInfo pi = t.GetProperty("ReadOnly");
if (pi!=null)
{
pi.SetValue(oControl, true, null);
}



Dan Tallent said:
I have code here that checks the type of an object and then cast it so I
can access its properties. In the simple example below I am accessing the
ReadOnly property that is unique to textbox controls. This code works
however I wanted to find out if its possible to write code using a
collection or array.. see below


Type oType = oControl.GetType();
if (oType == typeof(System.Windows.Forms.TextBox))

{

((System.Windows.Forms.TextBox)oControl).ReadOnly == true;

}

//==============================================

So the code below is kinda what I have in mind. Notice the line where
I am attempting to cast. I know this code

doesn't work, but I am wondering if there is a way to code such a
routine.

//==============================================

private ArrayList DevControls;

DevControls = new ArrayList();

DevControls.Add(typeof(System.Windows.Forms.TextBox));

DevControls.Add(typeof(System.Windows.Forms.ComboBox));

Type oType = oControl.GetType();

for (int cnt = 0; cnt<DevControls.Count; cnt++)

{

if (DevControls[cnt] == oType)

{

// This line below is the question... is there a way to
code this ?

( (DevControls[cnt]) oControl).ReadOnly = true;

}

}
 
D

Dan Tallent

Hi Stefan,

This method doesn't work because the code won't compile. I believe the
problem is that the DevControls[cnt] is not a known type.
((DevControls[cnt])oControl).ReadOnly = true;

In traditional casting code like below uses a known type.
((System.Windows.Forms.TextBox)oControl).ReadOnly = true;

I am trying to find a way where I can loop throught each control on the form
and set its properties based on its Type.

Thx
Dan




Stefan Hoffmann said:
hi Dan,

Dan said:
private ArrayList DevControls;
DevControls = new ArrayList();
DevControls.Add(typeof(System.Windows.Forms.TextBox));
DevControls.Add(typeof(System.Windows.Forms.ComboBox));
Type oType = oControl.GetType();
for (int cnt = 0; cnt<DevControls.Count; cnt++)
{
if (DevControls[cnt] == oType)
{
( (DevControls[cnt]) oControl).ReadOnly = true;
(DevControls[cnt]) doesn't contains a control.
Using

DevControls.Add(new ComboBox());

should work.


mfG
--> stefan <--
 
A

Alberto Poblacion

Dan Tallent said:
I thought about using reflection, the problem I am having there is the some
of the controls I'm working with have "sub-properites".

Example: The reach the actual property I would write code like this.
TextBox1.Properties.ReadOnly = true;

I'm not sure how to get to the sub-properties using reflection.

No problem. Just repeat the operation twice:

Type t1 = oControl.GetType();
PropertyInfo pi1 = t1.GetProperty("Properties");
if (pi1!=null)
{
object prop = pi1.GetValue(oControl, null);
Type t2 = prop.GetType();
PropertyInfo pi2 = t2.GetProperty("ReadOnly");
if (p2i!=null)
{
pi.SetValue(prop, true, null);
}
}
 
I

Ignacio Machin ( .NET/ C# MVP )

I have code here that checks the type of an object and then cast it so I can
access its properties. In the simple example below I am accessing the
ReadOnly property that is unique to textbox controls. This code works
however I wanted to find out if its possible to write code using a
collection or array.. see below

Type oType = oControl.GetType();
if (oType == typeof(System.Windows.Forms.TextBox))

{

((System.Windows.Forms.TextBox)oControl).ReadOnly == true;

}

A better approach is
TextBox tb = oControl as TextBox;
if ( tb!= null ){
}

you can use the same technique if the number of alternatives is nt too
long.
A more general solution would be using a dictionary:
Dictionary< Type, string> where the value indicate the name of the
property you want.
You could have:
< typeof(TextBox), "readOnly">;
< typeof(DropDown), "SelectedIndex">;

Then you have a method that using reflection get the value based on
the dictionary:

object o = GetValue( instance, dcitionary[ typeof(oControl]);
 
I

Ignacio Machin ( .NET/ C# MVP )

I thought about using reflection, the problem I am having there is the some
of the controls I'm working with have "sub-properites".

Example: The reach the actual property I would write code like this.
TextBox1.Properties.ReadOnly = true;

I'm not sure how to get to the sub-properties using reflection.

Thanks
Dan

It's not exactly what you were asking, but you can achieve a similar
result by means of System.Reflection:
Type t = oControl.GetType();
PropertyInfo pi = t.GetProperty("ReadOnly");
if (pi!=null)
{
pi.SetValue(oControl, true, null);
}
Dan Tallent said:
I have code here that checks the type of an object and then cast it so I
can access its properties. In the simple example below I am accessing the
ReadOnly property that is unique to textbox controls. This code works
however I wanted to find out if its possible to write code using a
collection or array.. see below
Type oType = oControl.GetType();
if (oType == typeof(System.Windows.Forms.TextBox))
{
((System.Windows.Forms.TextBox)oControl).ReadOnly == true;
}
//==============================================
So the code below is kinda what I have in mind. Notice the line where
I am attempting to cast. I know this code
doesn't work, but I am wondering if there is a way to code such a
routine.
//==============================================
private ArrayList DevControls;
DevControls = new ArrayList();
DevControls.Add(typeof(System.Windows.Forms.TextBox));
DevControls.Add(typeof(System.Windows.Forms.ComboBox));
Type oType = oControl.GetType();
for (int cnt = 0; cnt<DevControls.Count; cnt++)
{
if (DevControls[cnt] == oType)
{
// This line below is the question... is there a way to
code this ?
( (DevControls[cnt]) oControl).ReadOnly = true;
}
}

I have posted code before that do that; recursively evaluate complex
expression like Class.SubClass1.Subclass2.Property

This is the relevant part fo the code:
int Compare( object x, object y, string comparer)
{
if ( comparer.IndexOf( ".") != -1 )
{
//split the string
string[] parts = comparer.Split( new char[]{ '.'} );
return Compare( x.GetType().GetProperty( parts[0]).GetValue(x,
null) ,
y.GetType().GetProperty( parts[0]).GetValue(y, null) , parts[1]
);
}
else
{
 
D

Dan Tallent

This seems like a good approach, but for some reason its erroring out about
AmbiguousMatchException.

I'm going to look into using a recursive approach as suggested by Ignacio
Machin A little worried about
the speed using recursion and reflection.

Should be interesting.. I'll keep you posted.
Dan
 
D

Dan Tallent

This was a pretty good idea... but I ended up using this code:

Type t1 = oControl.GetType();


if (t1 == typeof(DevExpress.XtraEditors.MemoEdit))

{

((DevExpress.XtraEditors.MemoEdit)oControl).Properties.ReadOnly = ReadOnly;

Handled = true;

}

if (t1 == typeof(DevExpress.XtraEditors.TextEdit))

{

((DevExpress.XtraEditors.TextEdit)oControl).Properties.ReadOnly = ReadOnly;

Handled = true;

}

Thanks to everyone for your help. I really wish I could of found a way to
use a variable in place of the actual Control, but time is short and there
is much to do.



Thanks again

Dan









message
I have code here that checks the type of an object and then cast it so I
can
access its properties. In the simple example below I am accessing the
ReadOnly property that is unique to textbox controls. This code works
however I wanted to find out if its possible to write code using a
collection or array.. see below

Type oType = oControl.GetType();
if (oType == typeof(System.Windows.Forms.TextBox))

{

((System.Windows.Forms.TextBox)oControl).ReadOnly == true;

}

A better approach is
TextBox tb = oControl as TextBox;
if ( tb!= null ){
}

you can use the same technique if the number of alternatives is nt too
long.
A more general solution would be using a dictionary:
Dictionary< Type, string> where the value indicate the name of the
property you want.
You could have:
< typeof(TextBox), "readOnly">;
< typeof(DropDown), "SelectedIndex">;

Then you have a method that using reflection get the value based on
the dictionary:

object o = GetValue( instance, dcitionary[ typeof(oControl]);
 
I

Ignacio Machin ( .NET/ C# MVP )

This seems like a good approach, but for some reason its erroring out about
AmbiguousMatchException.

I'm going to look into using a recursive approach as suggested by Ignacio
Machin A little worried about
the speed using recursion and reflection.

Should be interesting.. I'll keep you posted.
Dan

The recursion will be just a few steps down. Not too much to worry
about
 

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