473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

nested classes... or something.

I just tried to do something and realized I'm not sure of the proper (if
there *IS* a proper) way to do it. I'm designing a class with a lot a
methods/properties. I would like to group some of them so you access them
like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of
the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the nested
Commands object (easily) access the data members of MyObject? I realy don't
want to have to create an instance of Commands and pass it a bunch of data
to initialize it. All I want to do is group methods/properties of MyObject
so the user of the class doesn't have to deal with tons of methods off the
root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper
way to do this? Or am I crazy for even wanting to do such a strange thing?
:)
Jul 21 '05 #1
10 1449
In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
I just tried to do something and realized I'm not sure of the proper (if
there *IS* a proper) way to do it. I'm designing a class with a lot a
methods/properties. I would like to group some of them so you access them
like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of
the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the nested Commands object (easily) access the data members of MyObject? I realy don't want to have to create an instance of Commands and pass it a bunch of data
to initialize it. All I want to do is group methods/properties of MyObject so the user of the class doesn't have to deal with tons of methods off the
root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper
way to do this? Or am I crazy for even wanting to do such a strange thing? :)

Jul 21 '05 #2
In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
I just tried to do something and realized I'm not sure of the proper (if
there *IS* a proper) way to do it. I'm designing a class with a lot a
methods/properties. I would like to group some of them so you access them
like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of
the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the nested Commands object (easily) access the data members of MyObject? I realy don't want to have to create an instance of Commands and pass it a bunch of data
to initialize it. All I want to do is group methods/properties of MyObject so the user of the class doesn't have to deal with tons of methods off the
root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper
way to do this? Or am I crazy for even wanting to do such a strange thing? :)

Jul 21 '05 #3
Right, but how would the methods and properties of m_InnerClass access the
data in MyClass? The methods and proprties I want to put in MyInnerClass
are all methods and properties that need to work with the data in MyClass.
I realy don't want to have MyClass pass it's data into m_InnerClass when it
instantiates it. That just sounds so kludgy, not to mention having two
copies of the data for what is really (to the user to MyClass) just one
object. I don't want the methods and properties to be "separate" from
MyClass, just "grouped" somehow so there aren't so many of them off the root
of the MyClass object. But I don't think there is a way I can do that
without separating the methods and properties from MyClass. :(

"Charles Law" <bl***@nowhere.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl...
In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
I just tried to do something and realized I'm not sure of the proper (if
there *IS* a proper) way to do it. I'm designing a class with a lot a
methods/properties. I would like to group some of them so you access them like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the

nested
Commands object (easily) access the data members of MyObject? I realy

don't
want to have to create an instance of Commands and pass it a bunch of data to initialize it. All I want to do is group methods/properties of

MyObject
so the user of the class doesn't have to deal with tons of methods off the root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper way to do this? Or am I crazy for even wanting to do such a strange

thing?
:)


Jul 21 '05 #4
Right, but how would the methods and properties of m_InnerClass access the
data in MyClass? The methods and proprties I want to put in MyInnerClass
are all methods and properties that need to work with the data in MyClass.
I realy don't want to have MyClass pass it's data into m_InnerClass when it
instantiates it. That just sounds so kludgy, not to mention having two
copies of the data for what is really (to the user to MyClass) just one
object. I don't want the methods and properties to be "separate" from
MyClass, just "grouped" somehow so there aren't so many of them off the root
of the MyClass object. But I don't think there is a way I can do that
without separating the methods and properties from MyClass. :(

"Charles Law" <bl***@nowhere.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl...
In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
I just tried to do something and realized I'm not sure of the proper (if
there *IS* a proper) way to do it. I'm designing a class with a lot a
methods/properties. I would like to group some of them so you access them like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the

nested
Commands object (easily) access the data members of MyObject? I realy

don't
want to have to create an instance of Commands and pass it a bunch of data to initialize it. All I want to do is group methods/properties of

MyObject
so the user of the class doesn't have to deal with tons of methods off the root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper way to do this? Or am I crazy for even wanting to do such a strange

thing?
:)


