Late Binding Question

S

ShaneO

A VB.NET 2005 question -

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

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
T

Tom Shelton

ShaneO said:
A VB.NET 2005 question -

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

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...

Dim controlArray As Control () = {TextBox1, ... Label1,...}
For Each txt in controlArray
txt.Text = string.empty
next
 
C

Cor Ligthert [MVP]

Shane,

Just some comments on what you wrote inline.
Dim sStr as String = "" '(or "---")

This is almost never necessary, by most regulars in this newsgroup seen as
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 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?

Cor
 
S

ShaneO

Tom said:
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...
Doh!.... Thanks Tom, I should have known this!!

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
S

ShaneO

Cor said:
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?
Thank-you Cor. As I wrote to Tom, I should have known about using
"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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top