Question regarding C# inheritance

I am working on a C# ASP.NET page that involves code to read the text value of a control and set the CSS class of that control based on the text. (posting here rather than /r/dotnet because it seems more of a generic C# issue.)

I am trying to create a function where I can pass a control to it and have it do the work. Currently I have two functions, one for labels and one for hyperlinks like this: (Oversimplified example, what I'm doing is much more complex.)

protected void SetLabel(Label l) { if(l.Text == "error") l.CssClass = "ErrorClass"; } protected void SetLink(HyperLink hl) { if(hl.Text == "error") hl.CssClass = "ErrorClass"; } 

Since both Label and Hyperlink are children of the Control object I tried creating a generic SetControl function passing a Control object, but that didn't work…

protected void SetControl(Control c) { if(c.Text == "error") c.CssClass = "ErrorClass"; // This doesn't work, Control does not contain a 'Text' property. } 

It seems to me there should be a way to do this, but I haven't been able to figure it out. Does anyone have any ideas?

by Gojs2015 via /r/csharp

Leave a Reply