Sign in to follow this  
spectre223

Variables in C#

Recommended Posts

Hello. I'm trying to make a Windows Form Application calculator as a beginner project in C#. I've already gotten it to work, so now I'm trying to shrink the code. My problem is, I'm having a hard time figuring out how to make variables in one namespace usable in another namespace. Can someone help me?

Share this post


Link to post
Share on other sites
Variables can't live in namespaces in C# (there is no exposed concept of globals). You can make public static members of a class, however:

namespace N {
public class C {
public static int SomeVariable = 0;
}
}

However, this might be an extremely bad idea depending on what you're actually trying to accomplish. Most likely you want to be passing instances back and forth between objects.

Why don't you explain what you're trying to actually accomplish by "sharing" these variables?

Share this post


Link to post
Share on other sites
Here's what I currently have set up.


namespace calculator
{
public partial class CalculatorInterface : Form
{
public CalculatorInterface()
{
InitializeComponent();
}
static double decDev = 10;
static bool dec;
static int step = 1;
static int opperation;
static double number;
static double answer;

private void button1_Click(object sender, EventArgs e)
{
if (step != 3)
{
if (dec == false)
{
number = number * 10 + 1;
}
if (dec == true)
{
number = number + (1 / decDev);
decDev = decDev * 10;
}
textBox1.Text = "" + number + "";
}
}



I want to move the if statements somewhere else, and then reference them.

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