Code Help (C#)
This is may code. For some reason, you can't declare global variables, so how do
I stop the number from being twenty. USING THIS WAY. NOT A TEXT BOX OR SOMETHING.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinFormHello
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Ok_Click(object sender, EventArgs e)
{
MessageBox.Show("Name: " + username.Text);
Age = age.ToString();
MessageBox.Show("Age: " + age);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void plus_Click(object sender, EventArgs e)
{
int age;
age++;
}
private void minus_Click(object sender, EventArgs e)
{
int age;
age--;
}
}
}
Oh I wasn't wondering how to set it to twenty, I was wondering how to stop becoming 20 when you press the buttons.
what if you did a check like if(age >= 20) age--;
Also won't age be deleted once the plus_click and minus_click functions end?
Also won't age be deleted once the plus_click and minus_click functions end?
Ya I am a new programmer so I don't understand how to do this. If you have any ideas please share them. Thanks!
Merging what Bob said with your code would yield something like
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace WinFormHello{public partial class Form1 : Form{ int age = 20; // The age variable, set age to 20 initially public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void Ok_Click(object sender, EventArgs e) { MessageBox.Show("Name: " + username.Text); Age = age.ToString(); MessageBox.Show("Age: " + age); } private void Form1_Load(object sender, EventArgs e) { } private void plus_Click(object sender, EventArgs e) { // int age; <-- removed, we don't want to create a new age variable, we want to use the same one... age++; } private void minus_Click(object sender, EventArgs e) { // int age; <-- removed, we don't want to create a new age variable, we want to use the same one... age--; }}}
Do you have an explicit requirement to not use a text box? Because there's the NumericUpDown control that integrates a text box with the up/down buttons for you. Set NumericUpDown.ReadOnly to false to prevent the user from modifying the text box.
This is yet another reason why you should learn to program before trying to write a windows application. To Answer your question:
1) You declare Global Variables in C# as static class variables(i'm not sure i got the terminology right on that):
2) Do you know about how the CLR(Common Language Runtime) handles memory? When you declare a variable in a block it goes out of scope and is deleted. Example:
1) You declare Global Variables in C# as static class variables(i'm not sure i got the terminology right on that):
public class Class{ public static int ClassVar = 1; } public class OtherClass{ int i = Class.ClassVar;// you just have to reference the name of the class }
2) Do you know about how the CLR(Common Language Runtime) handles memory? When you declare a variable in a block it goes out of scope and is deleted. Example:
public class Class{ public void ExampleFunction(){ int i = 0; i++; } // i is now out of scope and cannot be referenced like this public static void Main(String args[]){ ExampleFunction(); // i is created but deleted after the function ends Console.WriteLine(i);// doesn't work }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement