Jump to content
  • Advertisement
Sign in to follow this  
terranman

Code Help (C#)

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Oh I wasn't wondering how to set it to twenty, I was wondering how to stop becoming 20 when you press the buttons.

Share this post


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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!