Setting Text Value

  • Thread starter Thread starter Steve Letford
  • Start date Start date
S

Steve Letford

Hi,
I'm parsing every Button / Label / hyperlink that is on my web forms so that
I can look at the text values and translate them. I used to do this in
VB.Net by casting everything as an object (Adapting the page inheritance
model by Johathon Goodyear):

As shown:
CType(myControl, Object).text = TranslatedVersion(CType(myControl,
Object).text)

However, If I try to do something similar with C# this code doesn't work and
I get a compile error.
i.e. ((System.Web.UI.WebControls.WebControl)myControl).Text

Any ideas of what I need to do for a similar effect in C#?

Thanks for any help.

Steve
 
Steve:
You can't without using reflection. The only reason you could in VB.net was
because you had Option Strict turned off instead of On. Your code was
using what's called "late-binding" which is (a) slow (b) not very readable
and (c) more likely to give a run-time error. There are certainly times
when late-binding is a necessary feature, but my personal opinion is that I
like how hard C# makes it because them it makes you think "is this really
the best way to do it?!"...most of the time you'll hopefully find out that
it isn't...

You could do:

Type type = myControl.GetType();
PropertyInfo pi = type.GetProperty("Text");
if (pi != null){
pi.SetValue(myControl, TranslatedVersion(pi.GetValue(myControl, null)),
null);
}

or something similar...

Dunno what you are doing with translation, but you might wanna take a look
at:
http://openmymind.net/index.aspx?documentId=3
and
http://openmymind.net/index.aspx?documentId=4

Karl
 
Back
Top