Jul 21 '05 #5
"nobody" <no****@no.server> schrieb
Right, but how would the methods and properties of m_InnerClass
access the data in MyClass? The methods and proprties I want to put
in MyInnerClass are all methods and properties that need to work with
the data in MyClass. I realy don't want to have MyClass pass it's
data into m_InnerClass when it instantiates it. That just sounds so
kludgy, not to mention having two copies of the data for what is
really (to the user to MyClass) just one object. I don't want the
methods and properties to be "separate" from MyClass, just "grouped"
somehow so there aren't so many of them off the root of the MyClass
object. But I don't think there is a way I can do that without
separating the methods and properties from MyClass. :(


To access an object you need to pass a reference. In this case, pass a
reference to a MyClass object to the InnerClass object.

Object references have nothing to do with the project structure (assemblies,
(nested) namespaces/classes), so the InnerClass object does not "know" that
there is an outer class (MyClass) at design time. There might be none or
multiple instances of MyClass at run time.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Jul 21 '05 #6
"nobody" <no****@no.server> schrieb
Right, but how would the methods and properties of m_InnerClass
access the data in MyClass? The methods and proprties I want to put
in MyInnerClass are all methods and properties that need to work with
the data in MyClass. I realy don't want to have MyClass pass it's
data into m_InnerClass when it instantiates it. That just sounds so
kludgy, not to mention having two copies of the data for what is
really (to the user to MyClass) just one object. I don't want the
methods and properties to be "separate" from MyClass, just "grouped"
somehow so there aren't so many of them off the root of the MyClass
object. But I don't think there is a way I can do that without
separating the methods and properties from MyClass. :(


To access an object you need to pass a reference. In this case, pass a
reference to a MyClass object to the InnerClass object.

Object references have nothing to do with the project structure (assemblies,
(nested) namespaces/classes), so the InnerClass object does not "know" that
there is an outer class (MyClass) at design time. There might be none or
multiple instances of MyClass at run time.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Jul 21 '05 #7
The usual way would be to pass a reference when m_InnerClass is
instantiated.

Public Class MyClass

Private m_InnerClass As New MyInnerClass(Me)

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

Public Class MyInnerClass

Private m_Parent As MyClass

Public Sub New(Parent As MyClass)

m_Parent = Parent

End Sub

Public Sub DoStuffInvolvingMyClass()

m_Parent.SomeMethod()

...

End Sub

End Class

This doesn't create two copies of your data as you are only passing a
reference. The user of your class will see the effect you want without
knowing anything about how it is implemented internally.

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uD*************@tk2msftngp13.phx.gbl...
Right, but how would the methods and properties of m_InnerClass access the
data in MyClass? The methods and proprties I want to put in MyInnerClass
are all methods and properties that need to work with the data in MyClass.
I realy don't want to have MyClass pass it's data into m_InnerClass when it instantiates it. That just sounds so kludgy, not to mention having two
copies of the data for what is really (to the user to MyClass) just one
object. I don't want the methods and properties to be "separate" from
MyClass, just "grouped" somehow so there aren't so many of them off the root of the MyClass object. But I don't think there is a way I can do that
without separating the methods and properties from MyClass. :(

"Charles Law" <bl***@nowhere.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl...
In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
I just tried to do something and realized I'm not sure of the proper (if there *IS* a proper) way to do it. I'm designing a class with a lot a
methods/properties. I would like to group some of them so you access them like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the

nested
Commands object (easily) access the data members of MyObject? I realy

don't
want to have to create an instance of Commands and pass it a bunch of data to initialize it. All I want to do is group methods/properties of

MyObject
so the user of the class doesn't have to deal with tons of methods off the root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper way to do this? Or am I crazy for even wanting to do such a strange

thing?
:)



Jul 21 '05 #8
The usual way would be to pass a reference when m_InnerClass is
instantiated.

Public Class MyClass

Private m_InnerClass As New MyInnerClass(Me)

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

Public Class MyInnerClass

Private m_Parent As MyClass

Public Sub New(Parent As MyClass)

m_Parent = Parent

End Sub

Public Sub DoStuffInvolvingMyClass()

m_Parent.SomeMethod()

...

End Sub

End Class

This doesn't create two copies of your data as you are only passing a
reference. The user of your class will see the effect you want without
knowing anything about how it is implemented internally.

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uD*************@tk2msftngp13.phx.gbl...
Right, but how would the methods and properties of m_InnerClass access the
data in MyClass? The methods and proprties I want to put in MyInnerClass
are all methods and properties that need to work with the data in MyClass.
I realy don't want to have MyClass pass it's data into m_InnerClass when it instantiates it. That just sounds so kludgy, not to mention having two
copies of the data for what is really (to the user to MyClass) just one
object. I don't want the methods and properties to be "separate" from
MyClass, just "grouped" somehow so there aren't so many of them off the root of the MyClass object. But I don't think there is a way I can do that
without separating the methods and properties from MyClass. :(

"Charles Law" <bl***@nowhere.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl...
In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
I just tried to do something and realized I'm not sure of the proper (if there *IS* a proper) way to do it. I'm designing a class with a lot a
methods/properties. I would like to group some of them so you access them like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the

nested
Commands object (easily) access the data members of MyObject? I realy

don't
want to have to create an instance of Commands and pass it a bunch of data to initialize it. All I want to do is group methods/properties of

MyObject
so the user of the class doesn't have to deal with tons of methods off the root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper way to do this? Or am I crazy for even wanting to do such a strange

thing?
:)



Jul 21 '05 #9
Now that's one I didn't think of... It would require that all of MyClass's
data that needs to be accessible to MyInnerClass would have to be exposed
publicly (or friendly)... Maybe I could have a class to hold all of
MyClass's private data, and pass a reference to that into MyInnerClass....
I'll play that idea around in my mind for awhile (it might get lonely in
there though...) :)

Thanks all!

"Charles Law" <bl***@nowhere.com> wrote in message
news:er**************@TK2MSFTNGP09.phx.gbl...
The usual way would be to pass a reference when m_InnerClass is
instantiated.

