C
C#ā€¢3d ago
TIGRIIII

I need to consult about this code!

Hello so I'm trying to write a code and i wrote in the properties a condition that if the username is null or empty to show a message which in this case it doesnt. Here is the code and thank you for your time!
No description
No description
24 Replies
Jimmacle
Jimmacleā€¢3d ago
well, you never actually use the setter but i also would not put this code in a setter, setters should be pretty simple validation and not do anything extra like printing
TIGRIIII
TIGRIIIIOPā€¢3d ago
can you give me an example if okay?
Jimmacle
Jimmacleā€¢3d ago
if i wanted to stop username from being set to an empty/null string i would check it and throw an exception if it's invalid it's about keeping responsibilities separate, your customer class doesn't have a good reason to know anything about the console
TIGRIIII
TIGRIIIIOPā€¢3d ago
Im not going to lie but im still not understanding
Angius
Angiusā€¢3d ago
You're setting the field Not the property So you completely bypass anything and everything the property has
TIGRIIII
TIGRIIIIOPā€¢3d ago
Oh okay thank you :d šŸ˜„ * I got it a little ill do more research in advance
TIGRIIII
TIGRIIIIOPā€¢3d ago
wrote this but still the same thing
No description
Angius
Angiusā€¢3d ago
Are you actually setting UserName? Or _userName? Also, your setter doesn't actually set anything now
TIGRIIII
TIGRIIIIOPā€¢3d ago
right now my head is about to explode and i dont know what i did more adecuate asnwer is i dont know what im setting
Jimmacle
Jimmacleā€¢3d ago
in your constructor you're setting _userName not UserName
TIGRIIII
TIGRIIIIOPā€¢3d ago
ohhhhhhhhhhhhh
Jimmacle
Jimmacleā€¢3d ago
this will not call the setter for UserName
TIGRIIII
TIGRIIIIOPā€¢3d ago
okay okay i got it thank you for clarifying šŸ˜„ just to make sure get its for (To get the data that is in the memory) and setter (Its about the user to be able to change the data)
Angius
Angiusā€¢3d ago
Basically $getsetdevolve
MODiX
MODiXā€¢3d 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; }
}
Angius
Angiusā€¢3d ago
Maybe this will clarify what getters and setters really are
TIGRIIII
TIGRIIIIOPā€¢3d ago
okay thank you understood šŸ˜„ thanks for your help
Angius
Angiusā€¢3d ago
:Ok:
TIGRIIII
TIGRIIIIOPā€¢3d ago
:harold:
Rizwan Riaz
Rizwan Riazā€¢17h ago
Is this a bot command? If yes, where could I get them?
Angius
Angiusā€¢16h ago
It is, yeah. All the tags should be listed on mod.gg, not sure if you need any special role to see them
Rizwan Riaz
Rizwan Riazā€¢15h ago
$getsetdevolve
MODiX
MODiXā€¢15h 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; }
}
Rizwan Riaz
Rizwan Riazā€¢15h ago
There are commands on mod.gg but I think this getsetdevolve does not require any command.

Did you find this page helpful?