is there shorter way to code this

R

rodchar

hey all,

given:

protected void SearchedValues2(object sender, EventArgs e)
{
if (sender is CheckBox)
{
CheckBox cb1 = (CheckBox)sender;
controlValues.Add(KeyValuePair<string, string>(cb1.ID,
cb1.Text));
}
else if (sender is DropDownList)
{
DropDownList ddl1 = (DropDownList)sender;
controlValues.Add(KeyValuePair<string, string>(ddl1.ID,
ddl1.Text));
}
else if (sender is TextBox)
{
TextBox txb1 = (TextBox)sender;
controlValues.Add(KeyValuePair<string, string>(txb1.ID,
txb1.Text));
}
}

question:
if i knew i only wanted the id and the value of the control could i shorten
the code somehow?

thanks,
rodchar
 
D

DabblerNL

First create an interface that exposes the Text and ID properties:
public interface ITextAndID
{
string Text {get;set;}
string ID{get;set;}
}

second create new TextBox and DropDownList and CheckBox Controles that
inherit their ancestors and implement the new interface:

public class InterfacedTextBox: TextBox, ITextAndID
{}

public class NewCheckBox: CheckBox. ITextAndID
{}

Mind that these new classes do not need any code between the brackets.

Finally replace the TextBox, CheckBox and DropDownList on your form by their
Interfaced counterparts. The compiler will complain until you have got this
right.

Now you method can be shortened to:
protected void SearchedValues2(object sender, EventArgs e)
{
if (sender is ITextAndID)
{
var cb1 = (ITextAndID)sender;
controlValues.Add(KeyValuePair<string, string>(cb1.ID,
cb1.Text));
}
 
J

Jeroen Mostert

DabblerNL said:
First create an interface that exposes the Text and ID properties:
public interface ITextAndID
{
string Text {get;set;}
string ID{get;set;}
}
[snip]
Or you could cast to Control. Given the DropDownList, this is probably ASP.NET.
 
R

rodchar

if i cast to Control how do i get the values from the different controls?

Jeroen Mostert said:
DabblerNL said:
First create an interface that exposes the Text and ID properties:
public interface ITextAndID
{
string Text {get;set;}
string ID{get;set;}
}
[snip]
Or you could cast to Control. Given the DropDownList, this is probably ASP.NET.
 
F

Family Tree Mike

rodchar said:
if i cast to Control how do i get the values from the different controls?

Jeroen Mostert said:
DabblerNL said:
First create an interface that exposes the Text and ID properties:
public interface ITextAndID
{
string Text {get;set;}
string ID{get;set;}
}
[snip]
Or you could cast to Control. Given the DropDownList, this is probably
ASP.NET.


Control is the base of all of your objects that you test. You can get
Control.ID, but, Control doesn't have a Text property so I don't think it
really helps you.
 
J

Jeroen Mostert

Family said:
rodchar said:
if i cast to Control how do i get the values from the different controls?

Jeroen Mostert said:
DabblerNL wrote:
First create an interface that exposes the Text and ID properties:
public interface ITextAndID
{
string Text {get;set;}
string ID{get;set;}
}

[snip]
Or you could cast to Control. Given the DropDownList, this is
probably ASP.NET.


Control is the base of all of your objects that you test. You can get
Control.ID, but, Control doesn't have a Text property so I don't think
it really helps you.
Oh, right -- System.Windows.Forms.Control is the one with the .Text property
for everything, while System.Web.UI.Control is the one with the .ID property
for everything. Now, there's actually an ITextControl that offers access to
..Text, but while TextBox and DropDownList implement ITextControl, CheckBox
does not. So I guess it's custom interface time after all.

For completeness, there's one way of accessing properties in a generic
fashion that (almost) always works, and that's data binding.
DataBinder.Eval(sender, "Text") will get you the text, but since it uses
reflection and can return only strings, this is none too efficient. Still,
this is not a loop, so it wouldn't matter much.
 
C

Cor Ligthert[MVP]

Control is the base of all of your objects that you test.

In windowforms not in asp.net
 
F

Family Tree Mike

Cor Ligthert said:
In windowforms not in asp.net

Sorry, I should have stated:

System.Web.UI.Control is the closest common ancestor of DropDownList,
CheckBox and TextBox in asp.net. DropDownList does not inherit from
System.Web.UI.WebControls.WebControl, which CheckBox and TextBox do inherit.

Mike
 
H

hack2root

thanks for this helpful discussion,
rod.







- Show quoted text -


....or you can be more "generic"

while it seems slightly more complex, it does not impact on a
perfrmance:

public abstract class BaseControl : ITextAndID
{
public abstract string Text
{ get; }
public abstract string ID
{ get; }
}

public abstract class
ControlWrapper(T, U):
BaseControl,
ITextAndID
where T : ControlWrapper(T, U)
{
private readonly U _c;
protected ControlWrapper(U control){ _c = control; }
}
public T Control
{ get { return _c; } }
}

public class TextBoxWrapper : ControlWrapper(TextBoxWrapper, TextBox)
{
public TextBoxWrapper(TextBox text) : base(text) { }
public override string Text
{
return Control.Text;
}
public override string ID
{
return Control.ID;
}
}


So, in code you can use BaseControl class in methods

AND use

BaseControl's .Control object for other stuff;

Wile I'd prefer to do all job in derived from BaseClass classes (which
makes Control property protected), sometimes it needs to access
original wrapped object
 

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