vb.net Sub New() 在此上下文中不可访问,因为它是“朋友”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1167772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:18:44  来源:igfitidea点击:

Sub New() is not accessible in this context because it is 'Friend'

vb.net

提问by DenaliHardtail

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

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

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?

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

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

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

Also tried

也试过

Dim oPS As New AeccPointStyle
ops = oDescKey.PointStyle

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

Thanks!

谢谢!

Update 1 - based on comment from Meta-Knight

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

1 -

1 -

Dim oPS As AeccPointStyle = Nothing
oPS = oDescKey.PointStyle

2 -

2 -

Dim oPS As AeccPointStyle = oDescKey.PointStyle

Both versions throw NullReferenceExceptions.

两个版本都抛出 NullReferenceExceptions。

回答by Meta-Knight

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

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

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:

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

Dim oPS As AeccPointStyle = oDescKey.PointStyle


Edit about your NullReferenceException:

编辑您的 NullReferenceException:

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.

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

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.

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

回答by Mike Gates

If your AeccPointStyle class is using a 'Friend' modifier, ie it is defined as:

如果您的 AeccPointStyle 类使用了“朋友”修饰符,即它被定义为:

Friend Class AeccPointStyle

or the default constructor has the 'Friend' modifier, ie:

或者默认构造函数具有 'Friend' 修饰符,即:

Friend Sub New()

and the code you posted is not in the same assembly, you cannot call the constructor on this class. In order to get this to work, you must put your code in the same assembly as the AeccPointStyle class. Check out this page to learn more about the modifiers: more information about modifiers

并且您发布的代码不在同一个程序集中,您不能在此类上调用构造函数。为了使其工作,您必须将代码放在与 AeccPointStyle 类相同的程序集中。查看此页面以了解有关修饰符的更多信息:有关修饰符的更多信息

回答by Vahid Farahmandian

When using FRIEND as access modifier for your class, you need to make sure that both the class it-self and the class where you use it are in the same NAMESPACE, otherwise you will get this error message.

当使用 FRIEND 作为您的类的访问修饰符时,您需要确保类本身和您使用它的类在同一个 NAMESPACE 中,否则您将收到此错误消息。

回答by Fredrik M?rk

My guess is the following: AeccPointStyleis declared in another assembly than the code sample in your question. The constructor (Sub New) of AeccPointStyleis declared a Friend, which means that it is reachable only within the same assembly.

我的猜测如下:AeccPointStyle在另一个程序集中声明,而不是您问题中的代码示例。的构造函数 ( Sub New)AeccPointStyle被声明为 a Friend,这意味着它只能在同一个程序集中访问。

You can solve this in two ways

您可以通过两种方式解决此问题

  • Change Sub Newso that it is Public
  • Provide a Shared Public Sub Create, that will create and return a new AeccPointStyle
  • 改变Sub New它是Public
  • 提供一个Shared Public Sub Create, 这将创建并返回一个新的AeccPointStyle

回答by Fredrik M?rk

AeccPointStyle doesn't have a public default constructor. The one you're trying to use is limited to other classes within the same assembly.

AeccPointStyle 没有公共默认构造函数。您尝试使用的类仅限于同一程序集中的其他类。

Here's some code I found online:

这是我在网上找到的一些代码:

Dim oPointStyle As AeccPointStyle 
Set oPointStyle = g_oAeccDoc.PointStyles.Add(strName) 

Notice the PointStyles property (probably some sort of PointStylesColleciton) on g_oAeccDoc is instantiating and returning a new AeccPointStyle instance for you.

请注意 g_oAeccDoc 上的 PointStyles 属性(可能是某种 PointStylesColleciton)正在为您实例化并返回一个新的 AeccPointStyle 实例。