Home All Groups Group Topic Archive Search About

Exposing properties of a class within a class

Author
15 Feb 2006 1:32 PM
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?


--
Cheers, ymt.

Author
15 Feb 2006 1:50 PM
Armin Zingler
Show quote Hide quote
"Yuk Tang" <jim.lak***@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
Author
15 Feb 2006 2:06 PM
Yuk Tang
Show quote Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in
news:#skx7bjMGHA.2320@TK2MSFTNGP11.phx.gbl:
> "Yuk Tang" <jim.lak***@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.
Author
15 Feb 2006 2:29 PM
Yuk Tang
Yuk Tang <jim.lak***@yahoo.com> wrote in
Show quoteHide quote
news:Xns976B8F57F9A1Ejimlaker2yahoocom@130.133.1.4:
> "Armin Zingler" <az.nospam@freenet.de> wrote in
> news:#skx7bjMGHA.2320@TK2MSFTNGP11.phx.gbl:
>> "Yuk Tang" <jim.lak***@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.
Author
15 Feb 2006 3:28 PM
Armin Zingler
Show quote Hide quote
"Yuk Tang" <jim.lak***@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.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
Author
15 Feb 2006 6:54 PM
Yuk Tang
Show quote Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in
news:OQEcVTkMGHA.3144@TK2MSFTNGP11.phx.gbl:
> "Yuk Tang" <jim.lak***@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.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.


--
Cheers, ymt.
Author
15 Feb 2006 1:52 PM
Yuk Tang
Yuk Tang <jim.lak***@yahoo.com> wrote in
news:Xns976B898E639D2jimlaker2yahoocom@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.