Working with arrays

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

is there a better way to fill an array then this

a is a auto generated value, always different.
its meant to loop through until the array is full of values, will this work?

int i=0;
while(array[array.Length-1] == null)
{
array = a;
i++;
}
 
Thaynann said:
is there a better way to fill an array then this

a is a auto generated value, always different.
its meant to loop through until the array is full of values, will this work?

int i=0;
while(array[array.Length-1] == null)
{
array = a;
i++;
}


Well, this would be nicer:

for (int i=0; i < array.Length; i++)
{
array = a;
}

I don't think there's anything in the framework to fill an array
though.
 
Thaynann,

In addition to John sometimes called even a little bit better,

\\\
foreach (arrayType a) in array
{
a = x;
}
///
Cor
 
Not really, this code does nothing, you are simply assigning a local
variable (a) over and over, the array remains unchanged.
 
Actually, this code doesn't even compile, in a foreach the loop variable is
declared as Read-Only.
 
Not really, this code does nothing, you are simply assigning a local
variable (a) over and over, the array remains unchanged.
Yes as it is in the sample from Jon and Thaynmann the same, and what is
explained by Thanynmann.

Can you tell me your intentions with this message?

Cor
 
Yes as it is in the sample from Jon and Thaynmann the same, and what is
explained by Thanynmann.
Can you tell me your intentions with this message?

Sorry, but the code you posted does not make sense, in any language I know
anyways (C#, VB.NET, C++, J#), you can go ahead and test it if you want, .

My intention was simply to save Thaynann some time. Sorry if you took it
personal but after all the purpose of this newsgroup is to save people some
time, if you think I'm wrong simply post a sample using your code that does
what Thaynann wanted.

Peace
 
Condo said:
See this doc ...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csre
f/html/vclrftheforeachstatement.asp

Microsoft advises against altering members while in a foreach, however as
long as the member variable is a 'Reference' you could get away with it.
Value types will be treated as read only.

Just to be accurate, the variable is treated as read-only whether it's
a reference type or a value type. Don't forget that the value of a
reference type variable isn't the object itself, it's the reference.
 
Back
Top