Sub New() 在此上下文中无法访问,因为它是“Friend”

Den*_*ail 5 vb.net

那么这是什么意思以及如何解决它?

如果我将 New 关键字放在下面的行中,则会出现此消息。如果删除它,我会在运行时收到错误消息,提示我需要使用 New。我究竟做错了什么?

Dim oPS As AeccPointStyle = New AeccPointStyle
ops = oDescKey.PointStyle 

Debug.Print(oPS.Name)
Debug.Print(oPS.MarkerSymbolName)
Run Code Online (Sandbox Code Playgroud)

也尝试过

Dim oPS As New AeccPointStyle
ops = oDescKey.PointStyle

Debug.Print(oPS.Name)
Debug.Print(oPS.MarkerSymbolName)
Run Code Online (Sandbox Code Playgroud)

谢谢!

更新 1 - 基于 Meta-Knight 的评论

1 -

Dim oPS As AeccPointStyle = Nothing
oPS = oDescKey.PointStyle
Run Code Online (Sandbox Code Playgroud)

2 -

Dim oPS As AeccPointStyle = oDescKey.PointStyle
Run Code Online (Sandbox Code Playgroud)

两个版本都会抛出 NullReferenceExceptions。

Met*_*ght 4

AeccPointStyle 的空构造函数被标记为友元,这意味着只有其程序集中的类可以调用它。

但看看你的代码,我认为你不需要调用 New。一开始只需将其设置为Nothing 即可。或者更好的是,直接使用合适的值设置变量:

Dim oPS As AeccPointStyle = oDescKey.PointStyle
Run Code Online (Sandbox Code Playgroud)

编辑您的 NullReferenceException:

通常,当您调用值为 Nothing 的对象的属性时,会引发此类异常。在这种情况下,如果 oDescKey 设置为 Nothing,则会引发此类异常。

如果 oDescKey 的值不为 Nothing,则唯一执行某些代码的是 PointStyle 属性。因此可以安全地假设 PointStyle 属性抛出 NullReferenceException。尝试在调试器中观察 oDescKey.PointStyle 变量,您应该看到它抛出异常。