By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,703 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,703 IT Pros & Developers. It's quick & easy.

Exposing properties of a class within a class

P: n/a
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?
--
Cheers, ymt.
Feb 15 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Yuk Tang <ji********@yahoo.com> wrote in
news:Xn*******************************@130.133.1.4 :

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.
--
Cheers, ymt.
Feb 15 '06 #2

P: n/a
"Yuk Tang" <ji********@yahoo.com> schrieb
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

Feb 15 '06 #3

P: n/a
"Armin Zingler" <az*******@freenet.de> wrote in
news:#s**************@TK2MSFTNGP11.phx.gbl:
"Yuk Tang" <ji********@yahoo.com> schrieb

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.


Thanks, it works.
--
Cheers, ymt.
Feb 15 '06 #4

P: n/a
Yuk Tang <ji********@yahoo.com> wrote in
news:Xn*******************************@130.133.1.4 :
"Armin Zingler" <az*******@freenet.de> wrote in
news:#s**************@TK2MSFTNGP11.phx.gbl:
"Yuk Tang" <ji********@yahoo.com> schrieb

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.


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.
--
Cheers, ymt.
Feb 15 '06 #5

P: n/a
"Yuk Tang" <ji********@yahoo.com> schrieb
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.


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.Nev er)> _
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

Feb 15 '06 #6

P: n/a
"Armin Zingler" <az*******@freenet.de> wrote in
news:OQ**************@TK2MSFTNGP11.phx.gbl:
"Yuk Tang" <ji********@yahoo.com> schrieb

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.Nev er)> _
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.
--
Cheers, ymt.
Feb 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.