❔ Function with generic argument as a setter
Heyo, I'm currently trying to do something like this:
15 Replies
SceneInfo
is a struct that contains multiple member values, I want to be able to set those values with a setter function that uses a generic, the member var and value
used to re-define it are of the same type (say, double), I'm not exactly sure how I would go about this otherwise
sceneInfo is a variable, there should only be one sceneInfo
var in this class ever initialized, but for the sake of double-checking and safety's sake, I do this
is this acceptable anyhow?
structInit
is set to true once the constructor is called
also, will a var with it's initial value defined as default
call the constructor?no,
default
never calls the constructorGreat! As for the coding practice above, would this be considered "horror" or is it perfectly fine
I don't want people directly accessing the variable to make sure it's 1. been defined properly, 2. again, cleanliness/safety
i am not really sure how you would use this to set the member values on the
SceneInfo
well, let's say I have a member value that is called
Total
of the type double, I can put the argument as <double>
, so now I can only assign double
as the T value
for sceneInfo.Total
you would have to pass
ChangeSceneInfo(ref sceneInfo.Total, value)
, though, which means that you would need to directly access the value
am i misunderstandingbasically
when is
structInit
false
and what do you do to make it true
i think it would be better to do something like ooh that could work
what about the code path if structInit hasn't been initialized?
i mean, uh, i dunno, maybe just throw?
the good ol
or maybe it's better to make sure it has to be initialized? like, take SceneInfo in the constructor of whatever this class is, and throw there if
structInit
is falseI just made a public function that retrieves the (now private) sceneInfo's ref and if it isn't initialized, throws an error, if it is, all ensues as normal.
I'd love to use a super complicated template function like I wanted originally... but you can't pass properties as ref (boo womp)
I'm gonna move sceneInfo to have a proper default constructor instead of doing this terrible-ness very soon, I initially started off with the premise of "sceneInfo" should always be manually initialized because the player may load from a save, but I feel like this is a redundant thing to do in the context of C#, where I'm C++ brained (recently making a switch to expand into other languages and specialities) and I'm still very very used to doing stuff like this when it's over complicating code
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.