Question Structures/Arrays and Lists

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
I have the following code:

VB.NET:
    <Serializable()> Private Structure Sometext
        Public Sentance As String
        Public Text As String
    End Structure

    <Serializable()> Private Structure SerialSettings
        Public PortNumber As String
        Public baudRate As String
        Public line As List(Of Sometext)
    End Structure
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim SerialSetting As SerialSettings
    Dim tempText As New Sometext
        
        SerialSetting.PortNumber = "COM1"
        SerialSetting.baudRate = "4800"
        tempText.Sentance = "Start"
        tempText.Text = "Main"
        SerialSetting.line.Add(tempText)

   End Sub

When i run the code i get:

null error.png

and it points to the 'SerialSetting.line.add(tempText)'

Am i trying to implement this all wrong, because is seems logical to me, but i am a Noooooob

Thanks

Stu
 
The main problem is that you do not instantiate your List(Of Sometext). You declare the type of the member, but you never actually create it. Either you can leave the declaration as it is, and do 'SerialSetting.line = New List(Of Sometext)' before you actually start using it, or add the New keyword to the declaration (Public line As New List(Of Sometext)).

Also why are you even using structures here? Have a look at this article.

When you are defining a container type, consider the following criteria.
A structure can be preferable when:

  • You have a small amount of data and simply want the equivalent of the UDT (user-defined type) of previous versions of Visual Basic
  • You perform a large number of operations on each instance and would incur performance degradation with heap management
  • You have no need to inherit the structure or to specialize functionality among its instances
  • You do not box and unbox the structure
  • You are passing blittable data across a managed/unmanaged boundary
A class is preferable when:

  • You need to use inheritance and polymorphism
  • You need to initialize one or more members at creation time
  • You need to supply an unparameterized constructor
  • You need unlimited event handling support
If your container type does not fit clearly into either of these categories, define a Class. Classes are more flexible than structures, and the storage and performance differences are often negligible. Structures are intended primarily for types that behave like built-in types, not necessarily for general-purpose use.
 
Agreed, although a structure CAN hold a collection, I really see no use for it. Since 2005, the only time I had to use structures were when working with the Win32 API, and that happens less and less often these days. When in doubt, use a class.
 
Agreed, although a structure CAN hold a collection,
Yes, it could, but it's fields can't be instantiated like that.
 
Back
Top