Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET implicitly instantiating objects

Posted on 2004-08-11
1
Medium Priority
?
828 Views
Last Modified: 2008-02-01
I have a class Application, which has a collection containing Applicants attached to it.

In the property for the collection, I have the following:

Get
    If _Applicants is Nothing then
        _Applicants = new ObjectCollection
        LoadApplicants()
    End IF
End Get

Now, when I create an Application, I don't make any sort of reference to the Applicant collection, and immediately after the Application is created, in the Quick Watch window I can see that _Applicants is still nothing.  The problem starts in the loop below:

For each objApplicant in objApplication.Applicants
     ....do some stuff
Next

If I set a break point on the "for each" line, then use the watch to check, _Applicants has been instantiated.  However, because it hasn't gone through the property, the Applicants haven't been loaded and I never get inside the loop.

Is there anything I can do to stop .NET implicitly creating the Applicants collection, or force it to do so via the property?
0
Comment
Question by:racka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
IT Meetjesland earned 2000 total points
ID: 11781591
I don't know why you're using ObjectCollection.  But that doesn't really matter for the problem you have.  The reason why your Applicants property in the For Each loop is not properly loaded is because of the fact that it WAS loaded but you didn't return the reference to it.  Your Getter of property Applicants should be:

Get
    If IsNothing(_Applicants) Then
        _Applicants = new ObjectCollection
        LoadApplicants()
    End If
    Return _Applicants
End Get

Anyway.  I tried a little test with the code that i pasted below and it works just fine.  I do it with a Collection instead of ObjectCollection because I don't see an advantage in using the latter, but it really doesn't matter which Collection class you use.

I made a new VB.NET Windows Application in VS.NET 2003 and in the Main form code i have put following:

Public Class Form1
    Inherits System.Windows.Forms.Form

' here comes the #Region " Windows Form Designer generated code " which i'm not posting for obvious reasons

#Region "Event Handlers"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim app As New Application
        For Each Applic As Applicant In app.Applicants
            MessageBox.Show(Applic.ToString())
        Next
    End Sub

#End Region

End Class

Public Class Application

    Protected _Applicants As Collection

    Protected Overridable Sub LoadApplicants()
        For I As Integer = 0 To 4
            _Applicants.Add(New Applicant)
        Next
    End Sub

    Public Overridable ReadOnly Property Applicants() As Collection
        Get
            If _Applicants Is Nothing Then
                _Applicants = New Collection
                LoadApplicants()
            End If
            Return _Applicants
        End Get
    End Property

End Class

Public Class Applicant

    'empty class, just to demonstrate the Collection problem.

    Public Overrides Function ToString() As String
        Return "This is an Applicant"
    End Function

End Class

And to finish, some tips:
  * Better replace the identifier of class Application by another one because Application is a class of the .NET framework (namespace System) and you may get ambiguous identifier problems sooner or later.
  * Using method IsNothing() is better than comparing an object to Nothing because of the idealistic object oriented thought that checking whether an object is not instantiated could change in the future in which case it's easier to change it in the implementation of IsNothing() than change it EVERYWHERE in your code where you wrote If SomeObject is Nothing Then.
  * It is not advisable to add a lot of functionality to a property getter or setter.  Nobody (= other developers) ever will expect the property they use to do much more than what it's supposed to do: return a reference to the requested object.  So, the LoadApplicants() method call doesn't really belong in the getter.  In this case, I would create and load the collection in the constructor of the class, or even better: in an Initialize() method which is then called from the constructor or from outside the class.  The advantage of having a separate Initialize() method is that it can be stuffed with pure initialization code and can be called again and again later on.

With these things in mind, your class would look like:

Public Class ClsApplication

#Region "Fields"

    Protected _Applicants As Collection

#End Region

#Region "Constructors - Destructor"

    Public Sub New()
        _Applicants = New Collection
        ' Initialize other objects
        Initialize()
    End Sub

    Public Overridable Sub Dispose()
        _Applicants = Nothing
    End Sub

#End Region

#Region "Methods"

    Protected Overridable Sub LoadApplicants(ByVal AApplicants As Collection)
        For I As Integer = 0 To 4
            AApplicants.Add(New Applicant)
        Next
    End Sub

    Public Overridable Sub Initialize()
        LoadApplicants(_Applicants)
    End Sub

#End Region

#Region "Properties"

    Public Overridable ReadOnly Property Applicants() As Collection
        Get
            Return _Applicants
        End Get
    End Property

#End Region

End Class


Hope this helps, Thomas V
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question