Using TypeOf?

Discussion in 'ASP .Net' started by Leon, Nov 1, 2004.

  1. Leon

    Leon Guest

    I have a program that fill 6 textbox on button click, but when using TypeOf
    my program is trying to also fill label controls which throws my array out
    of range. how can use TypeOf to only read textbox Control?

    Piece of Code:

    Dim I As Integer

    For I = 0 To Me.Controls.Count - 1

    If (TypeOf Controls(I) Is TextBox) Then

    Dim tbox As TextBox = CType(Controls(I), TextBox)

    tbox.Text = arr1(I)

    End If

    Next
    Leon, Nov 1, 2004
    #1
    1. Advertising

  2. Leon

    Henri Guest

    It's not a matter of TypeOf, it's the way you're using "i" that is wrong:

    Dim i As Integer = 0

    For j As Integer = 0 To Controls.Count - 1
    Dim ctrl As Control = Controls(j)
    If TypeOf ctrl Is TextBox Then

    DirectCast(ctrl, TextBox).Text = arr1(i)

    i += 1

    End If

    Next


    "Leon" <> a écrit dans le message de
    news:...
    > I have a program that fill 6 textbox on button click, but when using

    TypeOf
    > my program is trying to also fill label controls which throws my array out
    > of range. how can use TypeOf to only read textbox Control?
    >
    > Piece of Code:
    >
    > Dim I As Integer
    >
    > For I = 0 To Me.Controls.Count - 1
    >
    > If (TypeOf Controls(I) Is TextBox) Then
    >
    > Dim tbox As TextBox = CType(Controls(I), TextBox)
    >
    > tbox.Text = arr1(I)
    >
    > End If
    >
    > Next
    >
    >
    >
    Henri, Nov 1, 2004
    #2
    1. Advertising

  3. Leon

    Henri Guest

    Better:

    Dim i As Integer = 0

    For Each ctrl As Control In Controls

    If TypeOf ctrl Is TextBox Then

    DirectCast(ctrl, TextBox).Text = arr1(i)

    i += 1

    End If

    Next
    Henri, Nov 1, 2004
    #3
  4. Leon

    Leon Guest

    Thanks So Much Henri,
    That most definitely solve my problem. Could you please explain to me in
    details what I was doing wrong.
    Thanks again!

    "Henri" <> wrote in message
    news:...
    > Better:
    >
    > Dim i As Integer = 0
    >
    > For Each ctrl As Control In Controls
    >
    > If TypeOf ctrl Is TextBox Then
    >
    > DirectCast(ctrl, TextBox).Text = arr1(i)
    >
    > i += 1
    >
    > End If
    >
    > Next
    >
    >
    >
    Leon, Nov 1, 2004
    #4
  5. Leon

    Henri Guest

    Ok, imagine that you have to simplify 1 textbox, 1 label, then 1 textbox in
    your Controls collection.
    arr1's length should be 2 if it's the same as the number of textboxes.

    Your code:

    Dim I As Integer
    For I = 0 To Me.Controls.Count - 1
    If (TypeOf Controls(I) Is TextBox) Then
    Dim tbox As TextBox = CType(Controls(I), TextBox)
    tbox.Text = arr1(I)
    End If
    Next

    Let's trace the for...next loop in your code:

    first iteration:
    i = 0
    typeof controls(0) is TextBox -> tbox.Text = arr1(0)

    2nd iteration:
    i = 1
    typeof controls(1) is Label -> do nothing

    3nd iteration:
    i = 2
    typeof controls(2) is TextBox -> tbox.Text = arr1(2) OUT OF RANGE
    EXCEPTION!

    Do you understand your mistake? The index you use to loop through the
    Controls collection must be separated from the one you use to access arr1.

    Henri
    Henri, Nov 1, 2004
    #5
  6. Leon

    Leon Guest

    Thanks so much Herni,
    I got it:) You Have Been A Great Help!
    And Thanks again!

    "Henri" <> wrote in message
    news:%...
    > Ok, imagine that you have to simplify 1 textbox, 1 label, then 1 textbox
    > in
    > your Controls collection.
    > arr1's length should be 2 if it's the same as the number of textboxes.
    >
    > Your code:
    >
    > Dim I As Integer
    > For I = 0 To Me.Controls.Count - 1
    > If (TypeOf Controls(I) Is TextBox) Then
    > Dim tbox As TextBox = CType(Controls(I), TextBox)
    > tbox.Text = arr1(I)
    > End If
    > Next
    >
    > Let's trace the for...next loop in your code:
    >
    > first iteration:
    > i = 0
    > typeof controls(0) is TextBox -> tbox.Text = arr1(0)
    >
    > 2nd iteration:
    > i = 1
    > typeof controls(1) is Label -> do nothing
    >
    > 3nd iteration:
    > i = 2
    > typeof controls(2) is TextBox -> tbox.Text = arr1(2) OUT OF RANGE
    > EXCEPTION!
    >
    > Do you understand your mistake? The index you use to loop through the
    > Controls collection must be separated from the one you use to access arr1.
    >
    > Henri
    >
    >
    >
    >
    >
    Leon, Nov 1, 2004
    #6
  7. Leon

    Matt Berther Guest

    Hello Henri,

    Better yet (no type checking and casting, since the foreach can do this for
    you)...

    foreach (TextBox ctrl in Controls)
    {
    ctrl.Text = arr1;
    i++;
    }

    --
    Matt Berther
    http://www.mattberther.com

    > Better:
    >
    > Dim i As Integer = 0
    >
    > For Each ctrl As Control In Controls
    >
    > If TypeOf ctrl Is TextBox Then
    >
    > DirectCast(ctrl, TextBox).Text = arr1(i)
    >
    > i += 1
    >
    > End If
    >
    > Next
    >
    Matt Berther, Nov 1, 2004
    #7
  8. Leon

    Henri Guest

    Hello Matt,

    But what happens if Controls also contains controls different from TextBox?

    Henri

    "Matt Berther" <> a écrit dans le message de
    news:...
    > Hello Henri,
    >
    > Better yet (no type checking and casting, since the foreach can do this

    for
    > you)...
    >
    > foreach (TextBox ctrl in Controls)
    > {
    > ctrl.Text = arr1;
    > i++;
    > }
    >
    > --
    > Matt Berther
    > http://www.mattberther.com
    >
    > > Better:
    > >
    > > Dim i As Integer = 0
    > >
    > > For Each ctrl As Control In Controls
    > >
    > > If TypeOf ctrl Is TextBox Then
    > >
    > > DirectCast(ctrl, TextBox).Text = arr1(i)
    > >
    > > i += 1
    > >
    > > End If
    > >
    > > Next
    > >

    >
    >
    >
    Henri, Nov 1, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M. Steinbach
    Replies:
    1
    Views:
    625
    tom_usenet
    Feb 20, 2004
  2. M. Steinbach
    Replies:
    0
    Views:
    340
    M. Steinbach
    Feb 20, 2004
  3. fabioppp

    typeof and g++

    fabioppp, Feb 23, 2005, in forum: C++
    Replies:
    10
    Views:
    1,292
    Ben Hetland
    Feb 24, 2005
  4. Arkadiy

    typeof emulation

    Arkadiy, Feb 24, 2005, in forum: C++
    Replies:
    0
    Views:
    749
    Arkadiy
    Feb 24, 2005
  5. Muckeypuck

    TypeOf question

    Muckeypuck, Aug 10, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    3,923
    Muckeypuck
    Aug 11, 2006
Loading...

Share This Page