So this is a homework question, but i've been doing endless reading and am really clouded over!!

I have to use an Object Oriented approach to gain the higher marks for this but I can't work out whether i'm going about this all wrong.

Is it possible to have just one instance of a class and a bunch of instance methods and it still be object oriented? There is a point in this problem where 2 objects are instantiated but that's at the very end and not essential. What i'm concerned with is this 'thing' which over time gets into certain states and those states have to be put right essentially. I'm atm assuming instance methods and a clock/timer class will work fine to do that but as far as I can tell this just leaves one object and that's it. :s

Recommended Answers

All 6 Replies

That is object oriented. Non-object oriented would be using static methods -- which does not rely on an instance to execute. Please explain your homework in more detail.

Kudos on asking your question by the way. You were up front with saying it was homework and you didn't ask for the solution, you asked for information on how you can go about solving it. That is how you get a homework question answered ;)

Thanks sknake for replying and thanks, I thought I might as well be honest XD.

Well, basically there are 3 states this 'thing' gets into, and 1 way of fixing each state. What I was toying with was the idea that each state was an object but classes and objects confuse me a little with their real world application. I was told once that objects can be anything, tangible or abstract so I thought perhaps each state could be an object.

But then I got confused with what their class would be! To me that logic says that I would then have to create three more classes because each state is totally different. Then presumably (i'm digging AND going round in circles at this point!) have the fixing instance methods within those 3 individual objects.

My other logic was, well this is just one object with a bunch of instance methods that changed the three 'state' instance variables! This way made far more sense and was far more simple but I was left thinking, hang on, this is just one object!

Please feel free to give more details than "this thing" You're not going to get hounded by explaining what you are doing, only if you say gimme the answer!

Usually a state is dependent on another property or entity. For example a connection usually means an open TCP/IP network stream, or if you had a car the "state of the car" would evaluate to it "is it operable" which is dependant on the quantity of gas or oil (among other things). That being said the design really revolves around what you are doing. You mentioned a timer so if your class was a sort of "stopwatch" that did lap-times the states could be "Ready to go", "in the middle of timing some fool running in the freezing cold at 7am", and "im finished timing but you need to clear my results before I can run again".

Well I see you marked it solved but hopefully this will help:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace daniweb
{
  public enum SomeConnectionState { Open, Closed, Error }

  public class SomeConnection
  {
    //Public properties
    public SomeConnectionState State { get; set; }

    //ctor
    public SomeConnection()
    {
      this.State = SomeConnectionState.Closed; //Default value
    }

    //Public methods
    public void OpenConnection()
    {
      this.State = SomeConnectionState.Open;
    }

    public void CloseConnection()
    {
      this.State = SomeConnectionState.Closed;
    }

    public void DoSomething()
    {
    }

  }
}

Then interfacing with that class that has multiple states for a single instance:

private void button1_Click(object sender, EventArgs e)
    {
      SomeConnection conn = new SomeConnection();
      //Lets open a connection

      switch (conn.State)
      {
        case SomeConnectionState.Open: //Dont need to anything, its already opened
          break;
        case SomeConnectionState.Closed: //Need to open
          conn.OpenConnection();
          break;
        case SomeConnectionState.Error: //We need to close and re-open in the case of an error
          conn.CloseConnection();
          conn.OpenConnection();
          break;
        default: //Someone left an enum out of the switch statement!
          throw new InvalidEnumArgumentException("conn.State", (int)conn.State, typeof(SomeConnectionState));
      }

      //Ok we have a connection open, lets do our important work
      conn.DoSomething();

      //We're all finished up. Lets shut it down!
      switch (conn.State)
      {
        case SomeConnectionState.Open: //Need to shut down open connections
          conn.CloseConnection();
          break;
        case SomeConnectionState.Closed: //Already closed, dont do anything
          break;
        case SomeConnectionState.Error: //Just shut the conn down
          conn.CloseConnection();
          break;
        default: //Someone left an enum out of the switch statement!
          throw new InvalidEnumArgumentException("conn.State", (int)conn.State, typeof(SomeConnectionState));
      }
    }

All of the states/scenarios are entirely made up so they may not make sense and they certainly dont do anything.

Thanks, that looks interesting and I think I understand your point with your code :). My problem isn't a stopwatch it's a toy, basically when it runs out of petrol i've got to re-fill it, when it crashes I've got to fix it, when it's freezing i've got to de-ice it lol, that kind of thing.

I marked it solved because I've got to go do a load more reading now! XD thanks again. :)

And thanks once more, your "Usually a state is dependent on another property or entity" has set me in a positive looking direction!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.