|
There has to be a better way...
Last post 02-26-2008 2:36 PM by ekolis. 32 replies.
-
02-20-2008 3:36 PM
|
|
-
Jonathan Holland


- Joined on 01-10-2008
- Posts 163
|
There has to be a better way...
currentControl is from a System.Web.UI.ControlCollection, so it is cast as a "Control". There has to be a better way to figure out what the actual subclass is.
Current code:
if (foundControl != null) { Type controlType = currentControl.GetType(); if (controlType == typeof(Label)) { Label control = (Label)currentControl; control.Text = foundControl[_displayLabelColumn].ToString(); } else if (controlType == typeof(Literal)) { Literal control = (Literal)currentControl; control.Text = foundControl[_displayLabelColumn].ToString(); } else if (controlType == typeof(LinkButton)) { LinkButton control = (LinkButton)currentControl; control.Text = foundControl[_displayLabelColumn].ToString(); } } Repeat for every control that has a text property.
|
|
-
-
belgariontheking


- Joined on 08-20-2007
- Cincinnati, OH, USA
- Posts 1,365
|
Re: There has to be a better way...
Of course there's a better way. I'll pseudocode it. 1) Generate random string of characters s.
2) if s = type goto 4 3) goto 1 4) ??? 5) Profit!
I guess I'm back.
Please continue to spam the addresses below.
PLEASE SPAM: jtobin@ohioinstituteofhealthcareers.edu jtobin@ohiobusinesscollege.edu
|
|
-
-
AbbydonKrafts


- Joined on 11-21-2006
- Carrollton, GA, USA
- Posts 1,022
|
Re: There has to be a better way...
belgariontheking:Filed under: FUCKING HELL WHY DOES IT DELETE TWO CHARACTERS WHEN I HIT BACKSPACE ONCE?
So it's not just me. I brought up a bunch of tag flaws in another sidebar post, and this was one of them. I'm gonna use that tag now.
Join us at #TDWTF on irc.slashnet.org !
|
|
-
-
dhromed


- Joined on 04-13-2005
- Dutchland
- Posts 2,614
|
Re: There has to be a better way...
AbbydonKrafts: belgariontheking:Filed under: FUCKING HELL WHY DOES IT DELETE TWO CHARACTERS WHEN I HIT BACKSPACE ONCE?
So it's not just me. I brought up a bunch of tag flaws in another sidebar post, and this was one of them. I'm gonna use that tag now. Why the fuck doesn't it do that for me?
— Flurp.
|
|
-
-
MasterPlanSoftware


- Joined on 11-10-2006
- Posts 108
|
Re: There has to be a better way...
dhromed:Why the fuck doesn't it do that for me? Because you are not special enough.
|
|
-
-
dhromed


- Joined on 04-13-2005
- Dutchland
- Posts 2,614
|
Re: There has to be a better way...
MasterPlanSoftware:Because you are not special enough.
:'(
— Flurp.
|
|
-
-
dlikhten


- Joined on 09-27-2007
- New York Citeyah
- Posts 670
|
Re: There has to be a better way...
belgariontheking: Of course there's a better way. I'll pseudocode it. 1) Generate random string of characters s.
2) if s = type goto 4 3) goto 1 4) ??? 5) Profit! Hey MasterPlanSoftware, whats step 4?
Code is like a box of chocolates. You never know who stuck a turd in there and why. The Stupidest Man On EarthSSDS Bug: Program should not start up
|
|
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 229
|
Re: There has to be a better way...
I was dealing with this same issue. I made a simple class that is used like so:
string strValue = new ValueControl( myControl ).Value;
The class figures out if it's using Text, Checked, or SelectedValue based on the kind of control. Then I was able to write a loop for all controls in a form to collect all the values, without having to do any special-cases.
Edit: Yes, I know, a function could have done the same. The class actually did a few more things than that, though.
|
|
-
-
Jonathan Holland


