Creating fixed Size array and Initialising using new in VB.net

Answered Creating fixed Size array and Initialising using new in VB.net

  • Friday, May 11, 2012 5:32 PM
     
     

    structure myStColor
     dim R as integer
     dim G as integer
     dim B as integer
    end structure

    dim myColorTable(255) as myStColor

    How to initialise the members of each myColorTable at once ?

    The solution I know will be

    for int i = 0 to 255
     myColorTable(i) = new myStColor

    next i

    Is there any other solution by which we can initialise the array members using new?



    • Edited by jovipl Friday, May 11, 2012 5:33 PM
    • Edited by jovipl Friday, May 11, 2012 5:33 PM
    •  

All Replies

  • Friday, May 11, 2012 7:51 PM
     
     Proposed Answer

    When you declare the  array and initialize it with 'New', then all of the Structures in the array are initialized to their default values, zero.  If you wish each member to have a unique value, then you will have to iterate the collection and assign individual values to the members.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • Saturday, May 12, 2012 5:03 AM
     
     

    Hi,

    I understood that if you are creating a structure variable , it should be initilised using new.

    So my concern is bout looping through the arrays

    for int i = 0 to 255
     myColorTable(i) = new myStColor

    next i

    Consider the case , in your application , you have differnt such kind of arrays of strurures of differnt size, the looping for initialising itself will be expensive .Is n't it?

    So in that case it will be usefull create the array members using new in a single step, instead of looping.

    I wanted to pass this arry to a function where I will get the members assigned with some values....

    When debugging , it shows the members are shown as "nothing" if its not initialsed using the loop.

  • Saturday, May 12, 2012 9:07 AM
     
     Answered

    In .Net when you create a variable in a method, the variable is using an area of memory called the Stack. When you create a Structure, the value of the structure is stored on the Stack. So if you say Dim i As Integer = 5, then you will set 32 bits on the stack with the value 5. Variables always get default values, so for integer if you say Dim x As Integer, then it will have the value 0 even though you didn't say New.

    If you are creating an instance of a Class and assigning it to a variable, then things are slightly more complicated. The actual object gets created in another area of memory - the Heap, and the stack just stores the address of this object - called a Reference. Classes are Reference types, Stacks are Value types - because variables on the stack store the value of a structure, but the reference to an instance of class. This makes things faster when passing things around (calling other methods)- passing a small reference is easier than the large amount of memory that the object itself takes up.

    So, when you Dim f As New Form, f will hold a reference on the stack, the reference is used to locate a Form object on the Heap. If you just say Dim f As Form without the New, then you just get the default reference value - 0. In VB.Net we call this default value Nothing.

    Arrays are similar. The Array itself is a Reference type, but it stores things in the same way as a stack - inside the array object will be either the values of value types, or references to Reference types.

    Dim forms() As Form

    This creates a variable for a form array, but the variable hold just the null reference because no array object was created yet.

    Dim forms As New Form(9){}

    Dim forms(9) As Form

    Either syntax will create a new array object on the heap, the array object will have room to store 10 references to Form objects, indexed from 0 to 9 inclusive. These will be initialised to the null reference (Nothing). We did not use the New keyword the second time because the syntax is special - if you say how many elements you want then you don't need to say new.

    Dim ages(9) As Integer

    This syntax will create a new array object on the heap, the array object will have room for 10 integers. These integers get the default value of 0.

    If you want to assign lots of values to the array, then it will required looping or using array literal syntax -
    Dim x() As Integer = {1, 2, 3, 4, 1000}
    Dim x As New Integer(){1, 2, 3, 4, 1000}
    Dim x = {1, 2, 3, 4, 1000} ' type inference

    When debugging , it shows the members are shown as "nothing" if its not initialsed using the loop.

    Your code shows an array of structures, all the fields of the structure are value types (R, G, B are Integer which is a value type), so there is nothing that can hold a null reference. You might have a null reference for the array itself if you have declared the array without New or without an upper bound - it is a reference type. If not, then hovering over the array members will call the default implementation of ToString, which will show the Type of the elements in the array - so in your case it would show something like "WindowsApplication1.Form1.myStColor". So I can't explain what you are seeing, maybe there's a typo in the code, or you changed myStColor to a Class to see what happened.

    (Note: arrays are always fixed size. Redim is lying to you - it doesn't alter the existing array object, it creates a new one with the new size)

    • Marked As Answer by jovipl Saturday, May 12, 2012 11:20 AM
    •  
  • Saturday, May 12, 2012 11:18 AM
     
     

    Hi all Sorry,

    My Type is  not structure .Its a class

    Classs myStColor
     dim R as integer
     dim G as integer
     dim B as integer
    End class

  • Sunday, May 13, 2012 6:40 PM
     
     

    If you use a class, then the you will need to create individual object instances and assign them to the array.  Period.  Your object is small enough that you could use a Structure, instead of a Class, because it will consume less than 16 bytes.  Your object would consume 12 bytes, 4 for each Integer.  A reference to an object on the stack would consume 16 bytes.

    You have not indicated whether or not you want the members to have identical 'values' or different 'values'.

    Rudy    =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/