- Home /
Proper use of c# properties?
Hey guys I have a question about Getters/Setters. I need to know if I am doing it correctly. Or is there a better way? Just trying to make my code more professional. Thanks!!!!
//GameManager
public void CreateChar()
{
GameObject tempChar = GameObject.CreatePrimitive(PrimitiveType.Cube);
CharManager tempComponent = new CharManager();
tempComponent = tempChar.AddComponent<CharManager>();
tempComponent.NameOfCharacter = "Random Name";
}
//CharManager that is referenced
//Header
private string nameOfCharacter;
public string NameOfCharacter
{
get
{
return nameOfCharacter;
}
set
{
nameOfCharacter = value;
}
}
Answer by _foa · Mar 02, 2015 at 08:25 PM
It looks ok to me, maybe you can use an autoproperty (see: https://msdn.microsoft.com/en-us/library/bb384054.aspx)
For a more detailed discussion with many examples about properties you can see: https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx
I hope it helps :)
This is great! I thought there was an other way as opposed to assigning Char$$anonymous$$anager tempComponent = new Char$$anonymous$$anager();
Thanks for the quick reply!!!
I just have one question, On my char$$anonymous$$anager how would I be able to initilize it so it is not new all the time? Like this?
//$$anonymous$$ain.cs
public void CreateChar()
{
GameObject tempChar = GameObject.CreatePrimitive(PrimitiveType.Cube);
Char$$anonymous$$anager tempComponent = new Char$$anonymous$$anager("This", 1);
Char$$anonymous$$anager.Instance.CheckName();
}
//Char$$anonymous$$anager.cs
public string NameOfCharacter { get; set; } public int HealthOfCharacter { get; set; }
public Char$$anonymous$$anager(string name, int health)
{
NameOfCharacter = name;
HealthOfCharacter = health;
}
public void CheckName()
{
Char$$anonymous$$anager character = this;
//character.HealthOfCharacter = Random.Range(10, 100);
Debug.Log(character.NameOfCharacter);
}
Just a small thing on your first question code, I think you can do:
Char$$anonymous$$anager tempComponent = tempChar.AddComponent<Char$$anonymous$$anager>();
In$$anonymous$$d of:
Char$$anonymous$$anager tempComponent = new Char$$anonymous$$anager();
tempComponent = tempChar.AddComponent<Char$$anonymous$$anager>();
Anyway, if you need a single instance class (a class that is initialized only once and shared across different objects) you can take a look at the Singleton pattern: http://en.wikipedia.org/wiki/Singleton_pattern and for a C# implementation of the pattern see: http://csharpindepth.com/Articles/General/Singleton.aspx
From the code posted on your comment it is not quite clear to me what your intentions are, as I can't see how Char$$anonymous$$anager.Instance is declared. Also, in the CheckName() method, it is redundant assigning this to a temporary variable to access the class members, you can use the member name directly it is more clear to read.
If you do not want an object be different all the time instantiating it all the time with new is not the correct way to avoid that as new instantiates a new object every time :)
Hope I've been clear, let me know :)
Your answer
Follow this Question
Related Questions
Accessing class properties through getters & setters in editor mode 1 Answer
Properties names, same as class okay or bad? 1 Answer
Get and Set difference 2 Answers
problems with setters and getters. 1 Answer
getter and setter not working 1 Answer