Home All Groups Group Topic Archive Search About

declare array with a default value?

Author
22 Sep 2006 11:03 AM
james
Hi,

Just looking here: http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx I
can't quite see what I want to do.
I want an array of Booleans of length 102, all initially "false".
I have this so far:

public bool[] Monitors = new bool[102];

but if I add on the end of that : {false}, I get a compile error of "Invalid
rank specifier, expected ',' or ']'.
Now, I presume there is a quicker way than typing
{false,false,false,false.... 100 times? :)
Or does it default to false anyway?

Author
22 Sep 2006 11:30 AM
Ciaran O''Donnell
Luckily for you, booleans are false by default anyway.

Ciaran O'Donnell

Show quoteHide quote
"james" wrote:

> Hi,
>
> Just looking here: http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx I
> can't quite see what I want to do.
> I want an array of Booleans of length 102, all initially "false".
> I have this so far:
>
> public bool[] Monitors = new bool[102];
>
> but if I add on the end of that : {false}, I get a compile error of "Invalid
> rank specifier, expected ',' or ']'.
> Now, I presume there is a quicker way than typing
> {false,false,false,false.... 100 times? :)
> Or does it default to false anyway?
>
>
>
Are all your drivers up to date? click for free checkup

Author
22 Sep 2006 11:50 AM
coolCoder
Hi,

> Or does it default to false anyway?

   You are right. When you initialize a new array of bool's all the
elements have default values set to False.


        private void button1_Click(object sender, EventArgs e)
        {

            bool[] arr = new bool[3];


            foreach (bool b in arr)
            {
                MessageBox.Show(b.ToString());
            }


        }

  Try this code in a windows application for a button click event. You
can change the array size.

Thanks.
Author
22 Sep 2006 11:54 AM
james
Show quote Hide quote
"coolCoder" <anant.yadunath.kulka***@gmail.com> wrote in message
news:1158925815.307129.118910@h48g2000cwc.googlegroups.com...
> Hi,
>
>> Or does it default to false anyway?
>
>   You are right. When you initialize a new array of bool's all the
> elements have default values set to False.
>
>
>        private void button1_Click(object sender, EventArgs e)
>        {
>
>            bool[] arr = new bool[3];
>
>
>            foreach (bool b in arr)
>            {
>                MessageBox.Show(b.ToString());
>            }
>
>
>        }
>
>  Try this code in a windows application for a button click event. You
> can change the array size.
>

Thanks to both replies - false is handy :)
My other option was to simply loop through and set them all to false before
the *useful* stuff that will happen, but that seemed a horrible waste of cpu
cycles. A Default of false suits me fine.

James.
Author
22 Sep 2006 12:10 PM
Morten Wennevik
Hi James,

All values types (int, bool etc) are set to their default level, which  =

usually is 0 or false.  References will be null.  If you want another  =

value you need to loop through and set it yourself.


On Fri, 22 Sep 2006 13:54:28 +0200, james <ja***@com.com> wrote:

Show quoteHide quote
>
> "coolCoder" <anant.yadunath.kulka***@gmail.com> wrote in message
> news:1158925815.307129.118910@h48g2000cwc.googlegroups.com...
>> Hi,
>>
>>> Or does it default to false anyway?
>>
>>   You are right. When you initialize a new array of bool's all the
>> elements have default values set to False.
>>
>>
>>        private void button1_Click(object sender, EventArgs e)
>>        {
>>
>>            bool[] arr =3D new bool[3];
>>
>>
>>            foreach (bool b in arr)
>>            {
>>                MessageBox.Show(b.ToString());
>>            }
>>
>>
>>        }
>>
>>  Try this code in a windows application for a button click event. You=

>> can change the array size.
>>
>
> Thanks to both replies - false is handy :)
> My other option was to simply loop through and set them all to false  =

> before
> the *useful* stuff that will happen, but that seemed a horrible waste =
of  =

> cpu
> cycles. A Default of false suits me fine.
>
> James.
>
>



-- =

Happy Coding!
Morten Wennevik [C# MVP]
Author
22 Sep 2006 12:16 PM
Noah Sham
Hi,

In general, if you wanted to initialize your array of value types to a
specific value other than the default you would need to loop through the
array.

   for (int x=0; x<Monitors.Length; ++x )
   {
    Monitors[x] = true;
   }

Show quoteHide quote
"james" <ja***@com.com> wrote in message
news:4513c30b$0$2511$fa0fcedb@news.zen.co.uk...
> Hi,
>
> Just looking here: http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx
> I can't quite see what I want to do.
> I want an array of Booleans of length 102, all initially "false".
> I have this so far:
>
> public bool[] Monitors = new bool[102];
>
> but if I add on the end of that : {false}, I get a compile error of
> "Invalid rank specifier, expected ',' or ']'.
> Now, I presume there is a quicker way than typing
> {false,false,false,false.... 100 times? :)
> Or does it default to false anyway?
>
Author
22 Sep 2006 12:48 PM
Bill Butler
"james" <ja***@com.com> wrote in message
news:4513c30b$0$2511$fa0fcedb@news.zen.co.uk...
> Hi,
>
> Just looking here:
> http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx I can't quite
> see what I want to do.
> I want an array of Booleans of length 102, all initially "false".
> I have this so far:
>
> public bool[] Monitors = new bool[102];
>

Have you looked into using the BitArray class instead of an Array of
bits?

    Bill
Author
22 Sep 2006 1:24 PM
james
"Bill Butler" <qwe***@asdf.com> wrote in message
news:wYQQg.4194$Bh.2872@trnddc03...
>
> Have you looked into using the BitArray class instead of an Array of bits?
>

I haven't - but I'll investigate. Presumably it's more efficient?
Author
23 Sep 2006 6:46 PM
Bill Butler
"james" <ja***@com.com> wrote in message
news:4513e3dc$0$11779$db0fefd9@news.zen.co.uk...
>
> "Bill Butler" <qwe***@asdf.com> wrote in message
> news:wYQQg.4194$Bh.2872@trnddc03...
>>
>> Have you looked into using the BitArray class instead of an Array of
>> bits?
>>
>
> I haven't - but I'll investigate. Presumably it's more efficient?

forgetting about overhead, an Array of bools takes up a whole byte for
every bit stored. It also has some constructors that ease
initialization.There are other features surrounding bit twiddling as
well.

    Bill



Post Thread options