|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Exposing properties of a class within a class"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.
Show quote
Hide quote
"Yuk Tang" <jim.lak***@yahoo.com> schrieb aCommands is the only variable that references a Commands object. As it's> 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? 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
Show quote
Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in Thanks, it works.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. -- Cheers, ymt. Yuk Tang <jim.lak***@yahoo.com> wrote in
Show quoteHide quote news:Xns976B8F57F9A1Ejimlaker2yahoocom@130.133.1.4: Sorry to be ungrateful, but is there a way of hiding Commands from > "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. 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.
Show quote
Hide quote
"Yuk Tang" <jim.lak***@yahoo.com> schrieb Apply the EditorBrowsableAttribute:> > > 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. 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
Show quote
Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in Thanks.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 > Why is it a problem that intellisense lists 'Commands'? It's a I did something similar, sort of. I renamed the inner class > 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. 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. Yuk Tang <jim.lak***@yahoo.com> wrote in
news:Xns976B898E639D2jimlaker2yahoocom@130.133.1.4: I can work around it by creating a differently named property and > > Here's the code in question. > > Dim aParser as new Parser > aParser.Commands.FileName = "HelloWorld.txt" > > Please can someone help? 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.
Unresponsive UI during lengthy operation ?
extending BackgroundWorker ProgressChangedEventArgs Invoking a PictureBox click ? MethodInfo or MethodBase? Random number problem console.write listview checkbox column - toggle image ThreadPool.QueueUserWorkItem Noob Question - Best Control for a TV Guide? Registering 2005 dll for COM |
|||||||||||||||||||||||