473,386 Members | 1,754 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,386 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 1450
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.