what happens really when I declare a class in a class

T

Thomas

I tried the following:

Public Class ClassA

Public Class ClassB
Private myName As String
Private myBirthday As Date
Public Property Birthday() As Date
Get
Return myBirthday
End Get
Set(ByVal Value As Date)
myBirthday = Value
End Set
End Property
Public Property Name() As String
Get
Return myName
End Get
Set(ByVal Value As String)
myName = Value
End Set
End Property
End Class

Private myFriends() As ClassB

Public Sub AddFriend(ByVal Name As String, ByVal Birthday As Date)
Dim c As New ClassB
c.Name = Name
c.Birthday = Birthday
Try
ReDim Preserve myFriends(myFriends.Length)
myFriends(myFriends.Length - 1) = c
Catch ex As Exception
ReDim Preserve myFriends(0)
myFriends(0) = c
End Try
End Sub

Public Sub ShowFriends()
Dim s As String
For i As Integer = 0 To myFriends.Length - 1
s += myFriends(i).Name + " - " + myFriends(i).Birthday.ToString +
vbNewLine
Next
MessageBox.Show(s)
End Sub

Public ReadOnly Property GetFriend(ByVal Index As Integer) As ClassB
Get
Try
Return myFriends(Index)
Catch ex As Exception
Return Nothing
End Try
End Get
End Property

End Class


This testcode does not make real sense but it shows my problem.
What does VB.NET do when I declare ClassB in ClassA ? Both, ClassA and
ClassB, can be used outside with Dim c as new ClassX. So where is the
difference between declaring ClassA and ClassB as normal classes and
declaring ClassB in ClassA ?

The real problem is that I used classes in classes in a program and
that I'm not sure about the consequences of this type of coding.
 
T

Tom John

I tried the following:
This testcode does not make real sense but it shows my problem.
What does VB.NET do when I declare ClassB in ClassA ? Both, ClassA and
ClassB, can be used outside with Dim c as new ClassX. So where is the
difference between declaring ClassA and ClassB as normal classes and
declaring ClassB in ClassA ?

The real problem is that I used classes in classes in a program and
that I'm not sure about the consequences of this type of coding.

Really you should declare ClassB as private, allowing it only be
accessible from ClassA, or if you want to structure your classes use a
namespace.

Hope this helps

Tom
 
C

Cor Ligthert

Thomas,

Does this give you an idea (namespace)

\\\
Dim [my] as New ClassA.ClassB
my.Name = "Cor"
///

I hope this helps,

Cor
 
H

Herfried K. Wagner [MVP]

Thomas said:
This testcode does not make real sense but it shows my problem.
What does VB.NET do when I declare ClassB in ClassA ? Both, ClassA and
ClassB, can be used outside with Dim c as new ClassX. So where is the
difference between declaring ClassA and ClassB as normal classes and
declaring ClassB in ClassA ?

In addition to the other replies, make sure you have read the chapters about
nested classes in the documentation:

Visual Basic and Visual C# Concepts -- Implementing Nested Classes
<URL:http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskImplementingNestedClass.asp>
 
D

David

I tried the following:

Public Class ClassA

Public Class ClassB
This testcode does not make real sense but it shows my problem.
What does VB.NET do when I declare ClassB in ClassA ? Both, ClassA and
ClassB, can be used outside with Dim c as new ClassX. So where is the
difference between declaring ClassA and ClassB as normal classes and
declaring ClassB in ClassA ?

The real problem is that I used classes in classes in a program and
that I'm not sure about the consequences of this type of coding.

From a technical standpoint, the only significant difference between
nesting and not nesting is that if you nest, then instances of ClassB
have access to private members of ClassA.

There's trivial changes, such as the fact that if you nest, then you
refer to ClassB with a different syntax, and ClassB is affected by the
access level of ClassA, but these things are obvious.

The thing you really want to be concerned with here is stylistic things
like code layout and readability. Herfried's post contains a link to
some good info, but the real question you want to ask yourself is
whether your code is more readable with nested classes than without.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top