Public Class MyClass

Private m_InnerClass As New MyInnerClass(Me)

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

Public Class MyInnerClass

Private m_Parent As MyClass

Public Sub New(Parent As MyClass)

m_Parent = Parent

End Sub

Public Sub DoStuffInvolvingMyClass()

m_Parent.SomeMethod()

...

End Sub

End Class

This doesn't create two copies of your data as you are only passing a
reference. The user of your class will see the effect you want without
knowing anything about how it is implemented internally.

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uD*************@tk2msftngp13.phx.gbl...
Right, but how would the methods and properties of m_InnerClass access the
data in MyClass? The methods and proprties I want to put in MyInnerClass are all methods and properties that need to work with the data in MyClass. I realy don't want to have MyClass pass it's data into m_InnerClass when

it
instantiates it. That just sounds so kludgy, not to mention having two
copies of the data for what is really (to the user to MyClass) just one
object. I don't want the methods and properties to be "separate" from
MyClass, just "grouped" somehow so there aren't so many of them off the

root
of the MyClass object. But I don't think there is a way I can do that
without separating the methods and properties from MyClass. :(

"Charles Law" <bl***@nowhere.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl...
In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
> I just tried to do something and realized I'm not sure of the proper (if > there *IS* a proper) way to do it. I'm designing a class with a lot a > methods/properties. I would like to group some of them so you access them
> like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So
some of
> the methods are grouped under "Commands".
>
> My first thought was a nested class, but how would the methods in
the nested
> Commands object (easily) access the data members of MyObject? I realy don't
> want to have to create an instance of Commands and pass it a bunch

of data
> to initialize it. All I want to do is group methods/properties of
MyObject
> so the user of the class doesn't have to deal with tons of methods
off the
> root of the object.
>
> Am I thinking along the wrong lines? Is there another/better/more

proper
> way to do this? Or am I crazy for even wanting to do such a strange
thing?
> :)
>
>



Jul 21 '05 #10
Now that's one I didn't think of... It would require that all of MyClass's
data that needs to be accessible to MyInnerClass would have to be exposed
publicly (or friendly)... Maybe I could have a class to hold all of
MyClass's private data, and pass a reference to that into MyInnerClass....
I'll play that idea around in my mind for awhile (it might get lonely in
there though...) :)

Thanks all!

"Charles Law" <bl***@nowhere.com> wrote in message
news:er**************@TK2MSFTNGP09.phx.gbl...
The usual way would be to pass a reference when m_InnerClass is
instantiated.

Public Class MyClass

Private m_InnerClass As New MyInnerClass(Me)

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

Public Class MyInnerClass

Private m_Parent As MyClass

Public Sub New(Parent As MyClass)

m_Parent = Parent

End Sub

Public Sub DoStuffInvolvingMyClass()

m_Parent.SomeMethod()

...

End Sub

End Class

This doesn't create two copies of your data as you are only passing a
reference. The user of your class will see the effect you want without
knowing anything about how it is implemented internally.

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uD*************@tk2msftngp13.phx.gbl...
Right, but how would the methods and properties of m_InnerClass access the
data in MyClass? The methods and proprties I want to put in MyInnerClass are all methods and properties that need to work with the data in MyClass. I realy don't want to have MyClass pass it's data into m_InnerClass when

it
instantiates it. That just sounds so kludgy, not to mention having two
copies of the data for what is really (to the user to MyClass) just one
object. I don't want the methods and properties to be "separate" from
MyClass, just "grouped" somehow so there aren't so many of them off the

root
of the MyClass object. But I don't think there is a way I can do that
without separating the methods and properties from MyClass. :(

"Charles Law" <bl***@nowhere.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl...
In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
> I just tried to do something and realized I'm not sure of the proper (if > there *IS* a proper) way to do it. I'm designing a class with a lot a > methods/properties. I would like to group some of them so you access them
> like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So
some of
> the methods are grouped under "Commands".
>
> My first thought was a nested class, but how would the methods in
the nested
> Commands object (easily) access the data members of MyObject? I realy don't
> want to have to create an instance of Commands and pass it a bunch

of data
> to initialize it. All I want to do is group methods/properties of
MyObject
> so the user of the class doesn't have to deal with tons of methods
off the
> root of the object.
>
> Am I thinking along the wrong lines? Is there another/better/more

proper
> way to do this? Or am I crazy for even wanting to do such a strange
thing?
> :)
>
>



Jul 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
14
by: theo | last post by:
if I have nested div combinations, can I call for styles only to specific nested combos? It's 3 lists <li>, on one page, needing different styles. <div id=list1><li> <a id="t1"...
3
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me...
11
by: C# Learner | last post by:
Is it not possible to declare a nested class in a seperate file from its "parent" class -- i.e. in a similar way to the idea of spreading namespaces over more than one file?
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
5
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
5
by: Calvin Spealman | last post by:
On Wed, Aug 13, 2008 at 11:32 AM, Cousson, Benoit <b-cousson@ti.comwrote: There is no point of nested classes because nested classes _are not_ supported by python. They are simply an artifact of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.