-
September 1st, 2010, 07:49 PM
#1
Global variable
Hi,
I will explain my problem in short. I make a window form application and add 3 buttons. When a press button 1. I want to create 8 or 16 or 32 combobox and i make this with follow code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
golfunctions=32;
array<System::Windows::Forms::ComboBox ^>^ pcom = gcnew array<System::Windows::Forms::ComboBox ^>(golfunctions);
x=28; y=106; j=0;
for(i=0;i<golfunctions;i++)
{
pcom[i]=(gcnew System::Windows::Forms::ComboBox());
this->SuspendLayout();
pcom[i]->FormattingEnabled = true;
pcom[i]->Items->AddRange(gcnew cli::array< System::Object^ >(3) {L"0", L"1", L"x"});
pcom[i]->Location = System:rawing::Point(x, (y+20));
pcom[i]->Size = System:rawing::Size(35, 20);
pcom[i]->TabIndex = 36;
Controls->Add(pcom[i]);
x+=35;
if((i==15)&&(j==0))
{
x=28;
j++;
y+=40;
}
}
This work fine! After that i want to take the choosen value ot each combobox when i press button 2 but
private: System::Void Add_Click(System::Object^ sender, System::EventArgs^ e) {
for(i=0;i<golfunctions;i++)
{
temp=System::Convert::ToChar(pcom[i]->SelectedItem);
.....
//if(pcom[i]->SelectedItem
}
}
i compile program and work to the moment when i press button 2 and
show error. i start the program in debbuge mode: at row
temp=System::Convert::ToChar(pcom[i]->SelectedItem);
show -pcom <undefined value>
pcom[i] error: index 'i' out of bound for pointer/array 'pcom'
My supposition it that i make pcom local variable and can`t use in range of button2
any i ideas who to make that ot how to find decision of this problem
-
September 2nd, 2010, 07:56 PM
#2
Re: Global variable
Originally Posted by iliyan_84
My supposition it that i make pcom local variable and can`t use in range of button2
Yes, exactly this is the case. The pcom you use in button1_Click() is local to that function and thus gets destroyed when the function returns. It should rather be a member of the form class (preferably a private one), what golfunctions appears to be already.
The fact that your Add_Click() compiles without error appears to indicate that there already is a pcom declared outside of button1_Click(), but it gets hidden by the declaration of a local variable with the same name, and thus remains uninitialized. Therefor it might be enough to change the line
Code:
array<System::Windows::Forms::ComboBox ^>^ pcom = gcnew array<System::Windows::Forms::ComboBox ^>(golfunctions);
in button1_Click() to
Code:
pcom = gcnew array<System::Windows::Forms::ComboBox ^>(golfunctions);
As a rule of thumb, OTOH, variables should be made "as local as possible". For example, I see no declaration of the variables x, y, i, and j in button1_Click(). I hope you merely left out these declarations for brevity, and they are not actually members of the form class or even real global variables.
And there are some other things about that program you should rethink IMO. Just to pick two:
Everytime you click button1, a new set of combo boxes is generated. Is this really intended?
I see no call to ResumeLayout() matching your call to SuspendLayout() in button1_Click().
Finaly, better use code tags next time you post code. It makes it much more readable.
Ah, and... Welcome to CG!
-
September 3rd, 2010, 10:13 AM
#3
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|