C
C#17mo ago
Loup&Snoop

❔ static class

Hey. I'm trying to define a static class. My static class's methods are almost entirely all functions of variables not in my class. However, I do want to initialize a reference to one thing, to an instance of a singleton, so I don't need to call GetInstance() (and all the baggage associated with calling it) every time I run a function from my static class. Q: Is there a way to store a reference to an instance as a variable in a static class at runtime?
10 Replies
Sossenbinder
Sossenbinder17mo ago
A static class can have static field as well
Angius
Angius17mo ago
You can use a static constructor, ig?
Sossenbinder
Sossenbinder17mo ago
Just like a regular class can have regular fields
Angius
Angius17mo ago
But if you rely on static classes this much, something might be fucky-wucky with your architecture as a whole
Loup&Snoop
Loup&SnoopOP17mo ago
maybe I could show you my code to see if this makes sense
Loup&Snoop
Loup&SnoopOP17mo ago
Loup&Snoop
Loup&SnoopOP17mo ago
assume that little error is a real singleton. Would the code that I just wrote actually be like a real constructor for my static class? which lets me just make a bunch of functions that use that static variable without concern?
Angius
Angius17mo ago
A static constructor runs once, IIRC when the static class is first accessed And, yeah, it should save that instance in your tileDirectory
Loup&Snoop
Loup&SnoopOP17mo ago
sweet. I'll give it a whirl. ty. it worked great. ty so much
Accord
Accord17mo 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