C
C#•5mo 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
Can you hover over the red squiggle?
Rj
Rj•5mo ago
No description
dabirbswerenoisyaround
Can you paste the code here?
Rj
Rj•5mo 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
First problem that the properties are doubled
Rj
Rj•5mo ago
wdym
Angius
Angius•5mo ago
Your Dog class has two constructors
Rj
Rj•5mo ago
oh
dabirbswerenoisyaround
Since you extends on Pet
Rj
Rj•5mo ago
so how do I fix that
Angius
Angius•5mo ago
No description
dabirbswerenoisyaround
You dont need these
Angius
Angius•5mo 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
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
Rj•5mo ago
WAIT so have I been like an idiot
dabirbswerenoisyaround
Have you used java before c#?
Rj
Rj•5mo ago
python
dabirbswerenoisyaround
Oh
Rj
Rj•5mo ago
I was imagining it like I was passing in the name and the rest were gonna be set later
dabirbswerenoisyaround
But you know access modifiers?
Rj
Rj•5mo ago
set get? private public i dont know terms
dabirbswerenoisyaround
There The default is public
Rj
Rj•5mo ago
okay
Angius
Angius•5mo ago
The default is private C# defaults to the least-accessible modifier
dabirbswerenoisyaround
OMG SORRY YES PRIVATE
Rj
Rj•5mo ago
No description
Rj
Rj•5mo ago
so um
dabirbswerenoisyaround
In my mind im typing private 😄
Angius
Angius•5mo 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
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•5mo ago
Classes that inherit from Pet will not have those fields So far as Dog is concerned, they don't exist
dabirbswerenoisyaround
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
Rj•5mo ago
okay so is base the same as this?
dabirbswerenoisyaround
Super
Rj
Rj•5mo ago
or is this and base diff
dabirbswerenoisyaround
this = this super = base
Rj
Rj•5mo ago
OHHHH
dabirbswerenoisyaround
Java -> c#
Rj
Rj•5mo ago
okayyy never touched java ...
Angius
Angius•5mo ago
Also, if it's public it should be a property, not a field Just saying
Rj
Rj•5mo ago
m so just wondering... how would I do that
Angius
Angius•5mo ago
$structure
MODiX
MODiX•5mo 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
Thats too complex for starters i think
Angius
Angius•5mo ago
This is the structure of a C# program
Rj
Rj•5mo ago
I think I understand all of that
dabirbswerenoisyaround
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
Rj•5mo ago
okay so one more thing nevermind I figured it out thank you for the help
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Rj
Rj•5mo ago
Okey Pls explain tho @TeBeCo
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•5mo 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;
}
}
Somgör from Human Resources
Does that mean the class itself can be the constructor?
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Rj
Rj•5mo ago
okay Thank you
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•5mo 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•5mo ago
Message Not Public
Sign In & Join Server To View
Somgör 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•5mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Somgör from Human Resources
Does my approach sound acceptable :SCtwocats:
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Somgör from Human Resources
:SCshocked:
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Somgör from Human Resources
I see Thanks
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Somgör 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•5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•5mo 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•5mo ago
Message Not Public
Sign In & Join Server To View
Somgör from Human Resources
What abt baking Naming* Not baking. Cause I start all my events with "On" For example "OnClick"
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Somgör from Human Resources
:SCgetoutofmyhead: Great I can refactor half my code base
Unknown User
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View