OO concept question

Associate
Joined
18 Oct 2002
Posts
972
Location
Derby
I would say I am a pretty basic/intermediate programmer (compared to all you lot) and I just have a query about using OO in general. While I was at Uni I got the impression from classes that the idea of OO was you have a class file, with a decent name that basically takes a variable or an object, does it's magic and returns the value. The main benefit being if someone says "how do I code a method that writes a text file?" somebody can say "just create an instance of the class and its done". This got me thinking with a simple project I am doing (it works but I want to check if I am doing it right).

I am creating a web application in C# using VS 2005. I have a number of .pdf documents that have got to be renamed and moved into relevant folders so I thought to make life easier I would allow users to enter the details in text boxes to save then opening and changing folders. Here is a method from my class:

Code:
//Date: 03/11/2006
    //Summary: This method takes a session value and creates a directory ready for .pdf files
    //to be inserted.
    public void MakeDirectory(string FolderName)
    {
        Directory.CreateDirectory("T:\\EUStore\\" +FolderName.Trim());

    }

The code below is in my .aspx.cs file and accesses the method

Code:
protected void CreateFolder()
    {
            string SessionFileNumber = "" + Session["SessionFileNumber"];
            EUStore TheEUStore = new EUStore();
            TheEUStore.MakeDirectory(SessionFileNumber);
       
    }

Basically what I am asking is shouldn't the variables be declared and assigned on
the .aspx.cs page? That way if I ever needed to create a directory I would not need to change any code in the class, just the code in my new application/page?
 
Hi,

My view of OO is that a class should define an entity that needs to be modelled within the application (identified using a classification process). It should contain the attributes that go to make up that entity (and no other attributes of other classes) and methods that allow actions to be performed on the entity. So, for example a class representing a user of an application might have the attributes:

personid
name
password

and might contain the methods

login
logout

The benefit of this is that the behaviour and contents of the entity are both held together. You can also be assured that the behaviour of the methods will be consistent no matter where it is called from. For example, in the above every call to the "login" method will behave the same rather than having numerous pieces of login code scattered in the application.

The other important thing is that a class should be able to interact with other classes by using methods on them. For example, the "user" class might have a link to a "preferences" class and will be able to invoke methods on this class.

In your code you might want to consider holding the directory created in the TheEUStore class as an attribute on the class so the class can tell which directory it has created after the MakeDirectory method has exited and it would then be available for other methods on the class (or methods on classes that can see the TheEUStore object) to use.

Hope that helps.
Jim
 
Vedder said:

What you would do is store the information relevant to the class in variables and use access modifiers to set/get them.

e.g.
Code:
public class EuStore
{
    string sdir;

    public void MakeDirectory()
    {
        Directory.CreateDirectory(this.sdir);
    }

    public string Directory
    {
         get
         {
              return this.sdir;
         }
         set
         {
              this.sdir = value;
         }
    }
}

Also would be ideal to have a constructor that can set them. Have a default constructor but give the option to set aswell

Code:
      public EuStore()
      {
           //default constructor
           this.sdir = "T:\\EUStore\\" +FolderName.Trim();
      }

      public EuStore(string sDirectory)
      {
           this.sdir = sDirectory;
      }

In your aspx.cs file you would call it like

Code:
protected void CreateFolder()
    {
            string SessionFileNumber = (string)Session["SessionFileNumber"];
            EUStore TheEUStore = new EUStore(SessionFileNumber);
            TheEUStore.MakeDirectory();
       
    }

Finally this class would most likely be better to implement as a static class as i don't think your requirement would need to instatiate multiple objects of this class. Google it to learn about it. If you do go the static route you will have to change it abit though.
 
Back
Top Bottom