VB.Net Dynamic Statements

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

theGarynator

Thread Starter
Joined
Jan 15, 2006
Messages
5
So i'm trying to figure out how to write a dynamic statement in VB.Net... i know how to write them in other languages, but i can't figure out the syntax in vb.net... i've searched everywhere. Here's what i'm trying to do with it :

clearing text boxes and labels in the form... there are 9 of each.

text box names : txtTotal1, txtTotal2, etc
label names : lblStment1, lblStment2, etc.

here's what i would have thought the code would have been(or at least close to this) :

dim y as integer = 1
for x = 0 to 9
txtTotal(y).text = ""
lblStment(y).text = ""
y += 1
next

VB underlines txtTotal and lblStment like it's underlined on here and gives me the error "name 'txtTotal' has not been declared" and the same for the label.

i've tried a lot of variances of that up to/including setting a string to the statement(but i couldn't figure out how to excecute a statment stored in a string either). am i missing some special syntax or what?

Thanks for your time,
Gary
 

f1_champ

Banned
Joined
Nov 6, 2005
Messages
713
You have named your textboxes and labels as

text box names : txtTotal1, txtTotal2, etc
label names : lblStment1, lblStment2, etc.
and in the code you are using the name

"lblStment" and "txtTotal"

Do you notice that you are using different names to refer these objects ?

change
txtTotal(y).text = ""
lblStment(y).text = ""

to
txtTotal1(y).text = ""
lblStment1(y).text = ""

and it should work fine now
 

theGarynator

Thread Starter
Joined
Jan 15, 2006
Messages
5
the (y) is a variable(an integer) that is replacing the "1" in "txtTotal1" and in "lblStment1"... it is going to change to "2" in the second iteration and "3" in the third, etc...

The point is to replace all of this code :

----------------------------------------
'clears all text boxes
txtTotal1.Text = ""
txtTotal2.Text = ""
txtTotal3.Text = ""
txtTotal4.Text = ""
txtTotal5.Text = ""
txtTotal6.Text = ""
txtTotal7.Text = ""
txtTotal8.Text = ""
txtTotal9.Text = ""
'clears all labels
lblStment1.Text = ""
lblStment2.Text = ""
lblStment3.Text = ""
lblStment4.Text = ""
lblStment5.Text = ""
lblStment6.Text = ""
lblStment7.Text = ""
lblStment8.Text = ""
lblStment9.Text = ""
---------------------------------------

with this :

------------------------------------------
dim y as integer = 1
for x = 0 to 9
txtTotal(y).text = ""
lblStment(y).text = ""
y += 1
next
 
Joined
Aug 5, 2005
Messages
3,086
Disclaimer: I don't know .net.

The code you have is syntactically illogical. In what you think should work, you have arrays. But your controls are not in a collection, they're just similarly named. Normally you'd use something like Eval to get the type of functionality you want, but you can't use Eval to point to a variable; it only works for procedure names.

In VBA, you'd do something like this airware:

Code:
Dim ctr As Container
Dim ctl As Control
Dim i   As Integer

i = 1

For Each ctl In ctr.Controls
  With ctl
    If .Name = "txtTotal" & i Then
      .Text = vbNullString
      i = i + 1
    End If
  End With
Next ctl
Or, you could do something like this in VBA:

Code:
Dim ctr As Container
Dim i   As Integer

For i = 1 To 9
  With ctr
    .Controls("txtTotal" & i).Text = vbNullString
    .Controls("lblStment" & i).Text = vbNullString
  End With
Next i
The difference is your txtTotal(y) is a collection (specifically, an array), but the collection doesn't exist. You have a virtual collection based on object names, so you have to use the Name property somehow. The second example is probably closer to what you want. It uses the Name property implicitly via the member's named index in its parent collection.

I don't know why you're dealing with the Text property instead of the Value property, but I don't know .net.

HTH, if not, disregard.

chris.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top