C
C#2y 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
Sossenbinder2y ago
A static class can have static field as well
Angius
Angius2y ago
You can use a static constructor, ig?
Sossenbinder
Sossenbinder2y ago
Just like a regular class can have regular fields
Angius
Angius2y ago
But if you rely on static classes this much, something might be fucky-wucky with your architecture as a whole
Loup&Snoop
Loup&SnoopOP2y ago
maybe I could show you my code to see if this makes sense
Loup&Snoop
Loup&SnoopOP2y ago
Loup&Snoop
Loup&SnoopOP2y 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
Angius2y 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&SnoopOP2y ago
sweet. I'll give it a whirl. ty. it worked great. ty so much
Accord
Accord2y 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.

Did you find this page helpful?