0

If I have a class in C# and I want a variable to be accessible to another class, which is the better way to do it?

1: Use a public static variable.

public static int staticInteger;

2: Use a public static property.

static int staticInteger;

public static int StaticInteger
{
    get
    {
        return staticInteger;
    }
    set
    {
        staticInteger = value;
    }
}
Ben
  • 1,299
  • 3
  • 17
  • 37
  • 5
    Always use a property over a field. This has been discussed many many times. Just google it. – rory.ap Feb 24 '16 at 15:56
  • 1
    For public access, properties are pretty much always the way to go. Note that since your property has no advanced logic, you can shorten your property to `public static int StaticInteger {get; set;}` – Glorin Oakenfoot Feb 24 '16 at 15:56
  • 6
    You don't make something static just because it's used by another type. You make something static when it's something that conceptually can only ever exist once for an application, and the entire idea of there being two of that thing is clearly wrong. It needing to be accessible from another class is a reason to make it *public*, but not necessarily *static*. – Servy Feb 24 '16 at 15:58
  • property is the way to go. – Ian Feb 24 '16 at 15:58
  • @Servy no where in his post does he say he needs two of them. We don't know his intentions behind why it's static. Maybe he does only want it once, we don't know. Your comment has nothing to do with his post. – Dispersia Feb 24 '16 at 16:05
  • @Servy But I can only access a public property if I have access to an instance of the class, correct? – Ben Feb 24 '16 at 16:05
  • @Ben Yes, you would. And if the data is conceptually tied to the instance, rather than the type itself, that's exactly what you should do. – Servy Feb 24 '16 at 16:06
  • @Dispersia If someone walked up to you and said, "I'm trying to figure out how to commute to work, but all of the plane flights flying out of the airport near me leave too late in the day for me to get to work on time" you wouldn't consider inquiring as to whether or not there might be a more applicable form of transportation for that behavior? Even if there isn't, and that really is necessary, it's *extremely* likely that there is a better approach as to make inquiring about it worthwhile. – Servy Feb 24 '16 at 16:09
  • @GlorinOakenfoot And will the shortened property recognise the specific variable that I'm trying to get and set? – Ben Feb 24 '16 at 16:09
  • @Ben That field would become obsolete. You would remove it if you used that approach. – Servy Feb 24 '16 at 16:10
  • @Servy Right. So I'd just have the property on its own and no accompanying variables? – Ben Feb 24 '16 at 16:11
  • @Ben Yes. That's the whole *point* of the change, to not need an explicitly defined field whenever you create a property. The backing field will be automatically generated by the compiler, along with a getter and setter to interact with it. – Servy Feb 24 '16 at 16:13
  • @Servy no, that's like someone walking up to you and saying 'I'm going to commute to work' and you say you should take a bus instead. He didn't say why he has it, what he's doing with it, or why it exists. There are plenty of reasons to use a static variable - your comment is out of the blue. – Dispersia Feb 24 '16 at 16:15
  • @Dispersia No, your analogy *doesn't* apply. He didn't ask, "how do I access data from class X in class Y", he asked which type of static member should be used. And while it's *possible* that the member should be static, it's *highly implausible* that it should be static, and the question states that he's using it in a situation where it's typically not correct, so I gave him enough information to evaluate his situation and determine if it really is correct, given that he didn't seem to understand the implications of what he was doing. – Servy Feb 24 '16 at 16:19
  • @Servy Your second sentence (He didn't ask...) exactly proves why my analogy is more correct than yours. The question is whether or not he should use a public accessor and has not a thing to do if he should use static or not. But I'm not continuing this discussion, this is becoming even more off-topic than your original post. – Dispersia Feb 24 '16 at 16:22
  • @Dispersia And that would be an entirely valid criticism if I posted an answer. I wasn't asserting that my comment answered his question, I was pointing out a likely problem that he had unrelated to the question he asked. That's an appropriate thing to do in a comment, but it's not an answer. – Servy Feb 24 '16 at 16:24

0 Answers0