C
C#13mo ago
Alltman

I want an attribute to increment each time an instance is created.

Everytime a new instance of Sleutel is made I want the _aantalInOmloop to increment.
16 Replies
Alltman
AlltmanOP13mo ago
public class Sleutel : Voorwerp
{
private int _deur;
public int Deur {
get
{
return _deur;
}
private set
{
if(value < 0)
{
throw new ArgumentException("Deurnummer moet positief zijn");
}
_deur = value;
}
}


private static int _aantalInOmloop;
public int AantalInOmloop
{
get
{
return _aantalInOmloop;
}
private set
{
_aantalInOmloop = value;
}
}

public Sleutel(string naam, double gewicht, int niveau, int deur): base(naam,gewicht, niveau)
{
Deur = deur;
_aantalInOmloop++;
AantalInOmloop = _aantalInOmloop;
}
public bool PastOp(int doelDeur)
{
return Deur == doelDeur;
}

public override string ToString()
{
return $"{base.ToString()} past op deur {Deur}. \nEr zijn {AantalInOmloop} sleutel(s) in omloop.";
}
public class Sleutel : Voorwerp
{
private int _deur;
public int Deur {
get
{
return _deur;
}
private set
{
if(value < 0)
{
throw new ArgumentException("Deurnummer moet positief zijn");
}
_deur = value;
}
}


private static int _aantalInOmloop;
public int AantalInOmloop
{
get
{
return _aantalInOmloop;
}
private set
{
_aantalInOmloop = value;
}
}

public Sleutel(string naam, double gewicht, int niveau, int deur): base(naam,gewicht, niveau)
{
Deur = deur;
_aantalInOmloop++;
AantalInOmloop = _aantalInOmloop;
}
public bool PastOp(int doelDeur)
{
return Deur == doelDeur;
}

public override string ToString()
{
return $"{base.ToString()} past op deur {Deur}. \nEr zijn {AantalInOmloop} sleutel(s) in omloop.";
}
Alltman
AlltmanOP13mo ago
No description
Alltman
AlltmanOP13mo ago
Marked numbers is what the output should be
Angius
Angius13mo ago
Make the _antalOolOop static and increment it in the ctor Ah, you already have it this way Well, it should work, then
MODiX
MODiX13mo ago
Angius
REPL Result: Success
class Foo
{
public static int Count { get; private set; } = 0;

public Foo()
{
Count++;
}
}

var f = new Foo[]{new(), new(), new(), new(), new()};

Foo.Count
class Foo
{
public static int Count { get; private set; } = 0;

public Foo()
{
Count++;
}
}

var f = new Foo[]{new(), new(), new(), new(), new()};

Foo.Count
Result: int
5
5
Compile: 451.997ms | Execution: 58.490ms | React with ❌ to remove this embed.
Angius
Angius13mo ago
Like so
Alltman
AlltmanOP13mo ago
public static int AantalInOmloop{get; set;}

public Sleutel(string naam, double gewicht, int niveau, int deur): base(naam,gewicht, niveau)
{
Deur = deur;
AantalInOmloop++;
}
public static int AantalInOmloop{get; set;}

public Sleutel(string naam, double gewicht, int niveau, int deur): base(naam,gewicht, niveau)
{
Deur = deur;
AantalInOmloop++;
}
like this A sleutel is a key btw but output is still key one... there are 2 keys, key two... there are 2 keys it needs to be key one... there is 1 key, key 2 ... there are 2 keys
Angius
Angius13mo ago
Output? Keys? What do you mean?
Alltman
AlltmanOP13mo ago
Create the domain class "Tool": -3 properties: name (mandatory, may not be changed), weight (in kg) and level (int) -constructor with 3 parameters (controls see description setter) -constructor with 2 parameters. Level is set to 1 by default here. -getter for each attribute -setter for the attribute:◦weight: must be a positive number, less than 1000 kg◦level: there are 10 levels in the game, numbered from 1 to 10◦no setter for name because name may not be changed. Name may not be null or empty. -ToString Object ... with weight ... (3 digits after comma) kg from level ... Create 2 subclasses: The class "Weapon": -2 extra attributes: force and used -constructor -getters for the extra attributes -setter for each extra attribute:◦force: is a positive number◦used: no check needed -weapons are only available in levels 1 through 5: provide a CheckLevel method for this -ToString (see below for the requested output) The class "Key": -2 extra attributes: door (int), number of the door to which the key fits and numberInOverflow:represents the number of keys present in the game -constructor -getters for the extra attributes -setter for door: must be a positive number -ToString (see below for the requested output) -method PastOp: returns true/false depending on the current key on the door given as a parameter fits or not Cui layer Write an ObjectApplication in which an array of Weapons and an array of Keys are created. Let the application display the objects created.Possible output: Weapon colt with weight 1,500 kg from level 3 and with force 6 not yet used. Weapon brown with weight 0,500 kg from level 1 and with force 23 already used. Key front door with weight 0,500 kg from level 3 fits on door 1. There are 1 key(s) in circulation. Key back door with weight 0,500 kg from level 1 fits on door 2. There are 2 key(s) in circulation. this is the assignment Expected output: Key front door with weight 0,500 kg from level 3 fits on door 1. There are 1 key(s) in circulation. Key back door with weight 0,500 kg from level 1 fits on door 2. There are 2 key(s) in circulation. Current output: Key front door with weight 0,500 kg from level 3 fits on door 1. There are 2key(s) in circulation. Key back door with weight 0,500 kg from level 1 fits on door 2. There are 2 key(s) in circulation.
Angius
Angius13mo ago
If you print all of that output after creating two keys, then that's how it'll look But still, not sure why you even need that property Why not just... keep the keys in a list? And count how many keys are in that list?
Alltman
AlltmanOP13mo ago
That would be easy but sadly enough that's not the assignment 😦
Angius
Angius13mo ago
Write an ObjectApplication in which an array of Weapons and an array of Keys are created.
Isn't it?
Alltman
AlltmanOP13mo ago
What is the command again to send my code? I will do it likethat
Angius
Angius13mo ago
$code
MODiX
MODiX13mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Alltman
AlltmanOP13mo ago
BlazeBin - puycivpuvdxa
A tool for sharing your source code with the world!
Want results from more Discord servers?
Add your server