1

In My Code I want to Re-Use My buttons, Since I dont want to Deal with tons of them... But the Variables that I use to Change the Events of the buttons Are not Transferring... How would I make A variable Global enough so that If it is changed by one button, Say X = 1, It can be Used by another button, So X = 1 in that button as well? Here is my Code for the two buttons... int ^ YesOrNo Has already been initialized outside of the two buttons, making them accessible but not change-able by buttons...

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
this -> button4 -> Visible = true;
this -> button2 -> Visible = false;
this -> button5 -> Visible = true;
this -> button3 -> Visible = false;
this -> label2 -> Visible = true;
this -> label2 -> Text = "Are you Sure?";
YesOrNo = 1;
}

 private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
  if (YesOrNo == 1)
  {
     this -> label2 -> Text = "To Make your Pony Character, See PonyMakin.txt in your... etc;                                                  
     button4 -> Text = "Show Pony";
     YesOrNo = 2;
  }
  else if (YesOrNo == 2)
  {
    this -> pictureBox3 -> Load("PonyCharacter.jpg");
  }
}

I have already Declared the int ^ YesOrNo; outside the two buttons, So both have access to it, But when one button changes YesOrNo to 1, The other button will still only see it as if were just initialized! I know that It is probably because both buttons are Void, But I don't know how to change those either...

Hal Canary
  • 2,154
  • 17
  • 17
Darksly
  • 17
  • 7
  • 1
    English: You should use capital letters only at the beginning of a sentence, or for names. Not after every comma. – Elazar May 12 '13 at 22:59
  • If these buttons are part of the same form, they should have access to the same variable. –  May 12 '13 at 23:00
  • I already specified that they have access to the same variable, What I said I NEEDED was that when a variable is changed from 0 to 1 by one button, That action is not recognized by any other button... So button 2 will not work since when button 1 changes YesOrNo to equal 1, the second button does not recognize it and therefore will not execute its own code... – Darksly May 12 '13 at 23:07
  • your YesOrNo is local inside button2 click . make it global thats all . – qwr May 12 '13 at 23:11
  • 1
    @Elazar: The capitalisation appears to be somewhat randomly scattered ("Are", "Used", etc.), rather than just after punctuation marks. I must say, I've never seen anyone write this way. – Marcelo Cantos May 12 '13 at 23:13
  • By global do you mean I should initialize It outside of both buttons? because I already did that, It still wont work, Like I said I have reason to believe that since both buttons are void, They are not allowed to make any changes to variables AND return them at the same time... If that is not what you mean, then what do you mean by Global? – Darksly May 12 '13 at 23:13
  • when you define variable inside block it be used as local . on your code remove int before YesOrNo inside button 2 click . – qwr May 12 '13 at 23:16
  • @QWR: Recommending the use of a global is terrible advice. Did you perhaps mean, "make it a class member"? – Marcelo Cantos May 12 '13 at 23:19
  • Sorry @QWR I have edited it so that It Is correct now, but the buttons still can not change the variables and be used by other buttons... – Darksly May 12 '13 at 23:22
  • i recommend him just make that variable to be seen both events . c++ cli it be forms member. for all purpose then he can try singleton approach – qwr May 12 '13 at 23:23
  • @user2375912: There is nothing about a function returning void (why use the awkward synonym `System::Void`, btw?) that prevents it from accessing member variables. – Marcelo Cantos May 12 '13 at 23:25
  • which variables ? did u post whole code – qwr May 12 '13 at 23:25
  • I use Microsoft Visual c++ to make these, for some reason it naturally adds Void functions at the beginning of each button... I am also Is there another option besides that? – Darksly May 12 '13 at 23:29
  • @QWR No, I did not post the entire code because It would have been a bit to long, Plus I only have this problem to deal with... All other code works fine – Darksly May 12 '13 at 23:31
  • @QWR Nope, changing the way I spell YesOrNo is not helping the fact that the Button4 will not execute its code – Darksly May 12 '13 at 23:34
  • this->YesOrNo . sorry my celly keyboard sucks . – qwr May 12 '13 at 23:37
  • @QWR Button4 will still not execute its code... Changing it to this -> YesOrNo, did not change the non-execution problem – Darksly May 12 '13 at 23:43
  • @user2375912 did it worked after my post ? i tested and it worked fine . – qwr May 13 '13 at 00:27
  • @user2375912: Then don't worry about the `System::Void`. It's obviously just an idiosyncrasy of the code generator. – Marcelo Cantos May 13 '13 at 01:07

1 Answers1

1

replace int^ with int . all will work . and see ^ its meaning from this link What does the caret (‘^’) mean in C++/CLI?

Community
  • 1
  • 1
qwr
  • 3,660
  • 17
  • 29