C
C#•11mo ago
Rj

Struggling with constructor

I don't understand why I'm getting a red line under the Pet constructor
No description
82 Replies
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
Can you hover over the red squiggle?
Rj
RjOP•11mo ago
No description
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
Can you paste the code here?
Rj
RjOP•11mo ago
namespace Ponis { abstract class Pet { int id; int health; int loyalty; int nutrition; int cleanliness; string name; } class Dog(string name) : Pet { int id; int health; int loyalty; int nutrition; int cleanliness; public string name; public Dog(int id, int health, int loyalty,int nutrition, int cleanliness,string name) { this.id = id; this.health = health; this.loyalty = loyalty; this.nutrition = nutrition; this.cleanliness = cleanliness; this.name = name; } } }
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
First problem that the properties are doubled
Rj
RjOP•11mo ago
wdym
Angius
Angius•11mo ago
Your Dog class has two constructors
Rj
RjOP•11mo ago
oh
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
Since you extends on Pet
Rj
RjOP•11mo ago
so how do I fix that
Angius
Angius•11mo ago
No description
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
You dont need these
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
No description
Angius
Angius•11mo ago
Those are fields Regardless, the issue is because of two constructors It tells you to call the one with fewer params from the one with more params Using base()
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
abstract class Pet2
{
int id;
int health;
int loyalty;
int nutrition;
int cleanliness;
public string name;
}

class Dog2 : Pet2
{
public Dog2(string name)
{
base.name = name;
}
}
abstract class Pet2
{
int id;
int health;
int loyalty;
int nutrition;
int cleanliness;
public string name;
}

class Dog2 : Pet2
{
public Dog2(string name)
{
base.name = name;
}
}
This one works fine but the id to cleanliness are null
Rj
RjOP•11mo ago
WAIT so have I been like an idiot
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
Have you used java before c#?
Rj
RjOP•11mo ago
python
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
Oh
Rj
RjOP•11mo ago
I was imagining it like I was passing in the name and the rest were gonna be set later
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
But you know access modifiers?
Rj
RjOP•11mo ago
set get? private public i dont know terms
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
There The default is public
Rj
RjOP•11mo ago
okay
Angius
Angius•11mo ago
The default is private C# defaults to the least-accessible modifier
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
OMG SORRY YES PRIVATE
Rj
RjOP•11mo ago
No description
Rj
RjOP•11mo ago
so um
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
In my mind im typing private 😄
Angius
Angius•11mo ago
Regardless, to explain the issue with constructors
public class Foo(string a, int b)
{

}
public class Foo(string a, int b)
{

}
is the same as
public class Foo
{
private string _a;
private int _b;
public Foo(string a, int b)
{
_a = a;
_b - b;
}
}
public class Foo
{
private string _a;
private int _b;
public Foo(string a, int b)
{
_a = a;
_b - b;
}
}
The fields of class Pet are private Private to Pet
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
abstract class Pet2 { public int id; public int health; public int loyalty; public int nutrition; public int cleanliness; public string name; } class Dog2 : Pet2 { public Dog2(string name, int id, int health, int loyalty, int nutrition, int cleanliness) { base.id = id; base.health = health; base.loyalty = loyalty; base.nutrition = nutrition; base.cleanliness = cleanliness; base.name = name; } } There
Angius
Angius•11mo ago
Classes that inherit from Pet will not have those fields So far as Dog is concerned, they don't exist
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
abstract class Pet2
{
public int id;
public int health;
public int loyalty;
public int nutrition;
public int cleanliness;
public string name;
}

class Dog2 : Pet2
{
public Dog2(string name, int id, int health, int loyalty, int nutrition, int cleanliness)
{
base.id = id;
base.health = health;
base.loyalty = loyalty;
base.nutrition = nutrition;
base.cleanliness = cleanliness;
base.name = name;
}
}
abstract class Pet2
{
public int id;
public int health;
public int loyalty;
public int nutrition;
public int cleanliness;
public string name;
}

class Dog2 : Pet2
{
public Dog2(string name, int id, int health, int loyalty, int nutrition, int cleanliness)
{
base.id = id;
base.health = health;
base.loyalty = loyalty;
base.nutrition = nutrition;
base.cleanliness = cleanliness;
base.name = name;
}
}
This is the simplest as it can get I think
Rj
RjOP•11mo ago
okay so is base the same as this?
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
Super
Rj
RjOP•11mo ago
or is this and base diff
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
this = this super = base
Rj
RjOP•11mo ago
OHHHH
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
Java -> c#
Rj
RjOP•11mo ago
okayyy never touched java ...
Angius
Angius•11mo ago
Also, if it's public it should be a property, not a field Just saying
Rj
RjOP•11mo ago
m so just wondering... how would I do that
Angius
Angius•11mo ago
$structure
MODiX
MODiX•11mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
Thats too complex for starters i think
Angius
Angius•11mo ago
This is the structure of a C# program
Rj
RjOP•11mo ago
I think I understand all of that
dabirbswerenoisyaround
dabirbswerenoisyaround•11mo ago
abstract class Pet2
{
public int id;
public int health;
public int loyalty;
public int nutrition;
public int cleanliness;
public string name;

public Pet2(string name, int id, int health, int loyalty, int nutrition, int cleanliness)
{
this.id = id;
this.health = health;
this.loyalty = loyalty;
this.nutrition = nutrition;
this.cleanliness = cleanliness;
this.name = name;
}
}

class Dog2 : Pet2
{
public string breed;

public Dog2(string breed, string name, int id, int health, int loyalty, int nutrition, int cleanliness) :
base(name, id, health, loyalty, nutrition, cleanliness)
{
this.breed = breed;
}
}
abstract class Pet2
{
public int id;
public int health;
public int loyalty;
public int nutrition;
public int cleanliness;
public string name;

public Pet2(string name, int id, int health, int loyalty, int nutrition, int cleanliness)
{
this.id = id;
this.health = health;
this.loyalty = loyalty;
this.nutrition = nutrition;
this.cleanliness = cleanliness;
this.name = name;
}
}

class Dog2 : Pet2
{
public string breed;

public Dog2(string breed, string name, int id, int health, int loyalty, int nutrition, int cleanliness) :
base(name, id, health, loyalty, nutrition, cleanliness)
{
this.breed = breed;
}
}
This is how you call the base constructor Remember that thats the simplest as it can get I'm not the best at c# but I hope i made things clear 😄
Rj
RjOP•11mo ago
okay so one more thing nevermind I figured it out thank you for the help
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Rj
RjOP•11mo ago
Okey Pls explain tho @TeBeCo
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•11mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
The Fog from Human Resources
Does that mean the class itself can be the constructor?
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Rj
RjOP•11mo ago
okay Thank you
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•11mo ago
see $structure for most C# usage
PascalCase
camelCase
MACRO_CASE
snake_case
kebab-case
Train-Case
PascalCase
camelCase
MACRO_CASE
snake_case
kebab-case
Train-Case
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
But do code conventions really matter too much if it's the style of the developer? People like me for example use camelCase on things like functions and variables and use PascalCase for classes so there is a clear seperation For me the key is consistency but I don't know what's right in that case
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
I see
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
Does my approach sound acceptable :SCtwocats:
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
:SCshocked:
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
I see Thanks
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
Then what is the correct convention for classes, functions, variables and event events* Does the bot have a command for that.
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•11mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
What abt baking Naming* Not baking. Cause I start all my events with "On" For example "OnClick"
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
:SCgetoutofmyhead: Great I can refactor half my code base
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
The Fog from Human Resources
I see

Did you find this page helpful?