0

I have a static abstract class called State, it defines the abstract methods start() and update(), and then I have multiple classes which implement State.

Now I want to create a class, let's call it StateMachine, that has a property called currentState. I want to be able to set this property pointing to a static class implementing state so I can run things like currentState.update().

Now comes my question:

What should be the type of the property? I'd say State but I feel like there's something more to it. Also, how would I go into setting the property? is

currentState = ClassThatImplementsState;

a valid thing to do?

EDIT: Ok, so I just learned that you cannot inherit from a static class in C#. I'd like to know a way of doing something similar to this that would allow me both the polymorfism and not having to instantiate the state objects. Is there such a thing?

Setzer22
  • 1,589
  • 2
  • 13
  • 29
  • 2
    You can't have a *static abstract class* in c# – Sriram Sakthivel Aug 18 '14 at 13:25
  • property should be type `State` – Grundy Aug 18 '14 at 13:26
  • you are trying to implement a state-machine? Fine you can do this with mutual-recursive functions (one for each state) - but typically all in the same static class .... – Random Dev Aug 18 '14 at 13:27
  • Yeah. I just realised static classes cannot be inherited in C#. Any more options to implement what I'm doing? I could go with non-static classes having only static methods? – Setzer22 Aug 18 '14 at 13:27
  • nope - if you need only the functions you can get yourself some delegates or use Action<>/Func<> – Random Dev Aug 18 '14 at 13:28
  • you might want to get a bit background in what static/abstract/inheritance/OOP really is all about - sorry but I don't think that this is the format for a basic tutorial like this – Random Dev Aug 18 '14 at 13:31
  • Why does `State` and/or its methods need to be `static`? – D Stanley Aug 18 '14 at 13:31
  • @Setzer22 Why are you against creating instances? What's wrong with them? Static members doesn't participate in inheritance. Makes no sense also. This seems to be a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Instead explain what you're trying to do. – Sriram Sakthivel Aug 18 '14 at 13:32
  • @D Stanley There's not really a need for it to be static, I just don't want to keep track of a bunch of State instances if I'm only going to use them as as singleton and like a purely "functional" object, so given the same input, the output is guaranteed. – Setzer22 Aug 18 '14 at 13:33
  • 1
    Don't use static classes, and don't create singletones. They are stupid and annoying. At least until you understand that a static class is just like a namespace to hold **functions** and not maintain **state**, and that a singletone is a good idea only if having multiple instances of something would **break** it. – AK_ Aug 18 '14 at 13:43
  • @AK_: Agreed - But to make matters worse, static classes CAN maintain state ... but to use them that way means _abusing_ them. – chris Aug 18 '14 at 14:31

1 Answers1

2

If State is a static class, you can't have multiple classes that "implement", i.e. derive from it. When, on the other hand, it is an abstract class, you can derive from it. But it can't be both.

EDIT: I think what you actually want is a Singleton rather than an abstract or static class. See http://msdn.microsoft.com/en-us/library/ff650316.aspx for hints how to implenent the Singleton pattern in C#.

EDIT 2: If your application is multithreaded, be sure to use a thread safe variant of the Singleton pattern.

chris
  • 2,541
  • 1
  • 23
  • 40
  • 1
    Also, This should be a comment! – Sriram Sakthivel Aug 18 '14 at 13:27
  • Yes, I'm looking for a singleton. As static classes are a possible implementation for singletons but they don't really allow me to do what I want, I guess I'll try another implementation instead. Sorry for all the confusion. – Setzer22 Aug 18 '14 at 13:36