Home All Groups Group Topic Archive Search About

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

Author
16 Jan 2006 5:32 PM
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!

Author
16 Jan 2006 6:11 PM
Chris
simon wrote:
Show quoteHide quote
> 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
Author
16 Jan 2006 6:32 PM
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>


Show quoteHide quote
"Chris" <no@spam.com> wrote in message
news:ekH$ugsGGHA.2000@TK2MSFTNGP15.phx.gbl...
> simon wrote:
>> 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
Author
16 Jan 2006 7:17 PM
Chris
AMDRIT wrote:
Show quoteHide quote
> 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>
>
>
> "Chris" <no@spam.com> wrote in message
> news:ekH$ugsGGHA.2000@TK2MSFTNGP15.phx.gbl...
>
>>simon wrote:
>>
>>>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
>
>
>

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
Author
16 Jan 2006 7:59 PM
AMDRIT
i just asumed asp.net because simon made a reference to javascript in his
post.  Your solution works either way.


Show quoteHide quote
"Chris" <no@spam.com> wrote in message
news:eLcueFtGGHA.1180@TK2MSFTNGP09.phx.gbl...
> AMDRIT wrote:
>> 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>
>>
>>
>> "Chris" <no@spam.com> wrote in message
>> news:ekH$ugsGGHA.2000@TK2MSFTNGP15.phx.gbl...
>>
>>>simon wrote:
>>>
>>>>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
>>
>>
>>
>
> 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
Author
16 Jan 2006 7:58 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"simon" <m*@here.com> schrieb:
> 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
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
16 Jan 2006 9:33 PM
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!

On Mon, 16 Jan 2006 20:58:26 +0100, "Herfried K. Wagner [MVP]"
Show quoteHide quote
>> 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
>///
Author
16 Jan 2006 9:40 PM
Herfried K. Wagner [MVP]
"simon" <m*@here.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
16 Jan 2006 9:42 PM
Chris
simon wrote:
Show quoteHide quote
> 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]"
>
>>>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
>>///
>
>

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
Author
16 Jan 2006 9:53 PM
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!


On Mon, 16 Jan 2006 16:42:35 -0500, Chris <no@spam.com> wrote:
Show quoteHide quote
>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.
Author
16 Jan 2006 9:58 PM
Chris
simon wrote:
Show quoteHide quote
> 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!
>
>
> On Mon, 16 Jan 2006 16:42:35 -0500, Chris <no@spam.com> wrote:
>
>>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.

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
Author
17 Jan 2006 1:48 AM
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!

On Mon, 16 Jan 2006 16:58:35 -0500, Chris <no@spam.com> wrote:
Show quoteHide quote
>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.