Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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);
}

  • Leaders
Posted (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 by snarfblam
[sIGPIC]e[/sIGPIC]
  • *Experts*
Posted

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

"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
  • *Experts*
Posted
I like to think that I'm just slower...
"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...