- Joined on 01-10-2008
- Posts 163
|
Re: There has to be a better way...
joe.edwards@imaginuity.com:I was dealing with this same issue. I made a simple class that is used like so:
string strValue = new ValueControl( myControl ).Value;
The class figures out if it's using Text, Checked, or SelectedValue based on the kind of control. Then I was able to write a loop for all controls in a form to collect all the values, without having to do any special-cases.
Edit: Yes, I know, a function could have done the same. The class actually did a few more things than that, though. Thats kind of backwards from what I need. I'm setting the text property, not retrieving it. This code is for this stupid localization system. Basicly, translators can use a tool to translate all the pages, which stores all text in a couple of database tables (mapping PageID and ControlID). Then when the page is rendered, all of the text gets swapped out with the proper localization.
I fought long and hard for a different solution, but this is what we have to build. I did build in cacheing and defaulting to english without running the sproc, so it will only be a slow site for our global customers.
|
|
-
-
arty


- Joined on 01-09-2007
- Posts 105
|
Re: There has to be a better way...
Jonathan Holland:if (foundControl != null) { Type controlType = currentControl.GetType(); if (controlType == typeof(Label)) {
This kind of crud is all over the UI for a tool I do some maintenance on. What's worse is that the types are most often types the original author wrote together, and gave each and every one a non-virtual copy of the same property.
|
|
-
-
Morbii


- Joined on 10-21-2006
- Posts 119
|
Re: There has to be a better way...
Perhaps I'm reading the docs wrong, but doesn't System.Web.UI.WebControls.Label inherit from System.Windows.Forms.Label to get its Text member, which is then inherited from System.Windows.Forms.Control. So... can't you just cast it as a System.Windows.Forms.Control and use the Text property?
e.g.
System.Windows.Forms.Control control = currentControl as System.Windows.Forms.Control;
if(control!=null)
control.Text = foundControl[_displayLabelColumn].ToString();
|
|
-
-
Jonathan Holland


- Joined on 01-10-2008
- Posts 163
|
Re: There has to be a better way...
Morbii:Perhaps I'm reading the docs wrong, but doesn't System.Web.UI.WebControls.Label inherit from System.Windows.Forms.Label to get its Text member, which is then inherited from System.Windows.Forms.Control. So... can't you just cast it as a System.Windows.Forms.Control and use the Text property?
e.g.
System.Windows.Forms.Control control = currentControl as System.Windows.Forms.Control;
if(control!=null)
control.Text = foundControl[_displayLabelColumn].ToString();
This is WebForms, not winForms. System.Web.UI.WebControls.Label is a subclass of System.Web.UI.Control. This allows Label to become a part of ControlCollection. However, when iterating a ControlCollection: foreach (Control c in ControlCollection) you end up with the parent class, Control, not the subclass, hence having to check the type and then cast it again. Control only exposes common virtual properties, since text is only on a handfull of subcontrols, it is not a property of control. Ugly.
|
|
-
-
dlikhten


- Joined on 09-27-2007
- New York Citeyah
- Posts 670
|
Re: There has to be a better way...
AH the beauty of dynamic languages... As long as you follow convention then figuring out who you are is unnecessary... In
Flex2 depending on the component you can tell if it has a label or
"text" (yes there is a difference, no i don't know why couldn't they
jsut use text property everywhere)... So you can just see
.hasOwnProperty("text") or .hasOwnProperty("label") and then just poof
set that property. In strongly typed languages I suppose you can make a class that just takes the object, introspects it, and returns a function for setting the Text property (unless its not a function in which case its even worse), if null is returned then wah-wah, otherwise just set it. and no more giant if blocks. (go Ruby!)
Code is like a box of chocolates. You never know who stuck a turd in there and why. The Stupidest Man On EarthSSDS Bug: Program should not start up
|
|
-
-
Jonathan Holland


- Joined on 01-10-2008
- Posts 163
|
Re: There has to be a better way...
dlikhten: AH the beauty of dynamic languages... As long as you follow convention then figuring out who you are is unnecessary... In
Flex2 depending on the component you can tell if it has a label or
"text" (yes there is a difference, no i don't know why couldn't they
jsut use text property everywhere)... So you can just see
.hasOwnProperty("text") or .hasOwnProperty("label") and then just poof
set that property. In strongly typed languages I suppose you can make a class that just takes the object, introspects it, and returns a function for setting the Text property (unless its not a function in which case its even worse), if null is returned then wah-wah, otherwise just set it. and no more giant if blocks. (go Ruby!)
Programming via Reflection could be implemented here, but I think that would make a slow solution even slower. What I really want to do is this: Type controlType = currentControl.getType() (currentControl as controlType).text = Foo; Yes, that looks like I'm casting an object to its own type, so it won't work. But it makes sense in my head dammit.
|
|
-
-
belgariontheking


- Joined on 08-20-2007
- Cincinnati, OH, USA
- Posts 1,365
|
Re: There has to be a better way...
AbbydonKrafts: belgariontheking:Filed under: FUCKING HELL WHY DOES IT DELETE TWO CHARACTERS WHEN I HIT BACKSPACE ONCE?
So it's not just me. I brought up a bunch of tag flaws in another sidebar post, and this was one of them. I'm gonna use that tag now.
I don't think it was clear in my tag that this happens when I type my post, not my tag. I can't wait until this becomes part of the tag cloud.
I guess I'm back.
Please continue to spam the addresses below.
PLEASE SPAM: jtobin@ohioinstituteofhealthcareers.edu jtobin@ohiobusinesscollege.edu
|
|
-
-
dogbrags


- Joined on 01-22-2008
- Posts 4
|
Re: There has to be a better way...
create a Interface, call it HasLabelControl. There does not need to be any methods in the interface. Create a subclass of each control you're interested in (Literal, LinkButton, Label, etc). For example, ourLiteral, ourLinkButton, etc. For a class like ourLiteral, you'd inherit from Literal and HasLabelControl. The ourLiteral class does not need any members, because all the functionality is in Literal already. Use ourLiteral where you'd use a Literal. (and the same for the others...) Then your code can be (I think, you'd have to check the syntax) HasControl c = foundControl as HasLabelControl; if (c != null) c.Text = ..........;
|
|
-
-
dlikhten


- Joined on 09-27-2007
- New York Citeyah
- Posts 670
|
Re: There has to be a better way...
Jonathan Holland: dlikhten: AH the beauty of dynamic languages... As long as you follow convention then figuring out who you are is unnecessary... In
Flex2 depending on the component you can tell if it has a label or
"text" (yes there is a difference, no i don't know why couldn't they
jsut use text property everywhere)... So you can just see
.hasOwnProperty("text") or .hasOwnProperty("label") and then just poof
set that property. In strongly typed languages I suppose you can make a class that just takes the object, introspects it, and returns a function for setting the Text property (unless its not a function in which case its even worse), if null is returned then wah-wah, otherwise just set it. and no more giant if blocks. (go Ruby!)
Programming via Reflection could be implemented here, but I think that would make a slow solution even slower. What I really want to do is this: Type controlType = currentControl.getType() (currentControl as controlType).text = Foo; Yes, that looks like I'm casting an object to its own type, so it won't work. But it makes sense in my head dammit.
Just letting you know... This is solved by using.... get this... Inheritence... If all your objects extends something called UIComponent and part of UI component is a text property... or say ControlComponent in your case. Just cast to ControlComponent and use ControlComponent.Text = "xxx makes the world go round". You can use interfaces instead if you don't want to change behavior. Using interfaces you can isolate behaviors and just check if its an instance of that interface.
Code is like a box of chocolates. You never know who stuck a turd in there and why. The Stupidest Man On EarthSSDS Bug: Program should not start up
|
|
-
-
dlikhten


- Joined on 09-27-2007
- New York Citeyah
- Posts 670
|
Re: There has to be a better way...
belgariontheking: AbbydonKrafts: belgariontheking:Filed under: FUCKING HELL WHY DOES IT DELETE TWO CHARACTERS WHEN I HIT BACKSPACE ONCE?
So it's not just me. I brought up a bunch of tag flaws in another sidebar post, and this was one of them. I'm gonna use that tag now.
I don't think it was clear in my tag that this happens when I type my post, not my tag. I can't wait until this becomes part of the tag cloud. Let the revolution begin!
Code is like a box of chocolates. You never know who stuck a turd in there and why. The Stupidest Man On EarthSSDS Bug: Program should not start up
|
|
-
-
Jonathan Holland


- Joined on 01-10-2008
- Posts 163
|
Re: There has to be a better way...
dogbrags:create a Interface, call it HasLabelControl. There does not need to be any methods in the interface. Create a subclass of each control you're interested in (Literal, LinkButton, Label, etc). For example, ourLiteral, ourLinkButton, etc. For a class like ourLiteral, you'd inherit from Literal and HasLabelControl. The ourLiteral class does not need any members, because all the functionality is in Literal already. Use ourLiteral where you'd use a Literal. (and the same for the others...) Then your code can be (I think, you'd have to check the syntax) HasControl c = foundControl as HasLabelControl; if (c != null) c.Text = ..........;
I understand how interfaces work. Doing that would involve rewriting all of the pages that use these controls to use the new controls that implement IOverComplicated.
|
|
-
-
Morbii


