2

I'm trying to assign a string to a variable in a separate class and then call on the variable later on in the code. How to write to variables in other classes and use them later on?

I have a public class called Player and a string variable called playerName declared as public and static. In my Program.cs I'm trying to assign the variable with Player.playerName = Console.ReadLine(); but, when I call on it later it returns nothing.

Paul
  • 4,160
  • 3
  • 30
  • 56
CalHoff
  • 21
  • 1
  • 6
    [google c# what is a property](https://www.google.com/search?q=c%23+what+is+a+property). You will also want to know about classes and instances. Basically it sounds like you need an [OOP](https://www.google.com/search?q=c%23+oop) tutorial. – Igor Jan 30 '19 at 16:21
  • 3
    You will need an *instance* of the class. – Andrew Morton Jan 30 '19 at 16:21
  • Possible duplicate of [What's the best way to store a group of constants that my program uses?](https://stackoverflow.com/questions/1724025/whats-the-best-way-to-store-a-group-of-constants-that-my-program-uses) – demo Jan 30 '19 at 16:22
  • @CalHoff: wow, that's a lot of code (edit your question) – Tim Schmelter Jan 30 '19 at 16:25
  • Basically I have a public class called "Player" and a string variable called "playerName" declared as public and static. In my Program.cs I'm trying to assign the variable with "Player.playerName = Console.ReadLine();" but, when i call on it later it returns nothing. – CalHoff Jan 30 '19 at 16:29
  • Could you also paste your code into your question, please. Just click the [edit](https://stackoverflow.com/posts/54444989/edit) button. – Paul Jan 30 '19 at 16:37
  • 1
    You are much more likely to get an answer if you show us the code you are using. Show a snippet of the `Player` class that shows the "string variable" (it is more likely a _field_ or a _property_ of the class). And show us the "Program.cs" code where you set and read it back. You can also explain what you mean by "nothing". `Nothing` is a keyword in Visual Basic (it's what C# calls `null`), but it has no meaning in C#. Are you getting `null` or an empty string? – Flydog57 Jan 30 '19 at 16:39
  • 1
    @CalHoff: Can you show the code you are using to both assign & read back the string. – PaulF Jan 30 '19 at 16:43
  • Dare I ask why you've declared the string as `static`? – Paul Jan 30 '19 at 16:43
  • @PaulF - Andrew Morton commented before my edit and the OP's comment. – Paul Jan 30 '19 at 16:44
  • 1
    @CalHoff: if you do add the code - do it by editing your question rather than adding a comment. You need to start each line with 4 spaces for it to be highlighted as code & retain formatting. – PaulF Jan 30 '19 at 17:03

1 Answers1

4

Could you please provide code example or describe better your problem?

One generic solution can be:

class Example
    {
        public string MyProperty { get; set; }
    }

Then:

//instance of the class
Example example = new Example();
//set the property:
example.MyProperty = "value";
//access the property:
example.MyProperty
meta4
  • 788
  • 1
  • 10
  • 24
  • 5
    Me neither, but you could flesh out your answer a little, @meta4. Also, I would describe how to access the property from within the class and how to pass the class as an argument to a function or method. The problem with the OP is that there's not enough detail; Calhoff is obviously completely new to OOP. – Paul Jan 30 '19 at 16:31
  • 1
    That's a start, @meta4, but just adding a couple more words to your comment lines isn't enough - keep going... – Paul Jan 30 '19 at 16:40
  • It should also be pointed out OP said the string was declared as static (after you posted this to be fair). – PaulF Jan 30 '19 at 16:44
  • @PaulF but also he mentions it's 'playerName' for 'Player' which does not make much sense to be static. – meta4 Jan 30 '19 at 16:48
  • Then you should explain why it doesn't make sense and what to do instead. – Fildor Jan 30 '19 at 16:55
  • 1
    In all probability it should not be static, but if we saw the code then we could advise on that with more knowledge of the application. I would tend to try to answer the initial question as well as advise on improvements. As described, whether or not the field is static, the field should contain a value. – PaulF Jan 30 '19 at 17:01