Results 1 to 5 of 5

Thread: Doubt...Need explaination

  1. #1

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Doubt...Need explaination

    I just saw a piece of code with following two lines. Can anyone explain what does that mean? I understand the first line but what does 2nd line do?
    Code:
    temp = new byte[2];
    byte[] temp2;

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Doubt...Need explaination

    The first line creates a byte array with 2 elements and assigns it to the temp variable. The second line declares the temp2 variable as type byte array. That means that, just like temp, temp2 is a byte array variable to which you can assign a byte array object. Before the first line temp was a byte array variable that didn't refer to any object. After the first line temp is a byte array variable that does refer to an array object. After the second line temp2 is a byte array variable that doesn't refer to any object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Re: Doubt...Need explaination

    That was confusing
    After reading 2-3 times....I got it
    Thanks.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Doubt...Need explaination

    It's important to understand the distinction between variables and objects. When you declare things with names in your code, like temp and temp2, you are declaring variables. Variables have names. Variables are stored in memory on the stack. Variables can either contain a value, like numbers or dates, or they can contain a reference to an object.

    An object is something else. Objects don't have names and they are stored in memory on the heap. When you create an object you may or may not assign it to a variable. Assigning an object to a variable means storing the object's memory address in the variable, thus the variable refers to the object. Multiple variables can refer to the same object because they can contain the same memory address. Variables can also refer to no object, in which case they are called a null reference.

    Arrays are objects in .NET, thus they are stored on the heap. In order to create an array object you must specify, either explicitly or implicitly, the number of elements it contains. The system cannot create the array if it doesn't know big to make it.

    Like I said, the first line creates an object and assigns it to an existing variable, while the second line simply declares a variable with no object involved.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Re: Doubt...Need explaination

    Good explanation. I understand it now. Thanks a lot.

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