Exposing properties of a class within a class

Y

Yuk Tang

Intellisense doesn't sense it, and I get the squiggly error
"Reference to a non-shared member requires an object reference".

Here's the property I want to expose.

Public Class Commands
Property FileName() As String
Get
....
Set
....
End Property
Public Sub New()
FileName = ""
End Sub
End Class

So Commands is a public class, with a publicly exposed property
FileName that is given a value when Commands is instantiated.

But Commands is inside another class.

Public Class Parser
Public Class Commands
....
End Class
Public Sub New()
Dim aCommands As New Commands
End Sub
End Class

So Parser is another public class, that creates an instance of
Commands when it's instantiated. Intellisense even detects that
Commands is a subclass of Parser. However, it doesn't give me any
options after that, and typing in the code manually gives me the
object reference error.

Here's the code in question.

Dim aParser as new Parser
aParser.Commands.FileName = "HelloWorld.txt"

Please can someone help?
 
A

Armin Zingler

Yuk Tang said:
Intellisense doesn't sense it, and I get the squiggly error
"Reference to a non-shared member requires an object reference".

Here's the property I want to expose.

Public Class Commands
Property FileName() As String
Get
...
Set
...
End Property
Public Sub New()
FileName = ""
End Sub
End Class

So Commands is a public class, with a publicly exposed property
FileName that is given a value when Commands is instantiated.

But Commands is inside another class.

Public Class Parser
Public Class Commands
...
End Class
Public Sub New()
Dim aCommands As New Commands
End Sub
End Class

So Parser is another public class, that creates an instance of
Commands when it's instantiated. Intellisense even detects that
Commands is a subclass of Parser. However, it doesn't give me any
options after that, and typing in the code manually gives me the
object reference error.

Here's the code in question.

Dim aParser as new Parser
aParser.Commands.FileName = "HelloWorld.txt"

Please can someone help?


aCommands is the only variable that references a Commands object. As it's
local, it can not be referenced outside Sub New. In addition, it runs out of
scope after Sub New exits. This would work:


Public Class Parser
Public Class Commands
...
End Class

Public aCommands As New Commands 'or Public Readonly

Public Sub New()
End Sub
End Class


Dim aParser as new Parser
aParser.aCommands.FileName = "HelloWorld.txt"


Whether this model is acceptable depends on factors unknown to me.


Armin
 
Y

Yuk Tang

Yuk Tang said:
Here's the code in question.

Dim aParser as new Parser
aParser.Commands.FileName = "HelloWorld.txt"

Please can someone help?

I can work around it by creating a differently named property and
setting the property to this instance of Commands, but Parser.Commands
still appears in Intellisense, along with Parser.Command (my new
property). Parser.Commands doesn't allow me to access the FileName,
while Parser.Command does.
 
Y

Yuk Tang

Armin Zingler said:
aCommands is the only variable that references a Commands object.
As it's local, it can not be referenced outside Sub New. In
addition, it runs out of scope after Sub New exits. This would
work:


Public Class Parser
Public Class Commands
...
End Class

Public aCommands As New Commands 'or Public Readonly

Public Sub New()
End Sub
End Class


Dim aParser as new Parser
aParser.aCommands.FileName = "HelloWorld.txt"


Whether this model is acceptable depends on factors unknown to me.

Thanks, it works.
 
Y

Yuk Tang

Yuk Tang said:
Thanks, it works.

Sorry to be ungrateful, but is there a way of hiding Commands from
Intellisense after doing this? Since I can't use the FileName
property of Commands, there's no real point in having it pop up, but
if I hide it by making Commands private I won't be able to expose
aCommands.
 
A

Armin Zingler

Yuk Tang said:
Sorry to be ungrateful, but is there a way of hiding Commands from
Intellisense after doing this? Since I can't use the FileName
property of Commands, there's no real point in having it pop up, but
if I hide it by making Commands private I won't be able to expose
aCommands.

Apply the EditorBrowsableAttribute:

Class outerClass
<EditorBrowsableAttribute(EditorBrowsableState.Never)> _
Class innerclass

End Class
End Class

Why is it a problem that intellisense lists 'Commands'? It's a member of the
outer class like aCommands. Before anybody else complains: I wouldn't call
the public variable aCommands, it was only to show how to expose the object.
I'd name it 'Commands' and either name the inner class 'CommandClass', or
don't use a nested class and name it 'ParserCommands'. Personal preference
only.


Armin
 
Y

Yuk Tang

Armin Zingler said:
Apply the EditorBrowsableAttribute:

Class outerClass
<EditorBrowsableAttribute(EditorBrowsableState.Never)> _
Class innerclass

End Class
End Class
Thanks.


Why is it a problem that intellisense lists 'Commands'? It's a
member of the outer class like aCommands. Before anybody else
complains: I wouldn't call the public variable aCommands, it was
only to show how to expose the object. I'd name it 'Commands' and
either name the inner class 'CommandClass', or don't use a nested
class and name it 'ParserCommands'. Personal preference only.

I did something similar, sort of. I renamed the inner class
aCommands so I could call the outer one Commands. I used nested
classes because I'm new to the OOP thing and hence my design is not
perfect, and also Parser had several distinct uses attached to it,
one of which was what I called Commands.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top