Dynamic Variables

C

Cuperman

Hi All,

Any ideas on how I could make the code below less verbose and more
scalable (more iterations are needed)...My thoughts are dynamically
named variables but I don't know how to do this (is it possible?)

VB.Net ideas preferred,
Many thanks,
Mark

=================================================

Select Case i
Case 1
Dim AttBtn1 As New ImageButton

With AttBtn1
.CssClass = "Att_Button " +
node.Attributes("type").Value
End With

e.Item.Cells("2").Controls.Add(AttBtn1)

Case 2
Dim AttBtn2 As New ImageButton
With AttBtn2
.CssClass = "Att_Button " +
node.Attributes("type").Value
End With

e.Item.Cells("2").Controls.Add(AttBtn2)

Case 3
Dim AttBtn3 As New ImageButton
With AttBtn3
.CssClass = "Att_Button " +
node.Attributes("type").Value
End With

e.Item.Cells("2").Controls.Add(AttBtn3)

'repeat ad infinitum

End Select
===================================================
 
P

Patrice

What is the "i" variable ?

Here it seems to me this is the exact same code in all branch so why have
branches ? You can perfectly use the same variable for several controls one
after the other. If you need to keep a reference to those controls you could
use an array.
 
C

Cuperman

Hi Patrice
The i is a counter. I was hoping to use a Dim AttBtn + i as
ImageButton logic.

If I can use the same variable name again and again, then i probably
havn't got a problem at all.

Thanks for your help.
Mark
 
J

Jeroen Vandezande

Cuperman said:
Hi All,

Any ideas on how I could make the code below less verbose and more
scalable (more iterations are needed)...My thoughts are dynamically
named variables but I don't know how to do this (is it possible?)

VB.Net ideas preferred,
Many thanks,
Mark

=================================================

If don't know VB.net but I give it a try :)
Why don't you try to put this in the counter loop:


Dim AttBtn As New ImageButton
AttBtn.CssClass = "Att_Button " +
node.Attributes("type").Value
e.Item.Cells("2").Controls.Add(AttBtn)



Best Regards,

Jeroen Vandezande.
 
P

Patrice

No problem. Once the control is created you add it to the control
collection. From there you can reuse this variable for another control. The
control is still available as it has been added to the list of controls.
 

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