declare array with a default value?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • james

    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,fa lse,false.... 100 times? :)
    Or does it default to false anyway?


  • Ciaran O''Donnell

    #2
    RE: declare array with a default value?

    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,fa lse,false.... 100 times? :)
    Or does it default to false anyway?
    >
    >
    >

    Comment

    • coolCoder

      #3
      Re: declare array with a default value?

      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(o bject 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.

      Comment

      • james

        #4
        Re: declare array with a default value?


        "coolCoder" <anant.yadunath .kulkarni@gmail .comwrote in message
        news:1158925815 .307129.118910@ h48g2000cwc.goo glegroups.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(o bject 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.


        Comment

        • Morten Wennevik

          #5
          Re: declare array with a default value?

          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 <james@com.comw rote:
          >
          "coolCoder" <anant.yadunath .kulkarni@gmail .comwrote in message
          news:1158925815 .307129.118910@ h48g2000cwc.goo glegroups.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(o bject 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]

          Comment

          • Noah Sham

            #6
            Re: declare array with a default value?

            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.Leng th; ++x )
            {
            Monitors[x] = true;
            }

            "james" <james@com.comw rote 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,fa lse,false.... 100 times? :)
            Or does it default to false anyway?
            >

            Comment

            • Bill Butler

              #7
              Re: declare array with a default value?

              "james" <james@com.comw rote 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


              Comment

              • james

                #8
                Re: declare array with a default value?


                "Bill Butler" <qwerty@asdf.co mwrote in message
                news:wYQQg.4194 $Bh.2872@trnddc 03...
                >
                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?


                Comment

                • Bill Butler

                  #9
                  Re: declare array with a default value?

                  "james" <james@com.comw rote in message
                  news:4513e3dc$0 $11779$db0fefd9 @news.zen.co.uk ...
                  >
                  "Bill Butler" <qwerty@asdf.co mwrote in message
                  news:wYQQg.4194 $Bh.2872@trnddc 03...
                  >>
                  >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


                  Comment

                  Working...