Nested Classes

snarfblam

Mega-Ultra Chicken
Joined
Jun 10, 2003
Messages
1,832
Location
USA
User Rank
*Expert*
Is there a way to declare a nested class without actually putting it within the declaration of the outer class in your code? This is leading to really cluttered code for me.
 
Would something like this work?
Code:
Public class class1

public thingy as class2

public sub new()
thingy = new class2
end sub

end class

Public class class2

public sub somethinguseful()
messagebox.show("ANNOY")
end sub

end class
 
No, that is not a nested class. Thats simply class instantiated within another class.

[VB]
Class A
Public Member As Integer

Private Class B
Public Member As Integer
End Class
End Class
[/VB]

In such a case, class B can only be instantiated by class A. Were class B declared publicly, anyone could instantiate class B, but would have to use the syntax [New A.B] as opposed to [New B].

I need to use nested classes to provide private classes and organize object models. But when my classes get large and complex, nested classes cause clutter.
 
marble_eater said:
Is there a way to declare a nested class without actually putting it within the declaration of the outer class in your code? This is leading to really cluttered code for me.

No though it might be easier in managed c++ becuase it has headers and source file.
 
HJB417 said:
No though it might be easier in managed c++ becuase it has headers and source file.

Certainly true, however, that isnt much of an option for me seeing as I am very rusty on what little (unmanaged) C++ I know.

And Im betting that the answer to my question is "no," but I am hoping that Im wrong because my code is starting to give me a headache.
 
Not possible right now but will be in the future with partial classes (not future if you want to use beta software which you cant distribute :)).
Did you consider using code Regions?
 
mutant said:
Did you consider using code Regions?

Class definitions are collapsable anyways. The problem is that I am editing all of the classes at the same time because they all relate so closeley to eachother, so I cant collapse them. If I could click a tab to open a different .vb file it wouldnt be so confusing, but as it is now, I have many functions between the different classes with similar names and the massive amount of functions is hurting my head.
 
of the hundereds of classes Ive created using the .net framework, the number of nested classes Ive created, I can count using 1 hand and its never caused me a headache. I think you may need to consider a design change if you find nested classes to be cumbersome.
 
Im designing an object model where nested classes are very appropriate. The nested classes should only ever be used in conjuntion with the class in which it is nested in, just like the ListView class, which nests ListViewItems, ListViewItemCollection, CheckedIndexCollection, CheckedListViewItemCollection, ColumnHeaderCollection, SelectedIndexCollection, and SelectedListViewItemCollection.

I think it might give me more of a headache to have so many classes all in the same namespace. Imagine if Windows Forms did not use nested classes. Using nested classes is far from cumbersome, but I cant say the same for their implementation in VB.
 
Back
Top