Dynamic variable/object name reference. how to do in VB.NET?

S

simon

hello,
I have a form that has 10 dropdown lists in one section
each of the dropdowns contain the same data

what i'm looking to do is that when a user selects a value from one
dropdown, change the value to null/nothing for any other dropdowns
that may have had the same value as the one that was just selected.
and have all the dropdowns use the same "selected index changed" code
behind.

so if ddl1 was changed, fire the code behind to sweep thru all the
dropdowns and reset it if there was a match.

was thinking of pseudo code for this would be something like....
assume all the dropdowns are named dd1,dd2,dd3,dd4,dd5,dd6,etc


while I < 11
loop
if newly selected value = "ddl"+I.selectedindex.text then
"ddl"+I.selectedindex = 0
end if
end loop

in javascript there is a function eval() whose parameter is a string.
so results of that call eval("string_to_evaluate") gets executed or
interpreted as if you had type the string out directly.

also, what would be the line of code to tell which dropdown the user
changed and cause the codebehind to trigger? what I was calling
"newly selected value" above

thanks again for any help!
 
C

Chris

simon said:
hello,
I have a form that has 10 dropdown lists in one section
each of the dropdowns contain the same data

what i'm looking to do is that when a user selects a value from one
dropdown, change the value to null/nothing for any other dropdowns
that may have had the same value as the one that was just selected.
and have all the dropdowns use the same "selected index changed" code
behind.

so if ddl1 was changed, fire the code behind to sweep thru all the
dropdowns and reset it if there was a match.

was thinking of pseudo code for this would be something like....
assume all the dropdowns are named dd1,dd2,dd3,dd4,dd5,dd6,etc


while I < 11
loop
if newly selected value = "ddl"+I.selectedindex.text then
"ddl"+I.selectedindex = 0
end if
end loop

in javascript there is a function eval() whose parameter is a string.
so results of that call eval("string_to_evaluate") gets executed or
interpreted as if you had type the string out directly.

also, what would be the line of code to tell which dropdown the user
changed and cause the codebehind to trigger? what I was calling
"newly selected value" above

thanks again for any help!

Well you can do your "Eval" thing using reflection. Do a search on "get
control by name" and you'll find some examples about it. However I'd so
something like this.

1. Create an array
Dim A(NumofControls-1) as Array

2. Create your handler code
Private sub Combobox_SelectionChanged(Sender as object, e as EventArgs)
handles DDL1.SelectionChanged, DDL2.SelectionChanged.....
End Sub

3. Make your loop
dim C as ComboBox
dim ChangeBox as ComboBox = DirectCast(Sender, ComboBox)
For ii as integer = 0 to NumofControls-1
C = DirectCast(A(ii), ComboBox)
'Make sure it is not the sender
if not ChangeBox is C then
'Do your check here
End if
Next ii

This code hasn't be checked but the idea is right. Also you'll see here
how do know which object caused the event to fire with this code:
dim ChangeBox as ComboBox = DirectCast(Sender, ComboBox)

Hope is helps
Chris
 
A

AMDRIT

That is fine for a round trip solution, however, couldn't this solution be
handled at the client. There is no reason to ask the server what to do,
when the client has all of the data.

<select id="box2" name="box2" onchange="cbochanged(this);">

<script language="jscript">

function cbochanged(cbo)
{

var oObject = document.all.item("Select");

for (i = 0; i < oObject.length; i++)
{
if (cbo.id != oObject.id)
{
oObject.selectedIndex=-1;
}
}

}
</script>
 
C

Chris

AMDRIT said:
That is fine for a round trip solution, however, couldn't this solution be
handled at the client. There is no reason to ask the server what to do,
when the client has all of the data.

<select id="box2" name="box2" onchange="cbochanged(this);">

<script language="jscript">

function cbochanged(cbo)
{

var oObject = document.all.item("Select");

for (i = 0; i < oObject.length; i++)
{
if (cbo.id != oObject.id)
{
oObject.selectedIndex=-1;
}
}

}
</script>

Ah, well it is helpful to know that you are using ASP.NET. I thought
your javascript reference was just to illistrate an idea. That's what I
get for being so winform bais.

There is a document.getElementById("DDL1") This should work for you
loop as you can just append the number to "DDL".

My javascript knowledge is very limited though, so I can't help you
beyond this.

Chris
 
H

