Sign in to follow this  
terranman

Code Help (C#)

Recommended Posts

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--; } } }

Share this post


Link to post
Share on other sites
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--;
}
}
}

Share this post


Link to post
Share on other sites
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):


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
}
}







Share this post


Link to post
Share on other sites
Thank you so much. I hope I will be able to give back to gamedev someday.
One more favor. How do I show more than one message on a messagebox?

Share this post


Link to post
Share on other sites
Quote:
Original post by terranman
Why dosen't this work?

private void label3_Click(object sender, EventArgs e)
{
age.ToString();
label3.Text = age;
}
}
}


age.ToString() returns a string representation of age, it does not modify age itself. You want to be doing label3.Text = age.ToString();

Share this post


Link to post
Share on other sites
Quote:
Original post by terranman
Thanks man, how do you loop?


There are different kinds of loops...

while(this is true)
{
do this
}


while x is less than 10 add 1 to x and execute the code
for(x = 1; x < 10; x++)
{
do this
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this