473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what happens really when I declare a class in a class

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.
Nov 21 '05 #1
4 1216
On 21 Apr 2005 01:34:44 -0700, Th***********@web.de (Thomas) wrote:
I tried the following:
<...Snip...>
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
Nov 21 '05 #2
Thomas,

Does this give you an idea (namespace)

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

I hope this helps,

Cor
Nov 21 '05 #3
"Thomas" <Th***********@web.de> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
On 2005-04-21, Thomas <Th***********@web.de> wrote:
I tried the following:

Public Class ClassA

Public Class ClassB <snip>
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.
Nov 21 '05 #5

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

Similar topics

12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
31
by: N.Davis | last post by:
I am very new to Python, but have done plenty of development in C++ and Java. One thing I find weird about python is the idea of a module. Why is this needed when there are already the ideas of...
6
by: Mark Broadbent | last post by:
this might sound like an obvious question but I have found that usually these two evolve at the same time. One of the biggest reasons for creating the abstraction in the first place (in my...
3
by: Joergen Bech | last post by:
Am trying to paint a simple rectangle directly on the screen DC using the code below, but nothing happens?!? Weird. Anyone who can tell me what I am doing wrong? This ought to be...
24
by: ypjofficial | last post by:
Hello all, I have written a class with many private data members.and i am putting it in a separate dll file. Now when i link that file while writing my main program module,natuarally i have to...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.