1

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
2
  • Why _strFinish = Nothing instead of setting it equal to String.Empty or vbNullString? You cannot do _strFinish.Contains("Silver") if the string is Nothing. Commented Feb 20, 2014 at 4:37
  • Yea I put that in went I first started and didn't change it when I started cleaning it up. Commented Feb 20, 2014 at 4:41

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

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.

12 Comments

To initialize the contents, you can change the declaration to Public chandelierStyle() As New Chandelier()(49).
@AdamZuckerman What exactly is the syntax for that? I tried what you have in your comment but that fails with a compiler error. Maybe I'm doing it wrong - haven't used VB for a while.
Why am I not seeing how to do that?
@VinceCat What do you mean?
There used to be a method of declaring an array of objects with a new initializer. It looks like that has be removed. I'll keep looking for the correct declaration.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.