Solved

How to declare an array of booleans within a .Net Form Application

Posted on 2006-06-23
16
281 Views
Last Modified: 2008-02-01
I have a Form App and need to show and hide picture boxes.  My problem is that I can't set the 'Visisble' state without declaring an array of booleans.  However I get this error when I declare the array within Form1.h:

bool PictureBoxStates[5];

PictureBoxStates: must explicitly specify __gc or __nogc for an array declared in a managed type

I need to assign all the values from a different array to the array stated above.  The other array is a normal array from an added .c file.  How do I declare the 'PictureBoxStates' array and how do I match the two?  'PicStatus' values would be the result of a normal array pulled in from an 'extern':

void Form1::HandlePictureBox1Update()
{
      pictureBox1->Visible = PictureBoxStates[0];            // set pictureBox visisble
}

void Form1::HandlePictureBox2Update()
{
      pictureBox2->Visible = PictureBoxsState[1];            // set pictureBox visisble
}

void Form1::HandlePictureBox3Update()
{
      pictureBox3->Visible = PictureBoxStates[2];            // set pictureBox visisble
}

void Form1::HandlePictureBox4Update()
{
      pictureBox4->Visible = PictureBoxStates[3];            // set pictureBox visisble
}

void Form1::HandlePictureBox5Update()
{
      pictureBox5->Visible = PictureBoxStates[4];            // set pictureBox visisble
}

void Form1::HandlePictureBox6Update()
{
      pictureBox6->Visible = PictureBoxStates[5];            // set pictureBox visisble
}



void Form1::SetPictureStatus(int SideNum, int LineUp, bool PicStatus)
{
       
      ....

      PictureBoxStates[0] = PicStatus1;
      if(pictureBox1->IsHandleCreated && pictureBox1->InvokeRequired){
            pictureBox1->Invoke( __gc new System::Threading::ThreadStart(this, &Form1::HandlePictureBox1Update));
      }
      
      PictureBoxStates[1] = PicStatus2;
      if(pictureBox2->IsHandleCreated && pictureBox2->InvokeRequired){
            pictureBox2->Invoke( __gc new System::Threading::ThreadStart(this, &Form1::HandlePictureBox2Update));
      }
      PictureBoxStates[2] = PicStatus3;
      if(pictureBox3->IsHandleCreated && pictureBox3->InvokeRequired){
            pictureBox3->Invoke( __gc new System::Threading::ThreadStart(this, &Form1::HandlePictureBox3Update));
      }
      PictureBoxStates[3] = PicStatus4;
      if(pictureBox4->IsHandleCreated && pictureBox4->InvokeRequired){
            pictureBox4->Invoke( __gc new System::Threading::ThreadStart(this, &Form1::HandlePictureBox4Update));
      }
      PictureBoxStates[4] = PicStatus5;
      if(pictureBox5->IsHandleCreated && pictureBox5->InvokeRequired){
            pictureBox5->Invoke( __gc new System::Threading::ThreadStart(this, &Form1::HandlePictureBox5Update));
      }
      

      return;
}

0
Comment
Question by:John500
16 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16968297
do __gc bool PictureBoxStates[5];

or __nogc bool PictureBoxStates[5];


its so .net knows to use garbage collection
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16968578
bool PictureBoxStates __gc[];

Initialize it in the class constructor:

void Form1::Form1()
{
     PictureBoxStates = new bool __gc[5];
}

Managed C++ syntax is so ugly. Move to C++/CLI.
0
 

Author Comment

by:John500
ID: 16973031
Jase-Coder/AlexFM,

Thanks for your input ! ........ I have to say, it has been difficult learning the nature of this beast!

I'm still getting an error.  Here's what I did:

public __gc class Form1: public System::Windows::Forms::Form
{

public:
   ...
   ...

private:

   __gc bool PictureBoxStates[5];
   ...
   ...

        void InitializeComponent(void)
        {
            ...
            ...         // in other words, I put nothing here

        }

private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
        PictureBoxStates = new bool __gc[5];
       ...
      ...
}

With this setup, I get:

C3150: 'bool' '__gc'  can only be applied to a class, struct, interface, array or pointer

Any ideas ?  Thanks !
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 48

Expert Comment

by:AlexFM
ID: 16974096
You still have error, because you didn't copy array declaration from my post:

bool PictureBoxStates __gc[];
0
 

Author Comment

by:John500
ID: 16975301
AlexFM,

I corrected the declaration --> bool PictureBoxStates __gc[];  ... but now I have this result:

error C3150:  'bool' '__gc' can only be applied to a class, struct, interface, array or pointer
warning C4200:  nonstandard extension used: zero-sized array in struct/union

(o o)
   L
  ~
0
 

Author Comment

by:John500
ID: 16975469
AlexFM,

