• Advertisement
Sign in to follow this  

Code Help (C#)

This topic is 3588 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
Ya I am a new programmer so I don't understand how to do this. If you have any ideas please share them. Thanks!

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

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
Jared just una projecta. Ill send it when im done.
I should listen but I want to make this for some reason.

Share this post


Link to post
Share on other sites
Why dosen't this work?

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

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

  • Advertisement