C
C#6mo ago
HD-Fr0sT

✅ Is there a way to add if statements and bools in a class?

is there a way or do i have to work around it?
16 Replies
Denis
Denis6mo ago
What do you mean $details
MODiX
MODiX6mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
HD-Fr0sT
HD-Fr0sT6mo ago
ok lets say we have a class and there is a an int array wich can be turned off and on
Angius
Angius6mo ago
What do you mean "turned off and on" The class either has an array field/property, or it does not have that property You can't toggle it
HD-Fr0sT
HD-Fr0sT6mo ago
like interactable and not interactable is there a way to add a new property by an if statement?
Angius
Angius6mo ago
So... some sort of
class Foo
{
public bool Interactable { get; set; } = true;
public List<int> Stuff { get; set; }
}

var f = new Foo();
f.Stuff.Add(9);
f.Interactable = false;
f.Stuff.Add(8); // error
class Foo
{
public bool Interactable { get; set; } = true;
public List<int> Stuff { get; set; }
}

var f = new Foo();
f.Stuff.Add(9);
f.Interactable = false;
f.Stuff.Add(8); // error
? No As I said, a class either has the property, or it does not have this property A car has a wheel count A ceiling fan does not have a wheel count Properties describe... the properties of the object An object either has a given property, or it does not There's nothing to be conditional here
HD-Fr0sT
HD-Fr0sT6mo ago
btw do you wanna kow what im actually doing and why i am asking? know
Angius
Angius6mo ago
Sure, it does seem like an XY issue
HD-Fr0sT
HD-Fr0sT6mo ago
basically im making a prototype script for operations of firearms like if its open bolt or auto or semi an array for magazine(catridge)
Angius
Angius6mo ago
Ah, then you would use inheritance
HD-Fr0sT
HD-Fr0sT6mo ago
also some operation have a chamber and some dont(i want make it possible to turn it off or on)
Angius
Angius6mo ago
abstract class Gun
{
public string Name { get; init }
}

class BoltActionGun : Gun
{
public double BoltLength { get; set; } // idk
}

class FullAutoGun : Gun
{
public double FireRate { get; set; }
public int AmmoCount { get; set; }
}
abstract class Gun
{
public string Name { get; init }
}

class BoltActionGun : Gun
{
public double BoltLength { get; set; } // idk
}

class FullAutoGun : Gun
{
public double FireRate { get; set; }
public int AmmoCount { get; set; }
}
That way, a FullAutoGun and a BoltActionGun can have different properties While still sharing some common properties of Gun
HD-Fr0sT
HD-Fr0sT6mo ago
thanks
Denis
Denis6mo ago
$close
MODiX
MODiX6mo ago
Use the /close command to mark a forum thread as answered
Buddy Rail
Buddy Rail6mo ago
You could do it with a constructor in the for the variable you wanted to toggle.
class Foo
{
public bool Interactable { get; set; } = true;

private List<int> _stuff;
public List<int> Stuff
{
get => _stuff;
set { if (Interactable) _stuff = value; }
}
}
class Foo
{
public bool Interactable { get; set; } = true;

private List<int> _stuff;
public List<int> Stuff
{
get => _stuff;
set { if (Interactable) _stuff = value; }
}
}
But inheritance is definitely the way to go.