[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

When Object gets created...

Posted on 2012-03-15
4
Medium Priority
?
257 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
[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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

650 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