❔ Function with generic argument as a setter

Heyo, I'm currently trying to do something like this:
No description
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?
reflectronic
reflectronic14mo ago
no, default never calls the constructor
𝐕𝐢𝐠𝐢𝐥
Great! 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
reflectronic
reflectronic14mo ago
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
reflectronic
reflectronic14mo ago
you would have to pass ChangeSceneInfo(ref sceneInfo.Total, value), though, which means that you would need to directly access the value am i misunderstanding
𝐕𝐢𝐠𝐢𝐥
basically
reflectronic
reflectronic14mo ago
when is structInit false and what do you do to make it true i think it would be better to do something like
public ref SceneInfo SceneInfo
{
get
{
// Ensure sceneInfo is properly initialized or whatever
return ref sceneInfo;
}
}
public ref SceneInfo SceneInfo
{
get
{
// Ensure sceneInfo is properly initialized or whatever
return ref sceneInfo;
}
}
𝐕𝐢𝐠𝐢𝐥
ooh that could work what about the code path if structInit hasn't been initialized?
reflectronic
reflectronic14mo ago
i mean, uh, i dunno, maybe just throw?
𝐕𝐢𝐠𝐢𝐥
the good ol
reflectronic
reflectronic14mo ago
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 false
𝐕𝐢𝐠𝐢𝐥
I 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
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server