Coffeehouse Thread

15 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Visual Basic.Net Reference vs Instantiate

Back to Forum: Coffeehouse
  • User profile image
    pdcjlw1

    I've got a question about using objects in Visual Basic 2008.

    If I do this:

    Dim joe as Guy

    This is a reference. In order to use joe I must also do this:

    Dim joe as New Guy

    This does what's called instantiate the object. What is the difference? Why do I have to do both before I can use the object? Are there reasons why I would only do the reference and not follow it up with instantiate? What really is going on inside the computer when I do each step? When is memory actually allocated?

    Thanks. I'm new at object oriented programming. I am used to more procedural languages like COBOL.

  • User profile image
    kettch

    @pdcjlw1:

    The reference created in the first example is indeed a reference, but it's empty. At that point joe is still null. This is useful if you are going to get the actual object from somewhere else and don't need to create a new one. Say, if you had a method that created a Guy object, you would do like so:

    Dim joe as Guy
    joe = GuyMethod()

    which is also the same as:

    dim joe as guy = GuyMethod()

    Really, you don't need to create a reference and instantiate it on different lines.

    This:

    Dim joe as New Guy

    is the same as:

    Dim joe as Guy
    joe = new Guy

    If you did both lines like you had in your example:

    Dim Joe as Guy
    Dim Joe as New Guy

    You would get a compiler error because you told it to create two separate variables with the same name.

    EDIT: On the subject of learning, you might check out this search and this search on MSDN. Some of the articles are a bit old, but the principles are sound.

    I think it's funny that when you include the language you want to use, you get a whole bunch of people obviously looking for people to do their homework for them.

  • User profile image
    pdcjlw1

    So at the point that I create the reference what occurs in the computer. Is there a variable created with no memory assigned?

    Also do you have a example of a method that would perform the instantiate? What would that look like?

     

    Thanks for your help.

  • User profile image
    ScanIAm

    ,pdcjlw1 wrote

    So at the point that I create the reference what occurs in the computer. Is there a variable created with no memory assigned?

    The reference takes up memory, too.  It's value is the 'where in memory is my variable located'.

    Think of all variables as 2 things, a location and a value. 

    Also do you have a example of a method that would perform the instantiate? What would that look like?

    Thanks for your help.

    Dim joe as Guy = New Guy()

    or, to break it up:

    Dim joe as Guy

    joe = New Guy()

     

  • User profile image
    kettch

    @pdcjlw1: A reference is just that. It's a pointer to another memory location where the data is held. Here's a good article on references. It's very old, but from a cursory look it seems like the concepts are still valid.

    An example of a method that instantiates a variable is somewhat simple. First you create the reference in your main class.

    Dim joe as Guy

    Then you have a method that creates a Guy object. Maybe it gets the data from a database or some other source:

    Function GetGuy()
        Dim tempGuy as New Guy()
        tempGuy.Name = "Fred"
        tempGuy.Age = 42
        tempGuy.IsAwesome = True
    
        return tempGuy
    end Function

    Then in your application somewhere you can put the result of that method into your empty reference like so:

    joe = GetGuy()

    Remember that VB can both create a reference and fill it in one line like this:

    Dim joe as Guy = GetGuy()

    EDIT: davewill pointed out that in VB only functions return values. I've edited the code for clarity.

  • User profile image
    spivonious

    Dim Joe as Something

    creates this in memory

    Joe ->

    Dim Joe as New Something

    creates this is memory

    Joe -> instance of Something

    Dim Joe as Something
    Dim Fred as New Something
    Joe = Fred

    creates this:

    Joe -> instance of Something <- Fred

    Both references are pointing to the same instance.

  • User profile image
    davewill

    @kettch: one minor edit.  Subs don't return values in VB.

    Function GetGuy() as Guy
        Dim tempGuy as New Guy
        tempGuy.Name = "Fred"
        tempGuy.Age = 42
        tempGuy.IsAwesome = True
        return tempGuy
    end function

  • User profile image
    davewill

    Expanding on spivonious ...

     

    Dim Joe as Guy
    'Joe is nothing at this point
    
    Dim Fred as New Guy
    'Fred is something, a newly initialized Guy (blank name, 0 age, not awesome)
    
    Joe = Fred
    'Now Joe points to the same thing as Fred
    
    Joe = GetGuy()
    'Now Joe points to a new instance of Guy (a initialized 42 year old awesome guy named Fred)
    'Fred still points to his original initialized Guy (blank name, 0 age, not awesome)
    
    Function GetGuy() as Guy
        Dim tempGuy as New Guy
        tempGuy.Name = "Fred"
        tempGuy.Age = 42
        tempGuy.IsAwesome = True
        return tempGuy
    End Function
    
    
    Private Class Guy
      'Property syntax shortened for brevity
      Public Property Name() as String
      Public Property Age() as Integer
      Public Property IsAwesome() as Boolean
    End Class
    

  • User profile image
    kettch

    ,davewill wrote

    @kettch: one minor edit.  Subs don't return values in VB.

    1
    2
    3
    4
    5
    6
    7
    Function GetGuy() as Guy
        DimtempGuy asNew Guy
        tempGuy.Name ="Fred"
        tempGuy.Age = 42
        tempGuy.IsAwesome =True
        return tempGuy
    end function

    Hah! You caught me. I've been working in c# for so long that the context switch defeated me.

  • User profile image
    davewill

    @kettch: nah!  i could tell you wrote it quick in a text editor.  mainly didn't want to OP to get off track.

  • User profile image
    kettch

    @davewill: It was hard enough too keep the semicolons out of there. Tongue Out

    Take a look at line two of the code in my quote of your comment. Does it look like the forum stripped out some spaces? I've seen that in other posts in the past few days, where a comment has had random spaces removed. Weird.

  • User profile image
    davewill

    @kettch: sure enough.  that's odd.  even odder that the human brain can subconsciously compensate.  i didn't even notice that until you pointed it out.

  • User profile image
    kettch

    ,davewill wrote

    @kettch: sure enough.  that's odd.  even odder that the human brain can subconsciously compensate.  i didn't even notice that until you pointed it out.

    I just went through the last few threads I've participated in, and the only quote boxes that have this problems are instances where people have quoted somethingI said.

    Curiouser and curiouser...

  • User profile image
    kettch

    ,kettch wrote

    *snip*

    I just went through the last few threads I've participated in, and the only quote boxes that have this problems are instances where people have quoted somethingI said.

    Curiouser and curiouser...

    Yargh! When I hit edit, there is definitely a space between "something" and "I"

  • User profile image
    PerfectPhase

    At times like this I like to fall back on this http://www.youtube.com/watch?v=6pmWojisM_E

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.