Log In

Your email is not your username

Register

If you were a member of the old Bodybuilding.com forums and would like to reuse your previous username, you can request it below. We use your email only for registration and do not store it. For more information, please see our Privacy Policy.

Confirm your email

A registration code was sent to your email. Enter it here.

Welcome

You have successfully setup your account.

Sign in

Quick Navigation Bottom Misc
Forum
ยป c# brahs please help me
  1. Results 1 to 8 of 8
post 1620087691 10-22-2020, 07:18 AM
-
#1
  1. darkeminence
  2. Registered User
  1. darkeminence
  2. Registered User
  3. Join Date: May 2007
  4. Age: 37
  5. Posts: 2,760
  6. Rep Power: 2174

c# brahs please help me

Ok I'm an utter noob in c#. Could anyone tell me why the variable in the content of the label doesn't get updated when I try to increment it in the method?
https://www.codepile.net/pile/yMdW2j8W

So here's what I'm thinking is happening: I declare the variable globally. Next, the event handler Window_Loaded put the text in the label, together with the value 0 of the int. Then when I try to click the button, it should increment the value of the variable in the label with 1. Why is this not happening brahs?
post 1620089111 10-22-2020, 07:35 AM
-
#2
  1. dcbone30
  2. Registered User
  1. dcbone30
  2. Registered User
  3. Join Date: Mar 2012
  4. Age: 39
  5. Posts: 14,563
  6. Rep Power: 73108
Why are you declaring it globally and not locally?

Alsowww.stackexchange.com
post 1620089651 10-22-2020, 07:45 AM
-
#3
  1. Lucas5814
  2. Registered User
  1. Lucas5814
  2. Registered User
  3. Join Date: Apr 2020
  4. Age: 56
  5. Posts: 5,030
  6. Rep Power: 30809
Where's your closing bracket for MainWindow?
post 1620090141 10-22-2020, 07:51 AM
-
#4
  1. Lucas5814
  2. Registered User
  1. Lucas5814
  2. Registered User
  3. Join Date: Apr 2020
  4. Age: 56
  5. Posts: 5,030
  6. Rep Power: 30809
I'm not a C# developer, but I work a lot with visual basic.

I'm guessing here: should you include some code to refresh the window? Is the count being set to 0 everytime the window updates/refreshes after a click?

My guess is, everytime you click, count is adding one, but the window is refreshing, and the first declaration is "count = 0", so it's just always going to be 0.

Again, I'm not a C# developer, so I don't know how this window stuff works.
post 1620090721 10-22-2020, 07:57 AM
-
#5
  1. darkeminence
  2. Registered User
  1. darkeminence
  2. Registered User
  3. Join Date: May 2007
  4. Age: 37
  5. Posts: 2,760
  6. Rep Power: 2174
Originally Posted By dcbone30
Why are you declaring it globally and not locally?

Alsowww.stackexchange.com
Well I have to, since it's used in those 2 methods, don't I? If it were used in one method, I could declare it locally.
post 1620099201 10-22-2020, 09:22 AM
-
#6
  1. darkeminence
  2. Registered User
  1. darkeminence
  2. Registered User
  3. Join Date: May 2007
  4. Age: 37
  5. Posts: 2,760
  6. Rep Power: 2174
Originally Posted By darkeminence
Well I have to, since it's used in those 2 methods, don't I? If it were used in one method, I could declare it locally.
bump
post 1620100721 10-22-2020, 09:37 AM
-
#7
  1. NiceGuy901
  2. Registered User
  1. NiceGuy901
  2. Registered User
  3. Join Date: Apr 2014
  4. Age: 35
  5. Posts: 540
  6. Rep Power: 2473
try this

Code:public partial class MainWindow : Window
{
public int count = 0;

private void btnMakeReservation_Click(object sender, RoutedEventArgs e)
{
this.count += 1;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
lblLabel.Content = $"Let's count: {count}";
}
}
post 1620101991 10-22-2020, 09:48 AM
-
#8
  1. darkeminence
  2. Registered User
  1. darkeminence
  2. Registered User
  3. Join Date: May 2007
  4. Age: 37
  5. Posts: 2,760
  6. Rep Power: 2174
Originally Posted By NiceGuy901
try this

Code:public partial class MainWindow : Window
{
public int count = 0;

private void btnMakeReservation_Click(object sender, RoutedEventArgs e)
{
this.count += 1;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
lblLabel.Content = $"Let's count: {count}";
}
}
Doesn't work... Don't get it.
Quick Navigation Top Misc
Bookmarks
Digg.com
Digg
del.icio.us
del.icio.us
Stumbleupon.com
StumbleUpon
Google.com
Google
Facebook.com
Facebook
Posting Permissions
  1. You may not post new threads
  2. You may not post replies
  3. You may not post attachments
  4. You may not edit your posts