rick9rick rick9rick - 4 months ago 22
Vb.net Question

Assigning values to an object and its nested object

I am a novice and am missing something simple. I have two Classes

Public Class Param
Public Property temperature As String
Public Property display As Boolean
Public Property storage As Boolean
Public Property reason As Boolean
Public Property stats As Object
Public Property errors As Object
End Class

Public Class getTemperature
Public Property method As String
Public Property params As Param()
Public Property id As String
End Class


I want to declare and assign values to the objects but I keep getting the error "Object reference not set to an instance of an object" when trying to assign values to items within param. I don't understand, I have created both the object GetTemperature and the object Params, what am I missing?

Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
Dim params As New Param
params.temperature = "true"
'GetTemp.params(0) = params


I have also tried, but get the same response:

Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
GetTemp.params(0).temperature = "True"

Answer

Try this:

Public Class Param
    Public Property temperature As String
    Public Property display As Boolean
    Public Property storage As Boolean
    Public Property reason As Boolean
    Public Property stats As Object
    Public Property errors As Object
End Class

Public Class getTemperature
    Public Property method As String
    Public Property params As List(Of Param)
    Public Property id As String

    Public Sub New()
        params = New List(Of Param)
    End Sub
End Class

So you could write something like:

        Dim a As New getTemperature
        a.params.Add(New Param)