Herfried K. Wagner [MVP]

simon said:
I have a form that has 10 dropdown lists in one section
each of the dropdowns contain the same data

what i'm looking to do is that when a user selects a value from one
dropdown, change the value to null/nothing for any other dropdowns
that may have had the same value as the one that was just selected.
and have all the dropdowns use the same "selected index changed" code
behind.

so if ddl1 was changed, fire the code behind to sweep thru all the
dropdowns and reset it if there was a match.

was thinking of pseudo code for this would be something like....
assume all the dropdowns are named dd1,dd2,dd3,dd4,dd5,dd6,etc


while I < 11
loop
if newly selected value = "ddl"+I.selectedindex.text then
"ddl"+I.selectedindex = 0
end if
end loop

\\\
Dim ComboBoxes() As ComboBox = _
{ _
Me.ComboBox1, Me.ComboBox2, Me.ComboBox3 _
}
ComboBoxes(i).SelectedIndex = 0
///
 
A

AMDRIT

i just asumed asp.net because simon made a reference to javascript in his
post. Your solution works either way.
 
S

simon

thank you all for your replies. very much appreciate it....
i'm trying to stick with just using VB.net, just because i'm new to it
and want to learn that route. have used other languages before, this
is my new quest :)

i'm going to try using both Chris and Herfrieds solutions (get a more
well rounded understanding of the concepts and syntax)

Herfried, what does the "Me." in "Me.ComboBox1" reference?

Chris, when you define an array in vb.net, you do not need to specify
the type of object it will hold?

"Dim A(NumofControls-1) as Array"


thanks again!
 
H

Herfried K. Wagner [MVP]

simon said:
i'm going to try using both Chris and Herfrieds solutions (get a more
well rounded understanding of the concepts and syntax)

Herfried, what does the "Me." in "Me.ComboBox1" reference?

It refers to the instance of the form. You can either type 'Me.ComboBox1'
or 'ComboBox1', which are semantically identical.
Chris, when you define an array in vb.net, you do not need to specify
the type of object it will hold?

"Dim A(NumofControls-1) as Array"

Use 'Dim A(...) As Object' instead.
 
C

Chris

simon said:
thank you all for your replies. very much appreciate it....
i'm trying to stick with just using VB.net, just because i'm new to it
and want to learn that route. have used other languages before, this
is my new quest :)

i'm going to try using both Chris and Herfrieds solutions (get a more
well rounded understanding of the concepts and syntax)

Herfried, what does the "Me." in "Me.ComboBox1" reference?

Chris, when you define an array in vb.net, you do not need to specify
the type of object it will hold?

"Dim A(NumofControls-1) as Array"


thanks again!

On Mon, 16 Jan 2006 20:58:26 +0100, "Herfried K. Wagner [MVP]"

Me references the current object. If you are in a form, it references
the form. It is not needed, just there for readabilty.

An array will accept an object type. That is why you see me using the
directcast keyword to cast it out of object type back into a combobox.

Chris
 
S

simon

thank you guys once again for the quick responses.
chris, in order to use the funtions that are part of the combobox
object, i'd have to cast it out from object to combobox, correct?
chris/herfried - use Object instead of Array, when creating the array
of comboboxes? more accurate?
"Use 'Dim A(...) As Object' instead."

thanks again!
 
C

Chris

simon said:
thank you guys once again for the quick responses.
chris, in order to use the funtions that are part of the combobox
object, i'd have to cast it out from object to combobox, correct?
chris/herfried - use Object instead of Array, when creating the array
of comboboxes? more accurate?
"Use 'Dim A(...) As Object' instead."

thanks again!

Since you are new to programming let's leave it at you should cast it
out. It will help save you problems down the line. Also, put "option
strict on" and "option explict on" at the top of your file. This will
help save you errors. It also forces you to do the casting.

Chris
 
S

simon

hello chris. thanks again for the tips. been programming for many
years, but brand new to vb/asp and .net
i was assuming Object was the top level class that all classes inherit
from. therefore you would need to cast your object to the appropriate
class in order to have access to methods available at the more
specific level. trying to get a hold of the .net framework concepts
and don't really want to assume anything - especially if i assume it
incorrectly :)
I'll look up those file lines you recommended to learn more about
them.
thanks again for taking the time to reply. people in this group are
much more resonsive than in the forums and its appreciated!
 

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