Results 1 to 9 of 9

Thread: Initialize Array Without Default Value *SOLVED*

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Initialize Array Without Default Value *SOLVED*

    Hi! I'm working on an art program, I have a 2D array of colors, and I need to find out how to set all the values to a specific color when I initialize it. (I want every element in the array to be black when this array is initialized, but it seems like the default value is Color[Empty] which looks transparent.) Is there any way to do this without a For/While loop?

    Code:
    Private Shared Tiles_Private(Width, Height) As Color
    Any help is appreciated, thanks!
    ~Nic
    Last edited by NinjaNic; Dec 2nd, 2016 at 01:47 PM.

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

    Re: Initialize Array Without Default Value

    No there is not. An array element is basically a variable. When you initialise an array by specifying the size, each element is Nothing by default, just like a variable. For reference types, Nothing corresponds to no object but for value types it corresponds to the default value for that type. It's just like if you were creating an array of Integers and each element would be 0 by default or a Boolean array would contains all False values by default. There would be options if you were creating a simple array but, for a 2D array, you're going to have to use nested For loops.

  3. #3

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Initialize Array Without Default Value

    There would be options if you were creating a simple array but, for a 2D array, you're going to have to use nested For loops.
    All right, thanks. I was afraid of this. What would my options be if I hypothetically had a simple array?

    And after thinking about it, a way I suppose a way I could solve this dilemma is to create another color class (Color2) with the default value of black, and creating an array of Color2.

    VB Code:
    1. ' A color with the default value of black.
    2.     Class Color2
    3.  
    4.         Private R As Int32 = 0
    5.         Private G As Int32 = 0
    6.         Private B As Int32 = 0
    7.         ReadOnly Property Color() As Color
    8.             Get
    9.                 Return Color.FromArgb(R, G, B)
    10.             End Get
    11.         End Property
    12.  
    13.     End Class

    Thanks,
    ~Nic

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Initialize Array Without Default Value

    Yep, that would almost solve it....except that then you'd end up with an array of Nothing, since that would now be an array of reference type. You'd still have to iterate through every index in the array and create a class for them, but those classes would now have the color. The Color structure is....a Structure, and therefore is a value type. You need to create a structure rather than a class to get it to act the way you want. You don't have to do the = 0, either, because Int32 would default to 0 anyways.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Initialize Array Without Default Value

    You need to create a structure rather than a class to get it to act the way you want. You don't have to do the = 0, either, because Int32 would default to 0 anyways.
    Thanks! So, is this what you meant? Literally change "Class" to "Structure," like this?

    Code:
    ' A color with the default value of black.
    Structure Color2
    
        Private R As Int32
        Private G As Int32
        Private B As Int32
        ReadOnly Property Color As Color
            Get
                Return Color.FromArgb(R, G, B)
            End Get
        End Property
    
    End Structure
    ...you'd end up with an array of Nothing...
    Is this because I would need to create a New Color2 in order for it to have a value?

    Thanks for the help!
    ~Nic

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Initialize Array Without Default Value

    Right. An array of reference types is an array of references. In other words, the actual array is really holding just the address of the actual object which is somewhere out on the heap. Until you create the objects, the array holds nothing. An array of value types holds the actual items, rather than just references to the actual items. So, as long as you use a value type, you'd get what you wanted, but a reference type would have to be created.

    I was thinking at first that a structure might not work, because it can't have a constructor without arguments, but that doesn't matter, as you already know, because you don't need a constructor at all. So, your Color2 structure would work fine. If you found it convenient, you may want to add a few other methods such as those found in the Color structure.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Initialize Array Without Default Value

    All right! Thanks a lot for the help!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Initialize Array Without Default Value *SOLVED*

    By the way, there's an option on the Thread Tools to mark the thread RESOLVED, though you can also do it via editing the first post, as you have done.
    My usual boring signature: Nothing

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Initialize Array Without Default Value

    I said back in post #2 that an array element is basically just a variable. When you declare a variable of a reference type, what is it's value? It's Nothing, right? It refers to no object. If an array element is like a variable then it follows that, for a reference type array, each element refers to no object by default.
    Quote Originally Posted by NinjaNic View Post
    What would my options be if I hypothetically had a simple array?
    Here's one:
    vb.net Code:
    1. Dim colors = Enumerable.Range(1, arrayLength).
    2.                         Select(Function(n) Color.Black).
    3.                         ToArray()

Tags for this Thread

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