Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a more generic function 1

Status
Not open for further replies.

Signit

MIS
Oct 17, 2003
114
US
I am trying to make the below function more generic. Presently, I have set it up so I can call it by passing it an HtmlInputText object and the name of the class I want to switch to. However, I'd like to configure this function to work for any object; any thoughts on how to accomplish this? Additionally, if someone has a suggestion for a better method of performing this operation I'd be interested in different solutions. Thank you!

Code:
public void switchClass(HtmlInputText textBox, string className)
  {
    IEnumerator keys = textBox.Attributes.Keys.GetEnumerator();
    
    while (keys.MoveNext()) 
    {
      String key = (String)keys.Current;
      
      if (key == "class")
        textBox.Attributes[key] = className;
    }
  }
 
What class does HtmlInputText inherit from?
If that doesn't do it, what class does that inherit from?
Repeat as needed.

If you end up at System.Object, you may have gone too far, and there wouldn't be a point in making it more generic.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Okay so reference: System.Web.UI.HtmlControls.HtmlControl instead of System.Web.UI.HtmlControls.HtmlInputControl. That makes good sense. Thanks for the advice!
 
Just be aware that since the parameter type is now HtmlControl, the strong typing (intellisense) limits you to the methods and properties of that type. If you want to use a specific HtmlControl subclass, you'll need to inspect the real type of what you were passed, and then cast it to that type.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Right, so if I wanted to access a particular property of a texbox for instance I'd have to cast the passed object. Really, my only goal with this function is switching an objects class. Am I doing this in the most efficent way or is there a more efficent way to perform this operation?
 
Nope, this is how you'd do it. If you need to pass in one of different types, you'd use an ancestor of them, or possibly an interface.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top