Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

is there shorter way to code this

0 views
Skip to first unread message

rodchar

unread,
Mar 5, 2009, 4:22:01 PM3/5/09
to
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

DabblerNL

unread,
Mar 5, 2009, 5:29:01 PM3/5/09
to
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));
}

Jeroen Mostert

unread,
Mar 5, 2009, 5:39:30 PM3/5/09
to
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.

--
J.

Family Tree Mike

unread,
Mar 5, 2009, 10:52:05 PM3/5/09
to
"rodchar" <rod...@discussions.microsoft.com> wrote in message
news:82E3E487-8514-45F9...@microsoft.com...
> if i cast to Control how do i get the values from the different controls?


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.

--
Mike

Jeroen Mostert

unread,
Mar 6, 2009, 1:08:07 AM3/6/09
to
Family Tree Mike wrote:
> "rodchar" <rod...@discussions.microsoft.com> wrote in message
> news:82E3E487-8514-45F9...@microsoft.com...
>> if i cast to Control how do i get the values from the different controls?
>>
>> "Jeroen Mostert" wrote:
>>
>>> 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.

--
J.

Cor Ligthert[MVP]

unread,
Mar 6, 2009, 1:19:03 AM3/6/09
to
> Control is the base of all of your objects that you test.

In windowforms not in asp.net

rodchar

unread,
Mar 6, 2009, 2:42:01 PM3/6/09
to
thanks for this helpful discussion,
rod.

hack...@gmail.com

unread,
Mar 17, 2009, 6:36:36 AM3/17/09
to
> > rodchar- Hide quoted text -
>
> - 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

0 new messages