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
Solved

When Object gets created...

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

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

807 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