Search code examples
vb.netclassobjectdata-structuresshared

Can I add an object to a struct in VB.net?


I have a data struct for a piece of lumber. I've built a class to handle dimensions (architectural, metric, etc... ) that I'd like to make the data type of the length member of this struct. VB says I can't use 'new' in my definition unless I make the member 'Shared'. If I make the member 'Shared' I can't see the data when I try to access the member in my code.

 Public Structure PieceInfo
    Dim ProjectNumber As String
    Dim ProjectName As String
    Dim BuildingType As String
    Dim BuildingNumber As String
    Dim BLevel As String
    Dim Batch As String
    Dim Trussname As String
    Dim Span As Single
    Dim PieceName As String
    Dim LumberType As String
    Shared PieceLength As New clsDimension
    Shared StockLength As New clsDimension
    Dim LeftSplicePlate As String
    Dim RightSplicePlate As String
End Structure

How can I use my "clsDimension" object as the data type for the "Length" members of my struct?


Solution

  • As all the comments indicate: You should change your Struct to a class because you want to reference it. And due to Structs being value types and Classes being reference types, This is what you want:

    Public Class PieceInfo
        Dim ProjectNumber As String
        Dim ProjectName As String
        Dim BuildingType As String
        Dim BuildingNumber As String
        Dim BLevel As String
        Dim Batch As String
        Dim Trussname As String
        Dim Span As Single
        Dim PieceName As String
        Dim LumberType As String
        Shared PieceLength As New clsDimension
        Shared StockLength As New clsDimension
        Dim LeftSplicePlate As String
        Dim RightSplicePlate As String
    End Class