Solved

When Object gets created...

Posted on 2012-03-15
4
248 Views
Last Modified: 2012-03-15
I have the following code to instantiate an object:

    Private Sub butCreateCar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCreateCar.Click

        MyCar = New Car 'instatiate Car class as MyCar

        SetMyCarProperties() 'associate user data with MyCar object
'???????????
        DisalbeControlsLevel(True)

    End Sub

Question 1: If one of the properties for MyCar is not set yet (it can not pass validation included with Set), does the object MyCar gets created? I hope it doesn't.

Question 2: How can I check if MyCar is created at line marked by '???????????

This I need to have to execute "DisalbeControlsLevel(True)" if in fact the object is created. Something like:

        If Not IsNothing(MyCar) Then
            DisalbeControlsLevel(True)
        End If

Thank you.
0
Comment
Question by:Mike Eghtebas
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 37726331
(1) Yes...it was created in the line before.

(2) Instead of checking for creation, you should check for "validity".  Create a property that indicates if the instance is valid or not and set it from your SetMyCarProperties() method:
If MyCar.IsValid Then

Else

End If

Open in new window


(3) If you really want to go with the "Nothing" paradigm then you need to make the constructor PRIVATE so you can no longer use the "New" keyword with your class.  Instead, provide a SHARED function that accepts the parameters and either returns a valid instance of Car, or returns Nothing to indicate failure.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 37726419
I guess I need to include IsValid property with my Car glass in this case, is this what I need to do?

Also, I have three properties I am checking, ideally it will be much better to know the first bad data entry so I can send the focus to that text box on the form asking user to correct it.

Like:

        If MyCar.IsValid Then
            DisalbeControlsLevel(True)
        Else
            IF <txtMileage> then
               txtMileage.Focus()
           ElseIF <txtMPG> then
               txtMPG.Focus()
          Else
               txtGasLevel.Focus()
          End If
        End If

Mike
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 150 total points
ID: 37726723
You can add further properties like IsMileageValid, IsMPGValid etc and then use those in if conditions.
0
 
LVL 5

Assisted Solution

by:Aruiz04
Aruiz04 earned 150 total points
ID: 37726847
I would change the constructor of the MyCar to receive parameters, that way you validate your properties data before even declaring the MyCar Object.

Public MyCar(parameter1 as something, parameter1 as something, parameter3 as something)
End
'so when you check for the right parameters then you create it as
Dim theCar as new MyCar(par1, par2, par3)

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now