-4

It's been a long while since I have done anything with C# and I decided I wanted to give it a go again. Basically I want to create a class add some variables to it that I can reference outside of the class.

class classname (variablename)
{
   function
}

classname (varaiblename = value)

I understand this is probably very much incorrect but that is the gist of wha tI would like to do.

Thank you very much for your time.

nathan rivera
  • 225
  • 1
  • 3
  • 12
  • 3
    I think you should know how to use MSDN C# Reference. It's more important than the answer and here is the link https://msdn.microsoft.com/en-us/library/0b0thckt.aspx – MichaelMao Jul 11 '16 at 18:13
  • See http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing – Chris H. Jul 11 '16 at 18:16
  • In the future, try to limit the questions to specific problems you are having with the code you write. This question is more of a request for a tutorial without actually using that word openly, and that's off topic for SO. – sstan Jul 11 '16 at 18:20

2 Answers2

5

You're looking for Properties:

class Foo
{
    public int Bar { get; set;}
}

Foo obj = new Foo();
obj.Bar = 5;
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • 3
    the important part o fthis answer is the 'public' part of the property. Making a private property means it is only available within the class itself (not accessable outside as the OP requested) – Kenneth Garza Jul 11 '16 at 18:12
1

I think you need public static variables:

Let's say you've integer variable in class A and you want to control this variable from other classes. There's static variable term which let's you change value of static variables from other classes and it'll affect all of the instance of classes:

Here is my quick code:

public class A {
    public static int myValue = 5;
}

So, when you declare your variable like this, you can use and control this variable from other classes such as:

Console.WriteLine(A.myValue); or A.myValue += 5; 
Natig Babayev
  • 3,128
  • 15
  • 23