- Joined on 10-21-2006
- Posts 119
|
Re: There has to be a better way...
Jonathan Holland:
Morbii:
Perhaps I'm reading the docs wrong, but doesn't System.Web.UI.WebControls.Label inherit from System.Windows.Forms.Label to get its Text member, which is then inherited from System.Windows.Forms.Control. So... can't you just cast it as a System.Windows.Forms.Control and use the Text property?
e.g.
System.Windows.Forms.Control control = currentControl as System.Windows.Forms.Control;
if(control!=null)
control.Text = foundControl[_displayLabelColumn].ToString();
This is WebForms, not winForms.
I realized that this was WebForms, but it was my error anyway. I was actually looking at the Microsoft.Office.Tools.Excel.Controls namespace whose Label class does derive from System.Windows.Forms and I assumed I was looking at System.Windows.WebForms. Oops :o
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 713
|
Re: There has to be a better way...
I don't think there is any "clean" solution to this if the text properties are really on independant classes and only "happen" to share the same name. After all, the compiler would strip out the names anyway, so there is no kind of logical relation between the properties. (Reflection notwithstanding at least) Doesn't have C++ a kind of special pointer to object properties though? Maybe there is an equivalent in X.NET that you could use somehow?
|
|
-
-
AbbydonKrafts


- Joined on 11-21-2006
- Carrollton, GA, USA
- Posts 1,022
|
Re: There has to be a better way...
belgariontheking:I don't think it was clear in my tag that this happens when I type my post, not my tag. I can't wait until this becomes part of the tag cloud.
Ahh.. that's even more strange. If you are using the tinyMCE editor.. well.. it's a loon, so anything's possible.
Join us at #TDWTF on irc.slashnet.org !
|
|
-
-
cvi


- Joined on 03-10-2006
- Posts 19
|
Re: There has to be a better way...
PSWorx:Doesn't have C++ a kind of special pointer to object properties though? Maybe there is an equivalent in X.NET that you could use somehow?
I don't think the C++ pointer-to-member is valid for different objects though (possibly by fluke if the classes happen to be layout compatible w.r.t to the member you're accessing, but I would not count on that, event if you had written those classes on your own).
A different solution could be:
- create a map/hash_table/something from Type -> Function, where the function sets the .Text property for the selected type
- use your_map[ typeof(foo) ]( foo, n
| | |
|