I forgot to mention the error and waring is for this line:

bool PictureBoxStates __gc[];

Could this be related to the fact that the project is a Form Application or simply because I haven't placed this line in the correct area?  Could you take another look at my Form1.h above?  I left out 98 % of the code but it does represent the entire Form1.h file from top to bottom... I only included what I thought was the most important sections - starting of course with the very first line:

public __gc class Form1: public System::Windows::Forms::Form
...

You wrote:

>> Initialize it in the class constructor
>> void Form1::Form1()
>> {
>>     PictureBoxStates = new bool __gc[5];
>> }

Please note I tried putting this line in both of the sections below:

void InitializeComponent(void) // constructor
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)       // Form load
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16975899
bool PictureBoxStates __gc[];

This must be declared on class level, this is class member. I tested this with VS2005 with /oldsyntax switch, I beleive it should be the same in 2003 version.
0
 

Author Comment

by:John500
ID: 16976786
Using the outline of my Form1.h above, can you tell me where exactly the 'class' level is ?  Are you saying I have it where it needs to be, or that I need to move it ?

Also, can you paste your code that utilizes the 'switch' statement ?  Thank you very much !

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16977574
class Form1 : public Form
{
    bool PictureBoxStates __gc[];

    public: Form()
    {
           PictureBoxStates = new bool __gc[5];
    }
};
0
 

Author Comment

by:John500
ID: 16983925
If you will review the documentation provided on __gc arrays on MSDN (at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspe
c/html/vcManagedExtensionsSpec_4_5.asp) you will find that you cannot declare a __gc array of just any type.  The type of an element of a __gc array can only be a __value class, or a __gc pointer to a __gc class.

'bool' is not a __value class.

If you need to have a __gc array of 5 booleans instead of 5 individual booleans, you will have to write your own boolean __value class.
AlexFM/Jase-Coder,

I was able to find the answer on Microsoft.

If you are interested, take a look at the documentation on a __gc class on MSDN (at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspe
c/html/vcManagedExtensionsSpec_4.asp) you'll see that a __gc class can have a data member that has type "pointer-to" any unmanaged type.

This means I can replace

 __gc bool PictureBoxStates[5];
with
 bool* PictureBoxStates;

and then replace:
 
PictureBoxStates = new bool __gc[5];  
with
PictureBoxStates = new bool[5];

Thanks for your attempts.  I'm going to ask that this question be deleted and points returned.......

Thanks again.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16984120
This means that compiling in VS 2005 with /oldsyntax doesn't give exactly the same results as in 2003 version, my code was tested in this version.
Of course, you can delete this question.

Out of topic: move to 2005 version ASAP - this is my recommendation for everybody who works with Manaded C++. Managed C++ in 2003 version has lot of bugs, ugly syntax, it is signifcantly changed in 2005 version. Code written in 2003 version must be completely rewritten for using in future versions.
0
 

Author Comment

by:John500
ID: 16984320
... would that explain why similar and simple array usuage doesn't always work ?  If you are interested and can answer this 2nd question, I'll just give you the points.  More specifically, here is an old question I asked back in 1999 which contains the complete answer for working with a simple circular queue:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_10133148.html

.... for some reason only _one_ insert to the array is possible in this .Net environment:

Boolean insert(queueADT *q, dataType value)
     {

          if(!full(*q)){
               q->tail = (q->tail + 1)% MAX_Q_SIZE;
               q->elements[q->tail] = value;                        //  FAILS ON EVERY ATTEMPT AFTER THE 1st
               return TRUE;
          }
          else
               return FALSE;

     }

Why on earth would this assignment possibly fail ??  There's absolutely no reason ?
0
 

Author Comment

by:John500
ID: 16984794
The input file for the project mentioned above would look like this:

qjobs.txt  // or whatever you choose to name it

time         job                // header line not included in file
----------------------
0010        NJR25
0020        BH875
0030
0040        L89V2
0050

For every empty slot in time, the program would interpret this as a call to removeJob() or remove the head value from the circular queue ......
0
 

Author Comment

by:John500
ID: 17015732
RomMod,

In response to:

>> John500: If you have not responded to the Experts' comments, provided your solution,
>> or "given an updated status," your points will not be refunded.

My updated status remains the same in accordance with my post on:
Date: 06/26/2006 08:39AM EST

This post demonstrates how the answer was obtained by myself and the answer is undisputedly correct.

AlexFM writes:
>> Of course, you can delete this question.
RomMod writes:
>> PAQ / Refund

Agreed, points refunded.  As always, thank you for your assistance.

John
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17025521
Closed, 250 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
help with SQL 15 126
Visual Studio 2010 C++ Import Typelib Problem 3 350
how to change my public IP to any country.... 9 83
Tab order 16 79
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question