Coffeehouse Post

Single Post Permalink

View Thread: Visual Basic.Net Reference vs Instantiate
  • User profile image

    @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.