CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Global variable

  1. #1
    Join Date
    Sep 2010
    Posts
    3

    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

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Global variable

    Quote Originally Posted by iliyan_84 View Post
    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!

  3. #3
    Join Date
    Sep 2010
    Posts
    3

    Re: Global variable

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured