|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Late Binding QuestionFor simplicity, if I have say, 20 TextBoxes and 15 Labels on a Form that regularly require clearing (ie. "") or setting to the same value (eg. "---"), would it be frowned upon to use the following code - Dim sStr as String = "" '(or "---") Dim Txt As Object Dim TxtArray As Object() = {TextBox1, TextBox2,.. Label1, Label2,..} For Each Txt In TxtArray Txt.Text = sStr Next or should I separate the two Object Types such as - Dim TextBoxArray As Object() = {TextBox1, TextBox2,..} Dim LabelArray As Object() = {Label1, Label2,..} and then iterate through different For Each..Next loops - or - don't do any of the above and explicitly assign the Text value of each Object such as - Dim sStr as String = "" '(or "---") TextBox1.Text = sStr: TextBox2.Text = sStr... Label1.Text = sStr: Label2.Text = sStr... I'd be interested to read your comments or suggestions and information on how others would address this one. ShaneO There are 10 kinds of people - Those who understand Binary and those who don't. ShaneO wrote:
Show quoteHide quote > A VB.NET 2005 question - Late binding is unnecessary here. Both, TextBox and Label inherit from> > For simplicity, if I have say, 20 TextBoxes and 15 Labels on a Form that > regularly require clearing (ie. "") or setting to the same value (eg. > "---"), would it be frowned upon to use the following code - > > Dim sStr as String = "" '(or "---") > Dim Txt As Object > Dim TxtArray As Object() = {TextBox1, TextBox2,.. Label1, Label2,..} > For Each Txt In TxtArray > Txt.Text = sStr > Next > > or should I separate the two Object Types such as - > > Dim TextBoxArray As Object() = {TextBox1, TextBox2,..} > Dim LabelArray As Object() = {Label1, Label2,..} > > and then iterate through different For Each..Next loops > > - or - > > don't do any of the above and explicitly assign the Text value of each > Object such as - > > Dim sStr as String = "" '(or "---") > TextBox1.Text = sStr: TextBox2.Text = sStr... > Label1.Text = sStr: Label2.Text = sStr... > > > I'd be interested to read your comments or suggestions and information > on how others would address this one. > > ShaneO Control. In fact, that is where their Text property comes from. So, just make your array of type Control... Dim controlArray As Control () = {TextBox1, ... Label1,...} For Each txt in controlArray txt.Text = string.empty next -- Tom Shelton [MVP] Tom Shelton wrote:
> Doh!.... Thanks Tom, I should have known this!!> Late binding is unnecessary here. Both, TextBox and Label inherit from > Control. In fact, that is where their Text property comes from. So, > just make your array of type Control... > ShaneO There are 10 kinds of people - Those who understand Binary and those who don't. Shane,
Just some comments on what you wrote inline. > This is almost never necessary, by most regulars in this newsgroup seen as > Dim sStr as String = "" '(or "---") bad practise > Dim Txt As Object Try to avoid using an object> Dim TxtArray As Object() = {TextBox1, TextBox2,.. Label1, Label2,..} Dim TxtArray as Control() = etc> For Each Txt In TxtArray For Each Txt as Control in TxtArrayTxt.Text = "" Next Text is a property from Control so works with every one, be aware that in a checkbox by instance Text is the description and value the content. Be aware that with controls on a control you have to do this recursive http://www.vb-tips.com/default.aspx?ID=8ff290d4-5c16-4cef-ba06-56e3599238c1 I thought that I have answered with this as well your other questions about this? Cor Cor Ligthert [MVP] wrote:
> Thank-you Cor. As I wrote to Tom, I should have known about using > For Each Txt as Control in TxtArray > Txt.Text = "" > Next > > Text is a property from Control so works with every one, be aware that in a > checkbox by instance Text is the description and value the content. > > Be aware that with controls on a control you have to do this recursive > http://www.vb-tips.com/default.aspx?ID=8ff290d4-5c16-4cef-ba06-56e3599238c1 > > I thought that I have answered with this as well your other questions about > this? > "Control". I also appreciate your comments on the Dim...Object and the information provided in your link. ShaneO There are 10 kinds of people - Those who understand Binary and those who don't.
Datagrid with VB 2005
Datagrid problem large arrays and system.outofmemoryexception DataBinding and Combo Boxes Doing some extra task while saving databind Controls in VB.Net 2005 ? Dynamically Loading Programs select all text when textbox1 is clicked Auto-stop DUN via VB.Net? Problems with TCP Listener Oledb VS Sql. Oledb works with Sql Server; Sql doesn't...why |
|||||||||||||||||||||||