Nested Classes (Newbie) Question

  • Thread starter Thread starter Keith Rebello
  • Start date Start date
K

Keith Rebello

I am trying to implement a graph drawing class. I want to nest a curve
class inside it to draw multiple curves.
i.e.
Class Graph2D
'
Class CurveObj
'
End Class
'
End Class

The Graph2D class will have multiple CurveObj classes, each with its own set
of (x,y) values.

Two questions:
1. Is there a way of keeping track of each CurveObj in the Graph2D class?
I would like to add each CurveObj to some sort of collection in the Graph2D
class when I instantiate the CurveObj (in Sub New() maybe). I want the
Graph2D class to do all the drawing and hence it needs to know each curve's
(x,y) values.
2. Is there a better way of doing this if I'm barking up the wrong tree?

Thanks for your help.

Keith Rebello.
 
You need to declare the classes seperately.

Class CurveObj
End Class

Class Graph2D
End Class

Then inside Graph2D you need to store your CurveObj
objects. Use could use an array or a linked list or
whatever you prefer. Then just write an add function:

Graph2D.Add(SomeX,SomeY) or
Graph2D.Add(SomeX,SomeY,SomeName) if you want to track
them by name.

Which creates a new CurveObj and adds it to your current
list.
 
* "Keith Rebello said:
I am trying to implement a graph drawing class. I want to nest a curve
class inside it to draw multiple curves.
i.e.
Class Graph2D
'
Class CurveObj
'
End Class
'
End Class

The Graph2D class will have multiple CurveObj classes, each with its own set
of (x,y) values.

Two questions:
1. Is there a way of keeping track of each CurveObj in the Graph2D class?

\\\
Public Class Foo
Private m_Bars As ArrayList

Public Sub New()
m_Bars = New ArrayList()
End Sub

Public Sub AddBar()
m_Bars.Add(New Bar())
End Sub

Private Class Bar
End Class
End Class
///
 
Keith,
As Sean suggested. I would have Graph2D have an instance field of ArrayList
for example, that holds each instance of CurveObj that Graph2D 'owns'.

Whether CurveObj is nested or not is up to you. If CurveObj is 'strictly' an
implementation detail of Graph2D I would make it nested & private. Otherwise
I would make it Public or Friend and consider not nesting it...

Something like (overly simplified):
Class Graph2D
'
' implementation detail
Private Class CurveObj
Public Sub New(x As Integer, y As Integer)
End Sub
'
End Class
'

Private Readonly m_curves As New ArrayList()

Public Sub Add(x As Integer, y As Integer)
m_curves.Add(New CurveObj(x, y))
End Sub

Public Sub Draw
For Each curve As CurveObj in m_curves
curve.Draw()
Next
End Sub
End Class

Hope this helps
Jay
 
Sean,

Is there a reason why you would need to make separate classes? Assuming
that the CurveObj does not need to be exposed why could he not leave the
inner class and simply have a private collection in the Graph2D object that
would contain the CurveObj's?

Thanks,
Dan
 
For simplicity's sake. He could embedded the second class
description inside as a private class I guess, but from
what he's trying to do I think that will just be more
confusing for him. I think to work out how to do it he's
better off thinking of them as two seperate classes.
 
No reason other than simplifying things for the guy. I'm
curious as to what the value you guys think comes from
declaring a class as an inner class? It is a data type,
not a variable, so I guess I never really saw much need to
encapsulate. Usually I just find out later on I can use
the class somewhere else and end up having to pull it out.
 
Thanks a lot, everyone. You all have been very helpful. I think I'm going
to enjoy programming in VB.Net.
Keith.
 
Hey, thanks for the link. Haha, he didn't sell me
though....=) His first reason for inner classes to me
illuminates why I usually don't use them. He says he would
break things down further with a Tree having a inner
search algorithm class. That logic makes sense to me,
except that a search algorithm is a good example of a
class you could probably find another use of somewhere
else in your code. By embedding it, you lose that
functionality. Haha, I guess to each there own though.
Thanks.
 
Sean,
No reason other than simplifying things for the guy. I'm
curious as to what the value you guys think comes from
declaring a class as an inner class?

As I stated in my post, I would make the class a private inner class, if the
inner class's sole purpose was an implementation detail for the outer class.

For example, if I were defining a LinkedList class, I would define the Node
class as a private local class to LinkedList, as only LinkedList knows or
cares about the Node class. The Node class holds the Next, Prev, and Data
points for each Node in the LinkedList.

' A single linked list.
Public Class LinkedList

Private Class Node
Public Data As Object
Public Next As Node

Public Sub New(data As Object, next As Node)
Me.Data = data
Me.Next = next
End Sub

End Class

Private m_head As Node

Public Sub Add(data As Object)
m_head = New Node(data, m_head)
End Sub

Public Readonly Property Head As Object
Get
If m_head Is Nothing Then
Return Nothing
End If
Return m_head.Data
End Get
End Property

End Class
Usually I just find out later on I can use
the class somewhere else and end up having to pull it out.
Why split it out to begin with if you don't need to, as splitting it out
suggests that others can use it in inappropriate ways! If I do want to split
it out later, Refactoring http://www.refactoring.com is handy.

Hope this helps
Jay
 
Back
Top