-
Dec 1st, 2016, 11:58 PM
#1
Thread Starter
Addicted Member
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.
-
Dec 2nd, 2016, 12:45 AM
#2
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.
-
Dec 2nd, 2016, 02:01 AM
#3
Thread Starter
Addicted Member
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:
' A color with the default value of black. Class Color2 Private R As Int32 = 0 Private G As Int32 = 0 Private B As Int32 = 0 ReadOnly Property Color() As Color Get Return Color.FromArgb(R, G, B) End Get End Property End Class
Thanks,
~Nic
-
Dec 2nd, 2016, 10:21 AM
#4
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
-
Dec 2nd, 2016, 10:31 AM
#5
Thread Starter
Addicted Member
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
-
Dec 2nd, 2016, 12:24 PM
#6
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
-
Dec 2nd, 2016, 01:47 PM
#7
Thread Starter
Addicted Member
Re: Initialize Array Without Default Value
All right! Thanks a lot for the help!
-
Dec 2nd, 2016, 04:36 PM
#8
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
-
Dec 2nd, 2016, 06:22 PM
#9
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.
Originally Posted by NinjaNic
What would my options be if I hypothetically had a simple array?
Here's one:
vb.net Code:
Dim colors = Enumerable.Range(1, arrayLength). Select(Function(n) Color.Black). 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|