Solved

When Object gets created...

Posted on 2012-03-15
4
252 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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 a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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