5 Replies - 439 Views - Last Post: 05 September 2012 - 12:08 AM Rate Topic: -----

#1 Vladimir_Vaskev  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 31-August 11

Initializing Label Array to Labels Created in Design

Posted 30 August 2012 - 02:17 AM

Hey, I created 26 labels in the design view, and now I want to group these in array so that I can dynamically change their properties, unfortunately the code I made below comes up with an error saying "Object reference not set to an instance of an object."

Here's my code:

    Dim valueArray() As Double = {0.5, 1, 10, 147, ..., 20018.68, 100025.8956}
    Dim labelArray() As Label = {Label1, Label2, Label3, ..., label25, label26}

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        
        For i = 0 To 25
            labelArray(i).Text = valueArray(i)
        Next

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Initializing Label Array to Labels Created in Design

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,221
  • Joined: 25-March 09

Re: Initializing Label Array to Labels Created in Design

Posted 30 August 2012 - 02:26 AM

you cant init your array with the labels before your form initialize. if you move this to your form load event it will work. the work around that i can think of right now is this:

    Public arrLbls() As Label

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim arrLblsTmp() As Label = {Label1, Label2}
        arrLbls = arrLblsTmp
        For i As Integer = 0 To arrLbls.Count - 1
            arrLbls(i).Text = i.ToString()
        Next
    End Sub



good luck

This post has been edited by raziel_: 30 August 2012 - 02:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: Initializing Label Array to Labels Created in Design

Posted 30 August 2012 - 02:28 AM

I wouldn't advise using a set number in the For Loop (To 25 in your case). Imagine if the bounds of the array changed. The code wouldn't cope with that. Just for durability's sake, make it:
For i As Integer = 0 To labelArray.Count - 1
... 'code here
Next

But maybe the problem is that you don't have the right amount of items in valueArray. For example:
In valueArray I have 12 items.
In labelArray I have 26 items.
I am referring to item 23 in labelArray and 23 in valueArray. But since the valueArray doesn't have 23 items then I am referring to an object that doesn't exist.
In that case, you will have to fix up your for loop.

~Dimitri~

This post has been edited by DimitriV: 30 August 2012 - 02:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 Vladimir_Vaskev  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 31-August 11

Re: Initializing Label Array to Labels Created in Design

Posted 30 August 2012 - 02:48 AM

Thanks Raziel for the fast reply, it works fine, and now that I think about it, your solution makes more sense than what I was trying.

Dimitri, thanks for the fast reply, but both arrays have the same amount of variables. Also, thanks for the .Count, I was going to search for that later.
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: Initializing Label Array to Labels Created in Design

Posted 30 August 2012 - 02:52 AM

Quote

Dimitri, thanks for the fast reply, but both arrays have the same amount of variables.

Glad you got it working, but with the … in there I just wasn't aware of the length.
Was This Post Helpful? 0
  • +
  • -

#6 Vladimir_Vaskev  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 31-August 11

Re: Initializing Label Array to Labels Created in Design

Posted 05 September 2012 - 12:08 AM

Quote

Glad you got it working, but with the … in there I just wasn't aware of the length.


No problem, I realized how important using .count() instead of a set value is, and ended up changing all my for loops, thanks for the advice :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1