Results 1 to 8 of 8

Thread: Why is this true?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    45
    Here is a question I recently answered on a practice Transcender test.

    Which Statement below actually causes the object to be created?

    1) Dim MyObject as New MyClass
    2) MyObject.Name = "MyObject"
    3) Set MyObject = CreateObject(MyClass)

    Well, I picked #1 but the correct answer is #2.

    So I check in my text book and find the following statement:
    "When you create an instance of a control, such as a text box, and set the name property to txtThing, Visual Basic responds as if the following statement had just exectuted:"

    Dim txtThing as New TextBox

    Well, I've placed TextBoxes on my form WITHOUT changing the name property and I've used them so I KNOW they've been "created".

    Obviously there is a little "trick" here. Does the NEW statement just set aside space for a reference to an object that isn't created until you use it in code or what? Why is this question even important? There must be some key I'm missing here.
    -Gregg
    -NoOBie At LaRg3

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    Unhappy dim..

    when you dim a variable (i guess in objects only)
    that it doesnt create object
    you have to set the object
    example

    dim rs as adodb.recordset

    if you set a sql to that recordset
    it wotn work
    unless you have

    set rs = new adodb.recordset before you use it
    so the object is not created until you make a new instance of it

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    45

    Not True.

    If you'll look in my example I DO use the New Keyword.
    Looky what I found in the VB documentation though:

    "If you declare an object variable with the New keyword, Visual Basic will automatically create a new object the first time you use the variable. For more information, see "Declaring an Object Variable.""

    In other words, the New statment doesn't create the object, you have to actually use the variable and upon its FIRST USE it is brought into existence. The New Keyword allows the object to be created automatically the first time the variable is used, but doesn't create anything itself.

    Odd.
    -Gregg
    -NoOBie At LaRg3

  4. #4
    Addicted Member
    Join Date
    Jul 1999
    Location
    St-Élie d'Orford, Quebec, Canada
    Posts
    133
    Hi,

    if you are wondering why your txtbox have been created since you have not changed there name, try openning a form with controls on it in something like WordPad...

    You will then see that every control you drag and drop on your form is CODED in the form and VB gives it a default name so, it is always created when you put it in a form.

    I would also say that the new statement only reserves memory space for a further referenced objet...

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    ya but

    Dim MyObject as New MyClass
    you cant use that until you set it
    like using SET =

    dont know man
    am more confused than i was
    hehe
    good luck

  6. #6
    New Member
    Join Date
    Aug 2000
    Location
    Tampa, FL
    Posts
    7

    Angry

    There is definitely a trick to the question...I chose number 1 also based on what I have read in the past. Below is copied from MSDN online library.

    You can use the New keyword with the Dim statement to declare an object variable of a specific type. If you include the New keyword in your variable declaration, you automatically create a new object and point the object variable to it. If you declare an object variable by using the New keyword, you don't need to use the Set statement.

    By using the New keyword, you can create an object variable to point to any type of object. The New keyword is used most frequently to create a new instance of a class or to create a new Collection object, as shown in the following example.

    ' Create object variable and point it to new object.
    Dim colInstances As New Collection



  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    OK! There are two ways to create an object in VB; CreateObject or by using the New keyword.
    The New keyword can be used in two ways.
    Either like this:
    Code:
    Dim MyObj As MyObject
    Set MyObj = New MyObject
    The first line just declare an object variable that can be created as MyObject. The second line is actually creating the object.

    The second way you can use the New keyword is like this:
    Code:
    Dim MyObj As New MyObject
    This declare the MyObj object variable so it can be created as a MyObject object, but it doesn't create the object.
    When using this syntax the object is created when it is first used.
    That is when you set any properties or call any methods of the object.
    This way of instancing an object is a little slower then using the first way (first declare the object and then use the New keyword) because every time you use the object VB has to check if the object is created or not.

    So in the 3 different possible answers given in the test only the second will actually create the object.
    The third alternative could have been the right answer, but the syntax is wrong.

    I hope this clarifies things for you.
    Best regards

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    45

    To Joacim

    Joacim,

    You are my hero.
    Thanks.
    -Gregg
    -NoOBie At LaRg3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width