473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

declare array with a default value?

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?
Sep 22 '06 #1
8 19684
Luckily for you, booleans are false by default anyway.

Ciaran O'Donnell

"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?
Sep 22 '06 #2
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.

Sep 22 '06 #3

"coolCoder" <an*********************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.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.
Sep 22 '06 #4
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.comwrote:
>
"coolCoder" <an*********************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.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.



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 22 '06 #5
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;
}

"james" <ja***@com.comwrote in message
news:45**********************@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?

Sep 22 '06 #6
"james" <ja***@com.comwrote in message
news:45**********************@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
Sep 22 '06 #7

"Bill Butler" <qw****@asdf.comwrote 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?
Sep 22 '06 #8
"james" <ja***@com.comwrote in message
news:45***********************@news.zen.co.uk...
>
"Bill Butler" <qw****@asdf.comwrote 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
Sep 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Glutinous | last post by:
I've been studying this for hours, searching the www & usenet, and still can't figure out why 'each' returns an array of four key/value pairs, when it looks like just two pairs would suffice... ...
5
by: Philipp | last post by:
Hello, I've got a class Lattice which is declared like that: // file: lattice.h class Lattice:public EventChooser{ public: Lattice(LatticeParameters* params); virtual ~Lattice(); //...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
13
by: MJ | last post by:
as topic, if i wan to create an array where the content of the array can be edited by form1 and form2, how i going to do it? for example the content of array is {1,2,3} form2 change the content...
3
by: Ling Chen Wu | last post by:
Hi, I need to call a dll function but have no idea how to declare the function in VB.NET In the .h file, it is declare as follows: int CALLBACK gscBsiGcReadTagList( IN ...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
12
by: Steve | last post by:
here's a quirk i can't seem to handle, just hack. since call-time by-reference is depreciated and i don't want to enable it in the php.ini, i'm kind of stuck when i want to pass userdata as an...
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.