|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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? 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? > > > Hi,
> Or does it default to false anyway? You are right. When you initialize a new array of bool's all theelements 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.
Show quote
Hide quote
"coolCoder" <anant.yadunath.kulka***@gmail.com> wrote in message Thanks to both replies - false is handy :)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. > 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. 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] 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? > "james" <ja***@com.com> wrote in message Have you looked into using the BitArray class instead of an Array of 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]; > bits? Bill "Bill Butler" <qwe***@asdf.com> wrote in message news:wYQQg.4194$Bh.2872@trnddc03...> I haven't - but I'll investigate. Presumably it's more efficient?> Have you looked into using the BitArray class instead of an Array of bits? > "james" <ja***@com.com> wrote in message forgetting about overhead, an Array of bools takes up a whole byte for 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? every bit stored. It also has some constructors that ease initialization.There are other features surrounding bit twiddling as well. Bill
Binding bug regarding visible property.
Writing update back to MSAccess table? Closing IDbConnection Problems when writing a SMTP client and talk with the hotmail Implementing Nested Transaction in C# How to shutdown open network ports? Looking for good referencing on unmanaged code interop Excel SaveAs problem Forms Datagrid in CSharp |
|||||||||||||||||||||||