Results 1 to 4 of 4

Thread: Why doesn't this work ? (control array)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99

    Angry Why doesn't this work ? (control array)

    Hello everybody,


    I'm creating a control array which I need to make on certain moment visible or not.

    Right now the only controls in my ctl array are labels. If I want to make them visible, I want to use in my ShowInvoice sub a loop to loop through my control array.
    Everything works fine ... only if I use
    VB Code:
    1. typeof sInvoice(i) is label then
    2. sInvoice(i).visible=true
    3. end if

    If I just use sInvoice(i).visible = true (without the typeof condition) then I receive Object reference not set to an instance of an object
    I don't want to change my showinvoice routine each time when there's a new kind of control in my array !

    I really don't see why this is happening !

    Hope someone can help me out !

    This is my code (this doesn't work => but I want this one to work)

    VB Code:
    1. Private Function InvoiceArray()
    2.  
    3.         Dim sInvoice(20) As Control
    4.  
    5.         sInvoice(1) = lblInvoiceTo
    6.         sInvoice(2) = lblInvoiceName
    7.         sInvoice(3) = lblInvoiceAddress
    8.         sInvoice(4) = lblInvoiceNr
    9.         sInvoice(5) = lblInvoiceZip
    10.         sInvoice(6) = lblInvoicePlace
    11.  
    12.         Return sInvoice
    13.  
    14.     End Function
    15.  
    16. Private Sub ShowInvoice()
    17.         Dim sInvoice() As Control
    18.         Dim i As Integer
    19.         sInvoice = InvoiceArray()
    20.  
    21.         Dim ctl As Control
    22.         For i = 0 To UBound(sInvoice)
    23.             sInvoice(i).Visible = True
    24.         Next


    This doesn't work

    VB Code:
    1. For i = 0 To UBound(sInvoice)
    2.             sInvoice(i).Visible = True
    3.             If TypeOf sInvoice(i) Is Label Then
    4.                 sInvoice(i).Visible = True
    5.                 Response.Write(sInvoice(i).GetType.ToString)
    6.             End If
    7.         Next

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99

    Why doesn't this work ? (control array) (resolved but intresting)

    Ok, ok, I got it !

    I needed to check my control on nothing ....
    This code works great now :

    VB Code:
    1. Private Function InvoiceArray()
    2.  
    3.         Dim sInvoice(20) As Control
    4.  
    5.         sInvoice(1) = lblInvoiceTo
    6.         sInvoice(2) = lblInvoiceName
    7.         sInvoice(3) = lblInvoiceAddress
    8.         sInvoice(4) = lblInvoiceNr
    9.         sInvoice(5) = lblInvoiceZip
    10.         sInvoice(6) = lblInvoicePlace
    11.  
    12.         Return sInvoice
    13.  
    14.     End Function
    15.  
    16. Private Sub ShowInvoice()
    17.         Dim sInvoice() As Control
    18.         Dim i As Integer
    19.         sInvoice = InvoiceArray()
    20.  
    21.         Dim ctl As Control
    22.         For i = 0 To UBound(sInvoice)
    23.             if not sinvoice(i) is nothing then
    24.                sInvoice(i).Visible = True
    25.             end if
    26.         Next
    27. End sub

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you wanted to shorten your code you could do this:
    VB Code:
    1. Private Function InvoiceArray()
    2.  
    3.         Dim sInvoice() As Control = {lblInvoiceTo, lblInvoiceName, lblInvoiceAddress, lblInvoiceNr, lblInvoiceZip, lblInvoicePlace}
    4.         Return sInvoice
    5.     End Function
    6.  
    7.     Private Sub ShowInvoice()
    8.         Dim sInvoice() As Control= InvoiceArray()
    9.  
    10.         Dim ctl As Control
    11.         For Each ctl In sInvoice
    12.             If Not ctl Is Nothing Then
    13.                 ctl.Visible = True
    14.             End If
    15.         Next
    16.     End Sub

    Functionally it is no different, other than the sInvoice array being a dynamic size instead of 20 elements. It just has a few shortcuts to save space.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99
    Indeed Edneis,



    this is in fact a wonderfull tip !
    You gave me in a few lines more to understand then I already read in lots of books and newsgroups !

    Thanks a lot !

    Bjorn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width