Here is the problem: WebControl.TextBox aaaab, aaaac,aaaad,aaaae; aaaab.visible = true; aaaac.visible = true; aaaad.visible = true; aaaae.visible = true; ... and I need to run above batch a number of times, now, I want to build a function something like void
setVisible(string str, bool val) { (str+"b").visible = val; (str+"c").visible = val; ... } does anybody know how to accomplish this? this soulds like trivil, to generate statement dymamically, but i do not know how to do it. thanks for your help. ^^
::this soulds like trivil, to generate statement dymamically It IS trivial to GENERATE statements dynamically. Just you dont try to do this. WHat you try to do is EXECUTE statementy dynamically. What for? Does it make any sense? I mean, I never stumbled over
the need for this. How does it come you: * Have only the NAME of the textbox, but nothing else about it * Can not use an iterator over all controls and their subcontrols (recursive) to identify all WebControls, possibly filter the name? With the latter you
could basically do something like this: void setVisible (COnstolrCollection Controls, string str, bool val) { foreach (Control c in Controls) { WebControl.Textbox box = c as WebControl.TextBox; if (box != null) { if (box.Name.StartsWith(str) { box.Visible=val;
} } else { SetVisible (c.Controls, str, val); } } } Not the fastest way (tons of string comparisons). Makes possible seense to pre-compute the list once and then just iterate over it.
It would be much more readable, and maintainable. If ever another control is included in the set it is a matter of adding it to the setVisible function and the rest
is taken care of. Hope this helps
::Personally I would rather write a function that can be called to handle the group. He propably can not. THere are a ton of reasons for this, like the controls are dynamically added from a database :-) Stuff like a survey system would work like this.
::WebControl.TextBox aaaab, aaaac,aaaad,aaaae; I did consider what you are saying and agree, but the above piece of code seemed to imply statically defined controls. Sorry if my answer was misleading.
ysm79
Member
165 Points
33 Posts
generate statement dynamically
Nov 11, 2003 01:13 AM|LINK
thona
Member
20 Points
2923 Posts
Re: generate statement dynamically
Nov 11, 2003 05:45 AM|LINK
taylorza
Member
325 Points
65 Posts
Re: generate statement dynamically
Nov 11, 2003 10:20 AM|LINK
private void setVisible( bool visible ) { aaaab.Visible = visible; aaaac.Visible = visible; aaaad.Visible = visible; aaaae.Visible = visible; }It would be much more readable, and maintainable. If ever another control is included in the set it is a matter of adding it to the setVisible function and the rest is taken care of. Hope this helpsMy Blog
thona
Member
20 Points
2923 Posts
Re: generate statement dynamically
Nov 11, 2003 10:42 AM|LINK
taylorza
Member
325 Points
65 Posts
Re: generate statement dynamically
Nov 11, 2003 11:02 AM|LINK
My Blog