|
-
Jul 8th, 2003, 02:42 AM
#1
Thread Starter
Lively Member
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:
typeof sInvoice(i) is label then
sInvoice(i).visible=true
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:
Private Function InvoiceArray()
Dim sInvoice(20) As Control
sInvoice(1) = lblInvoiceTo
sInvoice(2) = lblInvoiceName
sInvoice(3) = lblInvoiceAddress
sInvoice(4) = lblInvoiceNr
sInvoice(5) = lblInvoiceZip
sInvoice(6) = lblInvoicePlace
Return sInvoice
End Function
Private Sub ShowInvoice()
Dim sInvoice() As Control
Dim i As Integer
sInvoice = InvoiceArray()
Dim ctl As Control
For i = 0 To UBound(sInvoice)
sInvoice(i).Visible = True
Next
This doesn't work
VB Code:
For i = 0 To UBound(sInvoice)
sInvoice(i).Visible = True
If TypeOf sInvoice(i) Is Label Then
sInvoice(i).Visible = True
Response.Write(sInvoice(i).GetType.ToString)
End If
Next
-
Jul 8th, 2003, 02:45 AM
#2
Thread Starter
Lively Member
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:
Private Function InvoiceArray()
Dim sInvoice(20) As Control
sInvoice(1) = lblInvoiceTo
sInvoice(2) = lblInvoiceName
sInvoice(3) = lblInvoiceAddress
sInvoice(4) = lblInvoiceNr
sInvoice(5) = lblInvoiceZip
sInvoice(6) = lblInvoicePlace
Return sInvoice
End Function
Private Sub ShowInvoice()
Dim sInvoice() As Control
Dim i As Integer
sInvoice = InvoiceArray()
Dim ctl As Control
For i = 0 To UBound(sInvoice)
if not sinvoice(i) is nothing then
sInvoice(i).Visible = True
end if
Next
End sub
-
Jul 8th, 2003, 03:29 AM
#3
If you wanted to shorten your code you could do this:
VB Code:
Private Function InvoiceArray()
Dim sInvoice() As Control = {lblInvoiceTo, lblInvoiceName, lblInvoiceAddress, lblInvoiceNr, lblInvoiceZip, lblInvoicePlace}
Return sInvoice
End Function
Private Sub ShowInvoice()
Dim sInvoice() As Control= InvoiceArray()
Dim ctl As Control
For Each ctl In sInvoice
If Not ctl Is Nothing Then
ctl.Visible = True
End If
Next
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.
-
Jul 8th, 2003, 03:40 AM
#4
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|