【发布时间】:2016-04-03 17:15:34
【问题描述】:
我创建了一个有趣的结构“列表”。这些结构包含一些数组(动态),我想重新调整它们。但我收到一个错误:
“在 3D Cube.exe 中发生了‘System.NullReferenceException’类型的第一次机会异常”和
“对象引用未设置为对象的实例。”。
这就是代码的样子(你可以看到它看起来像 opengl):
模块:
Public Module _3DDefinitions
Public Pen1 As New System.Drawing.Pen(Color.White, 2)
Public Structure VertexesObjects
Dim Face() As Faces
Dim FaceCount As Double
Dim Position As DPoint ''Translated Position
Dim NPPF As Integer ''NumberofPointsPerFace
End Structure
Public Structure Faces
Dim PointVertexes() As _3DDefinitions.DPoint
Dim PointCount As Double
Dim FaceColor As Color
End Structure
Public Structure DPoint
Dim X As Single
Dim Y As Single
Dim Z As Single
End Structure
Enum GL_LoadAction As Byte
GL_Start = 1 ''Start- equivalent of GlBegin
GL_End = 2 ''End-equivalent of GlEnd
End Enum
Enum GL_EnableAction As Byte
GL_UseDefaultUnit = 1
GL_UseOwnUnit = 2
GL_Translation = 4
End Enum
End Module
类(我没有包括类的第一部分以及许多子类和函数):
Private Objects() As _3DDefinitions.VertexesObjects,
ObjectsIndex As Double, FacesIndex As Double, PointsIndex As Double,
GL_NPPF As Integer = 4, GL_COLOR As Color = Color.Brown,
GL_Status As _3DDefinitions.GL_LoadAction = GL_LoadAction.GL_Start, GL_TranslatePosition As _3DDefinitions.DPoint,
GL_Settings As _3DDefinitions.GL_EnableAction = GL_EnableAction.GL_UseDefaultUnit,
GL_DrawingInitialized As Boolean = False, GL_GraphicsInitialized As Boolean = False,
GL_Unit As Double = 300
Public Sub GL_LoadVertexes(ByVal Operation As _3DDefinitions.GL_LoadAction)
GL_Status = Operation
If Operation = _3DDefinitions.GL_LoadAction.GL_Start And Not GL_DrawingInitialized Then
GL_DrawingInitialized = True
GL_GraphicsInitialized = False
ReDim Preserve Objects(ObjectsIndex)
FacesIndex = 0
PointsIndex = 0
ElseIf Operation = GL_LoadAction.GL_End And GL_GraphicsInitialized And GL_DrawingInitialized Then
GL_DrawingInitialized = False
ObjectsIndex = ObjectsIndex + 1
Draw()
End If
End Sub
Public Sub LoadVertex3D(ByVal X As Single, ByVal Y As Single, ByVal Z As Single)
If GL_Status = GL_LoadAction.GL_Start Then
GL_GraphicsInitialized = True
ReDim Preserve Objects(ObjectsIndex).Face(FacesIndex).PointVertexes(PointsIndex)''<--Here is the error
If FindBit(GL_Settings, GL_EnableAction.GL_UseOwnUnit) Then
With Objects(ObjectsIndex).Face(FacesIndex).PointVertexes(PointsIndex)
.X = X
.Y = Y
.Z = Z
End With
ElseIf FindBit(GL_Settings, GL_EnableAction.GL_UseDefaultUnit) Then
With Objects(ObjectsIndex).Face(FacesIndex).PointVertexes(PointsIndex)
.X = X * GL_Unit / 10
.Y = Y * GL_Unit / 10
.Z = Z * GL_Unit / 10
End With
End If
If PointsIndex = GL_NPPF - 1 Then
FacesIndex = FacesIndex + 1
PointsIndex = 0
Else
PointsIndex = PointsIndex + 1
End If
End If
End Sub
子 LoadVertex3D 只是加载内存中的一些点(坐标)(使用结构),子 GL_LoadVertexes 告诉类用户想要加载 3d 对象的点.我真的需要这些结构,因为例如我想创建一个新的“对象”,所以我必须再次使用“对象”特殊变量。但是当我这样做时,FacesIndex 和 PointsIndex(它们只是计数器)将被重置。
唯一保持不变的变量是 ObjectsIndex。这就是为什么我需要这些结构,因为在这些结构中我可以保存创建的面和点的数量(在 FaceCount 和 PointCount 变量中)。
有什么问题?或者如果您知道更好的解决方案来满足我的需求,可以告诉我吗?
【问题讨论】:
-
避免使用
ReDim(VB6 天的保留),考虑改用List<T>。 -
考虑不要对变量使用标准的 NET 类型名称。基本上
Private Objects() As ...只是声明了数组。我看不到您在那堵代码中创建和实例化的位置。见stackoverflow.com/questions/4660142/… -
@Plutonix 我对 vb.net 有点陌生。 “NET 类型”是什么意思?我从 vb6 知道 Type 用于结构..
-
@Plutonix 我不知道在创建变量时必须使用 new 关键字。有一个模块,其中我有一些结构。然后在课堂上我创建一个像这样的变量: Private Objects() as
. 。我已经完全按照你说的做了。并创建一个什么的新实例? -
New 不是为了“创建变量”,而是创建一个对象的实例。
I create a variable like this...。和以前一样,Private | Dim只是声明一个变量;您的数组已声明但 未 实例化。使用列表代替,并为自己省点麻烦。
标签: arrays vb.net optimization data-structures