C
C#•10mo ago
robs0271

Explanation of the Value parameter in Class constructor

Hello! I am following tutorials on Game Dev with C# and MonoGame to improve and practice my basic OOP programming knowledge with C#. I am trying to understand which is the meaning of "value" in the following, which I suppose to be a constructor: public ContentManager Content { get { return _content; } set { if (value == null) { throw new ArgumentNullException(); } _content = value; } } The class constructor pasted above is inside the class Game : IDisposable that comes from the Microsoft.Xna.Framework. Specifically, I don't understand where the "value" comes from and how it is handled, as it seems to me to be a parameter passed inside such a class constructor: At the same time, the constructor seems parameterless to me, as there is no () after the name of the constructor... I suppose I am missing some OOP key concepts in this regard; please help me to understand 🙂 More in general, this "Content {get;set;}" is called to load the content inside the game, in the LoadContent method of the Game.cs: protected override void LoadContent() {
target = Content.Load<Texture2D>("target");
No description
12 Replies
Thinker
Thinker•10mo ago
This is not a constructor, it is a property A property is essentially two methods, a method which gets a value, and a method to set a value. In this case you have a field called _content, which this property gets and sets the value of. $getsetdevolve
MODiX
MODiX•10mo ago
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
can be shortened to
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
can be shortened to
class Foo
{
public int Bar { get; set; }
}
class Foo
{
public int Bar { get; set; }
}
Thinker
Thinker•10mo ago
What this property is doing is in the setter it's confirming that the value passed into the property is not null, before actually setting the _content field to that value.
i like chatgpt
i like chatgpt•10mo ago
Imagine value is a "parameter" of set "method". Your original code
class Something
{
private ContentManager _content;
public ContentManager Content
{
get
{
return _content;
}

set
{
if (value == null)
{
throw new ArgumentException();
}
_content = value;
}

}
}
class Something
{
private ContentManager _content;
public ContentManager Content
{
get
{
return _content;
}

set
{
if (value == null)
{
throw new ArgumentException();
}
_content = value;
}

}
}
You can imagine that get is a method without parameter and set is a method with a parameter value of type ContentManager as follows. As it is just an imagination, the following does not compile!
class Something
{
private ContentManager _content;
public ContentManager Content
{
ContentManager get() // <===== This is the only difference I added!
{
return _content;
}

void set (ContentManager value) // <===== This is the only difference I added!
{
if (value == null)
{
throw new ArgumentException();
}
_content = value;
}

}
}
class Something
{
private ContentManager _content;
public ContentManager Content
{
ContentManager get() // <===== This is the only difference I added!
{
return _content;
}

void set (ContentManager value) // <===== This is the only difference I added!
{
if (value == null)
{
throw new ArgumentException();
}
_content = value;
}

}
}
robs0271
robs0271OP•10mo ago
Thank you so much for the explanations! These are really useful to me! 🙂
robs0271
robs0271OP•10mo ago
I have another doubt: this LoadContent method is where the Content property mentioned before is used. By hovering with the mouse to see the root, I see these two class names (ContentManager and Game) with a space between them. Game is the class where the Content property is defined, so it appears obvious to me that this is shown here. But what is the role of the ContentManager instead? Does the space imply a relationship between the two classes here?
No description
i like chatgpt
i like chatgpt•10mo ago
Edit: I think this answer does not answer your question right above. Just navigate to my answer right after this one. I keep this for your future reference about this.
class Game
{
private ContentManager _content;
public ContentManager Content
{
get
{
return _content;
}

set
{
if (value == null)
{
throw new ArgumentException();
}
_content = value;
}

}

protected override void LoadContent()
{
... = Content; // this line is just a simplified version of
... = this.Content; // this line
}
}
class Game
{
private ContentManager _content;
public ContentManager Content
{
get
{
return _content;
}

set
{
if (value == null)
{
throw new ArgumentException();
}
_content = value;
}

}

protected override void LoadContent()
{
... = Content; // this line is just a simplified version of
... = this.Content; // this line
}
}
- this word represents the instance of Game being constructed. - this word can only be used inside the class (in this example, Game class) Still confused? A Game instance hahaha created outside Game class
Game hahaha = new Game();
hahaha.Content = whatever;
Game hahaha = new Game();
hahaha.Content = whatever;
A Game instance this created automatically inside Game class - this is provided automatically and cannot be manually created! - so any attempt to do this = new Game(); is illegal, it does not compile!
class Game
{
//
{
... = this.Content; // or the simplest one just remove this
... = Content;
}
}
class Game
{
//
{
... = this.Content; // or the simplest one just remove this
... = Content;
}
}
i like chatgpt
i like chatgpt•10mo ago
No description
robs0271
robs0271OP•10mo ago
Great!!! Thank you so much @ABOUT ME explains my gameplay !!
i like chatgpt
i like chatgpt•10mo ago
Because I am not using Unity, I cannot answer with 100% accuracy for the meaning of
Microsoft.Xna.Framework.Content.ContentManager
Microsoft.Xna.Framework.Content.ContentManager
It can be any thing such as:
namespace Microsoft.Xna.Framework.Content
{
public class ContentManager;
}
namespace Microsoft.Xna.Framework.Content
{
public class ContentManager;
}
or
namespace Microsoft.Xna.Framework
{
public class Content
{
public class ContentManager;
}
}
namespace Microsoft.Xna.Framework
{
public class Content
{
public class ContentManager;
}
}
or
namespace Microsoft.Xna
{
public class Framework
{
public class Content
{
public class ContentManager;
}
}
}
namespace Microsoft.Xna
{
public class Framework
{
public class Content
{
public class ContentManager;
}
}
}
or others that I am lazy to write.
Jimmacle
Jimmacle•10mo ago
this is a property, not a constructor oh, chat jump
Want results from more Discord servers?
Add your server