[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

When Object gets created...

Posted on 2012-03-15
4
Medium Priority
?
260 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 86

Accepted Solution

by:
Mike Tomlinson earned 800 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 34

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 600 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 600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

831 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