C
C#17mo ago
laurapigeon

Advice for how best to adapt a Python habit to C#

(This is code for a Game of Life board's Populate method, that aims to create a 2D array of tiles based on a percentage living rate, or entered pattern) The way I used to do this in Python is as shown, I would pass a float into a method, but in the method I'd check first that the float wasn't a string, in which case I'd act differently. I could obviously do this by setting percentage as a var or object, and checking it can be cast to different types, but I'm curious to know what's best practice here. Can I do something with overloads? Optional parameters? I'm still very new to this.
21 Replies
laurapigeon
laurapigeon17mo ago
(oh also tileState is an int for other reasons)
Angius
Angius17mo ago
Well, in C#, a float cannot be a string So you might need a second parameter, for example An enum perhaps Or, indeed, a method overload One that takes float percentage and other that takes float percentage, SomethingType somethingType With
enum SomethingType {
Checkerboard,
Lines,
Edges
}
enum SomethingType {
Checkerboard,
Lines,
Edges
}
Although it seems you wouldn't need that percentage parameter for the second method
laurapigeon
laurapigeon17mo ago
ideally, i would only need to pass a percentage in the case that the enum is Percentage, rather than Checkerboard etc since it isnt used in any other case
Angius
Angius17mo ago
A float cannot be an enum An enum cannot be a float A float cannot be a string... you get the idea
laurapigeon
laurapigeon17mo ago
yeye i guess i can just pass in the enum and then an optional parameter for percentage which i only use if passed in it just feels ungainly to have to pass in an enum even if percentage is given, or have to pass in a percentage even if the enum is Checkerboard etc
Relevant
Relevant17mo ago
You can use overloads instead
laurapigeon
laurapigeon17mo ago
wait theres a kind of method that acts differently on different types, could I have the method work differently on a string for the type of populate, and a float for the percentage? i forget what it was called
Angius
Angius17mo ago
public void DoStuff(float percentage)
{
tileState = rng.NextSingle() < percentage ? 1 : 0
}
public void DoStuff(SomethingType somethingType)
{
tileState = somethingType switch {
SomethingType.Checkerboard => ...,
SomethingType.Lines => ...,
...
}
}
public void DoStuff(float percentage)
{
tileState = rng.NextSingle() < percentage ? 1 : 0
}
public void DoStuff(SomethingType somethingType)
{
tileState = somethingType switch {
SomethingType.Checkerboard => ...,
SomethingType.Lines => ...,
...
}
}
Parametric polymorphism?
laurapigeon
laurapigeon17mo ago
scary lol
Anton
Anton17mo ago
overloads or like this
public enum Kind
{
Float,
Lines,
Edges,
Checkerboard,
}
public struct Rules
{
public Kind Kind;
public float PercentageValue;
}
public enum Kind
{
Float,
Lines,
Edges,
Checkerboard,
}
public struct Rules
{
public Kind Kind;
public float PercentageValue;
}
laurapigeon
laurapigeon17mo ago
i could go find it,,, it might have been a class whose instances worked differently based on kind, so might be completely irrelevant
Anton
Anton17mo ago
switch on Kind ignore the float if kind is not Float that's the best data oriented way to do it
laurapigeon
laurapigeon17mo ago
this seems good, since theres a lot in the method before i reach the point where the overload is important, is there a way to not have the whole method twice?
Angius
Angius17mo ago
An optional prameter ig
laurapigeon
laurapigeon17mo ago
valid
Angius
Angius17mo ago
public void DoStuff(SomethingType somethingType, float? percentage = null)
{
tileState = somethingType switch {
SomethingType.Checkerboard => ...,
SomethingType.Lines => ...,
...,
_ => rng.NextSingle() < percentage ? 1 : 0
}
}
public void DoStuff(SomethingType somethingType, float? percentage = null)
{
tileState = somethingType switch {
SomethingType.Checkerboard => ...,
SomethingType.Lines => ...,
...,
_ => rng.NextSingle() < percentage ? 1 : 0
}
}
laurapigeon
laurapigeon17mo ago
are there overloads that can call each other? i guess,,, thats just calling the method from one version of itself
Angius
Angius17mo ago
Yes, you can call one method from another method
laurapigeon
laurapigeon17mo ago
lastly, if im calling the Board's Populate method from outside it, where best do i define the enum to be used inside and outside it? just in the namespace i guess? i might just not understand scope right here
Angius
Angius17mo ago
Wherever, could be a whole separate file
laurapigeon
laurapigeon17mo ago
ok the enum definitely makes this feel a lot neater, even with the sorta stray null float