VinceCat
VinceCat

Reputation: 25

vb.net class and array

I am having an issue with trying to take info from class and putting them into an array with the class data type. I am getting a null error. I can see its not adding the variable info into the array at all. I am unsure what it is I am missing. Can anyone point out to me what it is?

Here is the code:

    Option Explicit On
    Option Strict On
    Option Infer Off

    Public Class Form1

Public Class Chandelier
    Private _strFinish As String
    Private _intLights As Integer
    Private _blnCrystal As Boolean
    Private _dblTotal As Double
    Public Property Finish As String
        Get
            Return _strFinish
        End Get
        Set(value As String)
            _strFinish = value
        End Set
    End Property
    Public Property Lights As Integer
        Get
            Return _intLights
        End Get
        Set(value As Integer)
            If value > 0 Then
                _intLights = value
            Else
                _intLights = 0
            End If
        End Set
    End Property
    Public Property Crystal As Boolean
        Get
            Return _blnCrystal
        End Get
        Set(value As Boolean)
            _blnCrystal = value
        End Set
    End Property
    Public Property Total As Double
        Get
            Return _dblTotal
        End Get
        Set(value As Double)
            If value > 0 Then
                _dblTotal = value
            Else
                _dblTotal = 0
            End If
        End Set
    End Property
    Public Sub New()
        _strFinish = Nothing
        _intLights = 0
        _blnCrystal = False
        _dblTotal = 0
    End Sub
    Public Function getCrystal() As Boolean
        Return _blnCrystal
    End Function
    Public Function getPrice() As Double
        Dim crystalPrice As Double
        Dim price As Double
        If _strFinish.Contains("Silver") Then
            price = 39.99
            If getCrystal() = True Then
                crystalPrice = _intLights * 25.5
            Else
                crystalPrice = 0
            End If
            price = price + crystalPrice
        End If
        If _strFinish.Contains("Brass") Then
            price = 49.99
            If getCrystal() = True Then
                crystalPrice = _intLights * 25.5
            Else
                crystalPrice = 0
            End If
            price = price + crystalPrice
        End If
        Return price
    End Function
End Class
Public chandelierStyle(49) As Chandelier
Dim count As Integer = 0
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

    Dim ceilingOrnament As New Chandelier

    ceilingOrnament.Finish = cboFinish.SelectedItem.ToString
    Integer.TryParse(cboLights.SelectedItem.ToString, ceilingOrnament.Lights)
    If chkTrimmings.Checked Then
        ceilingOrnament.Crystal = True
    End If
    Dim dblTotal As Double = ceilingOrnament.getPrice()
    ceilingOrnament.Total = dblTotal

    If count <= 49 Then
        'here is where the error starts
        chandelierStyle(count).Finish = ceilingOrnament.Finish
        chandelierStyle(count).Lights = ceilingOrnament.Lights
        chandelierStyle(count).Crystal = ceilingOrnament.Crystal
        chandelierStyle(count).Total = ceilingOrnament.Total
        count += 1
    End If

End Sub

Upvotes: 1

Views: 216

Answers (2)

xxbbcc
xxbbcc

Reputation: 17346

Looks like you create your array of 49 elements with this line:

Public chandelierStyle(49) As Chandelier

but you never actually initialize its contents. What you have is 49 empty slots, not 49 Chandelier instances. You need to first set each array slot to a new instance of Chandelier before you can update their properties in your button Click event.

You can use a For loop to initialize each array slot before you use it; something like:

For i As Integer = 0 To 49
    chandelierStyle(i) = New Chandelier()
Next i

You'd have this loop in the constructor of the class (looks like it's a Form) that contains your chandelierStyle array.

Note: Don't quote me on that. :) I haven't used VB.NET for ages and there may be an easier way to do this.

Edit: Or, you can do what Idle_Mind suggested in his answer and just stash the ceilingOrnament instance in the appropriate array slot.

Upvotes: 3

Idle_Mind
Idle_Mind

Reputation: 39152

Just place your dynamic instance into the array, instead of trying to copy each individual field.

Change this:

If count <= 49 Then
    'here is where the error starts
    chandelierStyle(count).Finish = ceilingOrnament.Finish
    chandelierStyle(count).Lights = ceilingOrnament.Lights
    chandelierStyle(count).Crystal = ceilingOrnament.Crystal
    chandelierStyle(count).Total = ceilingOrnament.Total
    count += 1
End If

To:

If count <= 49 Then
    'here is where the error starts
    chandelierStyle(count) = ceilingOrnament
    count += 1
End If

Upvotes: 4

Related Questions