0

我有一块木材的数据结构。我已经建立了一个类来处理我想制作这个结构的长度成员的数据类型的维度(架构、公制等)。VB 说除非我将成员设为“共享”,否则我不能在定义中使用“新”。如果我将成员设为“共享”,当我尝试访问代码中的成员时,我将看不到数据。

 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

如何使用我的“clsDimension”对象作为结构的“Length”成员的数据类型?

4

2 回答 2

0

正如所有评论所示:您应该将 Struct 更改为一个类,因为您想引用它。由于结构是值类型,类是引用类型,这就是你想要的:

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
于 2013-08-16T16:51:18.310 回答
0

.NET 结构没有默认构造函数,您必须创建自己的(或初始化值的函数)。但是那种破坏结构的目的。

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
    Dim PieceLength As clsDimension
    Dim StockLength As clsDimension
    Dim LeftSplicePlate As String
    Dim RightSplicePlate As String

    Public Sub New(ByVal t As String)
        PieceLength = New clsDimension
        StockLength = New clsDimension
    End Sub
End Structure

但正如其他人所说,将其更改为类是正确的做法。类是引用类型,结构是值类型。

于 2013-08-16T18:34:25.257 回答