Monday, March 9, 2009

The "Weekly" Newsgroup post #1

When I decided to become more active in the dot net community, I did a number of things:

I started this blog,
I started to attend the monthly meetings of our local users group ( omahamtg.com )
I started writing articles for codeproject.com (None of which I've finished yet - Bad Lee)

And, I started to hang out in the C# and ASP.net newsgroups as well as the forums at asp.net and silverlight.net in the hope that with my feeble knowledge I could help someone that the more experienced and knowledgeable would pass over as not worth their time.

Well, first thing I learned was that all the "Experts" on these newsgroups/forums are so helpful that I was only able to help a few people, and only then because their questions dovetailed exactly into my knowledge base.

But the big thing I learned was home much I could learn by reading the posted questions and answers. People are out there trying to do things with Dot Net that I never even thought of trying and reading in the replies how achieve those things has expanded my knowledge of all things Dot Net.

I used to think (and still do) that reading others source code was a great way to learn. When I found a link to Scott Hanselman's Weekly Source Code blog entries, I was forever indebted to Scott for searching the web and finding those few source code gems in the vast sea that is the Web.

I the same vein as Scott's Weekly Source Code posts, I'd like to start a "Weekly" posting of one or more Newsgroup/Forum questions that really deserve more focus than they received.

And Now: Question of the Week #1
================================
is there shorter way to code this

In this series of questions and answers, rodchar asks how he can get to two properties (Id and Text) of three different controls. His example shows the brute force way of taking an Object and through a series of IF statements determining if the Object "IS" of a certain type, then casting it to that type so as to access its ID and Text properties.

On reading the answers the Experts gave, it is obviously a perfect problem to be solved by an Interface. I chose this question because in my dealings with other programmers, Interfaces are the hardest part of OOP for them to grasp. Here in this one simple question, we have nearly the perfect example of how and where to use an interface.

Since there is now common parent that exposes the Text property, rodchar cannot just cast the Object to the parent. But, since they all already implement a Text property and an Id property, we can define an Interface that declares these two properties.

Here is the Interface suggested by DabblerNL

public interface ITextAndID 
{
string Text {get;set;}
string ID{get;set;}
}


Now, All that is needed is to have the Objects implement the interface. Since they technically have already done so, they only need to be declared.

public class InterfacedTextBox: TextBox, ITextAndID {} 
public class InterfacedCheckBox: CheckBox, ITextAndID {}


And viola! Just replace your old TextBox and CheckBox with the new versions and then all you need to do is cast the new controls "AS" your new interface: ITextAndID as so!

var TheId = (ITextAndID)TheObject).ID;
var TheText = (ITextAndID)TheObject).Text;


Being able to access these properties through the interface, and being able to implement the interface with no actual code other than the declarations makes this a pure example of interfaces!

1 comment:

Anonymous said...

LOL! Very funny! ;)

...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)
{
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