Sub New() 在此上下文中不可访问,因为它是“朋友" [英] Sub New() is not accessible in this context because it is 'Friend'

查看:25
本文介绍了Sub New() 在此上下文中不可访问,因为它是“朋友"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么意思,我该如何解决?

So what does this mean and how do I fix it?

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

This message occurs if I place the New keyword in the line(s) below. If I remove it, i get an error at runtime saying I need to use New. What am I doing wrong?

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

Debug.Print(oPS.Name)
Debug.Print(oPS.MarkerSymbolName)

也试过了

Dim oPS As New AeccPointStyle
ops = oDescKey.PointStyle

Debug.Print(oPS.Name)
Debug.Print(oPS.MarkerSymbolName)

谢谢!

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

Update 1 - based on comment from Meta-Knight

1 -

Dim oPS As AeccPointStyle = Nothing
oPS = oDescKey.PointStyle

2 -

Dim oPS As AeccPointStyle = oDescKey.PointStyle

两个版本都抛出 NullReferenceExceptions.

Both versions throw NullReferenceExceptions.

推荐答案

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

The empty constructor of AeccPointStyle is marked as friend, which means only classes inside its assembly can call it.

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

But looking at your code, I don't think you need to call New. Just set it to Nothing at first. Or even better, directly set your variable with the good value:

Dim oPS As AeccPointStyle = oDescKey.PointStyle

<小时>

编辑您的 NullReferenceException:

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

Typically, this type of exception is raised when you call a property of an object with a value of Nothing. In this case, if oDescKey was set to Nothing, such an exception would be raised.

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

If oDescKey does NOT have a value of Nothing, then the only thing that executes some code is the PointStyle property. So it's safe to assume that the PointStyle property throws a NullReferenceException. Try watching the oDescKey.PointStyle variable in the debugger, you should see that it throws an exception.

这篇关于Sub New() 在此上下文中不可访问,因为它是“朋友"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