Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
I am totally new to programming so please forgive me and english is not my first language. I am learning C++ in CLR format so I have the following:

test.h

Declared global variable int x;

C++
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{   
x = 5;
test ( x ) 

}

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
       test(x) -> Still cannot access the variable x from button1.. :(
}

Update: Created another function in cpp, called it over here in test.h

C++
Function int test ( int i)
{
int i;
return i;
}
Posted
Updated 29-Mar-15 22:23pm
v2
Comments
Sergey Alexandrovich Kryukov 30-Mar-15 0:38am    
There are no "global variables" in .NET. Use class/struct instance members. What are you trying to achieve?
—SA
justbegin123 30-Mar-15 0:49am    
Hi Sergey, what I am trying to achieve is , for example button1 variable x has a value of 5.

i want to access the x, which is 5, in button 2 to do something else.
Sergey Alexandrovich Kryukov 30-Mar-15 1:04am    
Wrong. There is no such thing as "button variable". Doing something with a variable cannot be your goal. What do you really want to achieve? Really, without mixing your goal with your thoughts on "how". First, "what"?
—SA
justbegin123 30-Mar-15 1:08am    
Alright, let me put it in another way.

Let's say I have an int x in button 1, when I clicks it, it will generate out a random number. I want to store that number into x, and redirects the number to button 2, to be able to print out the value. As mentioned, I am new, that might be an excuse, but definitely needing a guidance to start off. Thanks once again.
Sergey Alexandrovich Kryukov 30-Mar-15 1:12am    
What is "int x in button 1"? Button is a button, it does not have any int. If you want to generate a random number, do generate it. Your whole approach to thinking is wrong. Your goal cannot be storing something into x, because in write approach (if you are asking a question, you don't know write approach, isn't it logical?), x may or may not exist. Forget about those "variables", explain the behavior.
—SA

1 solution

It's a little bit difficult understanding exactly what you want, but I think it's something like this;

  1. When button 1 is pressed, store some value in a variable, somewhere.
  2. When button 2 is pressed, use the value stored by pressing button 1, somehow.


If that's the case something like this might help you get started;
C#
#include "stdafx.h"

#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

ref class MyForm : public Form {
private:
    Button^ button1;
    Button^ button2;
    Label^ label;
    int result;

public:
    MyForm();
    void OnClick1(System::Object ^sender, System::EventArgs ^e);
    void OnClick2(System::Object ^sender, System::EventArgs ^e);
};

// Constructor initializing the buttons and label, and adding them to the form
MyForm::MyForm() {
    Text = "My Form";

    button1 = gcnew Button();
    button1->Text = "Button 1 (add 1)";
    button1->SetBounds(10, 10, 100, 32);
    button1->Click += gcnew System::EventHandler(this, &MyForm::OnClick1);

    button2 = gcnew Button();
    button2->Text = "Button 2 (add 5)";
    button2->SetBounds(10, 50, 100, 32);
    button2->Click += gcnew System::EventHandler(this, &MyForm::OnClick2);

    label = gcnew Label();
    label->Text = "Result area";
    label->SetBounds(10, 90, 200, 32);

    Controls->Add(button1);
    Controls->Add(button2);
    Controls->Add(label);
}

// Click handler for button1, this will add 1 to the member variable result
void MyForm::OnClick1(System::Object ^sender, System::EventArgs ^e) {
    result += 1;
    label->Text = String::Format("Pressed 1, result is {0}", result);
}

// Click handler for button2, this will add 5 to the member variable result
void MyForm::OnClick2(System::Object ^sender, System::EventArgs ^e) {
    result += 5;
    label->Text = String::Format("Pressed 2, result is {0}", result);
}

int main(array<System::String ^> ^args) {
    MyForm^ form = gcnew MyForm();
    Application::Run(form);
    return 0;
}


The MyForm class holds variables for the buttons, but also a result variable that is shared by every method in the class. Thus, when button 1 or 2 modifies the value, the click-handler for the other button can always read it.

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900