sgt_pinky Posted February 24, 2005 Posted February 24, 2005 Hi, Just a quick question, that I can't seem to find an answer to. Is it possible to declare, for example, an array of Single(10), and set all the values to -1.0 without having to do a for loop? I know it's just a simple thing, but I am having to do this a fair bit in my code lately, so it would be handy. Cheers, Pinky Quote
HJB417 Posted February 24, 2005 Posted February 24, 2005 Array.SetValue is probably as good as it gets. I think the reason why there isn't a method that does what you want is in the case where the array is storing non-value type objects (aka references). public static void Initialize(Array array, ValueType value) { for(int i=0; i < array.Length; i++) array.SetValue(value, i); } Quote
Leaders snarfblam Posted February 24, 2005 Leaders Posted February 24, 2005 (edited) If you want to initialize all the elements and you know how many elements you will have before you compile you could do this: Dim Singles as Integer() {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1} If you don't know beforehand, but you want each element to have the same value, make a function to do it for you. 'Edit: I put sub instead of function by accident Public Function MakeMeAnArrayOfSinglesPlease(ArraySize As Integer, InitialValue As Single) As Single() '<--Edit: forgot return type Dim NewArray As Single() = New Single(ArraySize - 1) {} For i As Integer = 0 To ArraySize - 1 NewArray(i) = InitialValue Next i Return NewArray End Sub Edited February 24, 2005 by snarfblam Quote [sIGPIC]e[/sIGPIC]
*Experts* Nerseus Posted February 24, 2005 *Experts* Posted February 24, 2005 If you can use an ArrayList: ArrayList al = ArrayList.Repeat(-1, 10); If you're doing this a lot then you want a function: public sealed class ArrayUtil { private ArrayUtil() { } // private constructor - can't create public static int[] CreateArray(int size, int defaultValue) { // Make sure size is valid if (size <= 0) return null; int[] returnArray = new int[size]; for (int i = 0; i < size; i++) returnArray[i] = defaultValue; return returnArray; } public static decimal[] CreateArray(int size, decimal defaultValue) { // Make sure size is valid if (size <= 0) return null; decimal[] returnArray = new decimal[size]; for (int i = 0; i < size; i++) returnArray[i] = defaultValue; return returnArray; } } ... int[] i = ArrayUtil.CreateArray(10, -1); decimal[] d = ArrayUtil.CreateArray(10, -1M); For now, I don't think there's any way to avoid a for loop. If you have a fixed array size, like 4, you could do the following, but I'm guess you won't know the size. int[] i = new int[] { -1, -1, -1, -1 }; -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Leaders snarfblam Posted February 24, 2005 Leaders Posted February 24, 2005 Is it just me, Nerseus, or have you picked up a habit of repeating me? Quote [sIGPIC]e[/sIGPIC]
*Experts* Nerseus Posted February 24, 2005 *Experts* Posted February 24, 2005 I like to think that I'm just slower... Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.