C
C#ā€¢2mo 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ā€¢2mo 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ā€¢2mo ago
can you give me an example if okay?
Jimmacle
Jimmacleā€¢2mo 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ā€¢2mo ago
Im not going to lie but im still not understanding
Angius
Angiusā€¢2mo ago
You're setting the field Not the property So you completely bypass anything and everything the property has
TIGRIIII
TIGRIIIIOPā€¢2mo ago
Oh okay thank you :d šŸ˜„ * I got it a little ill do more research in advance
TIGRIIII
TIGRIIIIOPā€¢2mo ago
wrote this but still the same thing
No description
Angius
Angiusā€¢2mo ago
Are you actually setting UserName? Or _userName? Also, your setter doesn't actually set anything now
TIGRIIII
TIGRIIIIOPā€¢2mo 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ā€¢2mo ago
in your constructor you're setting _userName not UserName
TIGRIIII
TIGRIIIIOPā€¢2mo ago
ohhhhhhhhhhhhh
Jimmacle
Jimmacleā€¢2mo ago
this will not call the setter for UserName
TIGRIIII
TIGRIIIIOPā€¢2mo 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ā€¢2mo ago
Basically $getsetdevolve
MODiX
MODiXā€¢2mo 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ā€¢2mo ago
Maybe this will clarify what getters and setters really are
TIGRIIII
TIGRIIIIOPā€¢2mo ago
okay thank you understood šŸ˜„ thanks for your help
Angius
Angiusā€¢2mo ago
:Ok:
TIGRIIII
TIGRIIIIOPā€¢2mo ago
:harold:
AnarchyNim
AnarchyNimā€¢2mo ago
Is this a bot command? If yes, where could I get them?
Angius
Angiusā€¢2mo ago
It is, yeah. All the tags should be listed on mod.gg, not sure if you need any special role to see them
AnarchyNim
AnarchyNimā€¢2mo ago
$getsetdevolve
MODiX
MODiXā€¢2mo 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; }
}
AnarchyNim
AnarchyNimā€¢2mo ago
There are commands on mod.gg but I think this getsetdevolve does not require any command.

Did you find this page helpful?