tags:

views:

2039

answers:

4

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?

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 -

Dim oPS As AeccPointStyle = Nothing
oPS = oDescKey.PointStyle

2 -

Dim oPS As AeccPointStyle = oDescKey.PointStyle

Both versions throw NullReferenceExceptions.

+3  A: 

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

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


Edit about your 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.

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.

Meta-Knight
I think that you are mixing up the `Friend` and `Protected` keywords.
Fredrik Mörk
@Fredrik: Oops, you're right, will edit
Meta-Knight
oDescKey is not null. I set a watch and now I'm debug.printing the name property of oDescKey immediately before the line which throws the NullException.
Scott
The PointStyle property probably throws an exception. See my edit.
Meta-Knight
A: 

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

You can solve this in two ways

  • Change Sub New so that it is Public
  • Provide a Shared Public Sub Create, that will create and return a new AeccPointStyle
Fredrik Mörk
Yes, AeccPointStyle resides in another assembly. I believe it is a COM component wrapped for .NET consumption. I cannot modify the source as this type is exposed through a product's API.
Scott
A: 

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

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.

Will
A: 

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

Friend Class AeccPointStyle

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

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

Mike Gates