tags:

views:

253

answers:

4

I have read the docs and everything but I'm very confused. I never needed to create a class before and now I do.

I want to have something like:

TextDocument.Save("filepath", "contents of file to save");

and stuff like:

Application.Create("filepath", "text/code to save");

and:

Stylesheet.Save("filepath", "contents");

and have these in a class and create methods for them but I'm very confused as to how to go about doing it can somebody please help me with this?

thank you, jase

+10  A: 

Impossible to say without more code, but those look like static methods, i.e. create a new class (cs) file, and add something like:

using System.IO;
public class TextDocument {
   public static void Save(string path, string contents) {
       File.WriteAllText(path, contents);
   }
}

If TextDocument is actually an instance, take away the word static.

Note that to be callable, you also need to know about namespaces. The above is in the default namespace, but that is a bit vulgar. It should really be more like:

using System.IO;
namespace FooCorp.MagicApp {
    public class TextDocument {
        public static void Save(string path, string contents) {
            File.WriteAllText(path, contents);
        }
    }
}

Then only code with using FooCorp.MagicApp will see your class (this is a good thing for retaining sanity; there are lots of classes in the .NET framework)

Marc Gravell
thank you very much marc, that makes complete sense, i'm trying it out now. i wish i could buy knowledge lol
baeltazor
You can - they're called books ;-)
FinnNk
Why buy when there are 100s of tutorials out there for free!
James
omg i like SOOO didnt know that! lol ;) +1 for sense of humor
baeltazor
yay free tutorials here i come!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :D
baeltazor
+1 Retaining sanity is always a good thing. :D
Groo
@Marc +1 for deciphering the question@baeltazor if it's the right answer accept it?
ParmesanCodice
+4  A: 
public class Foo
{
   public static Foo Create()
   {
     //do stuff
   }

   public void Save()
   {
     //do stuff
   }
}

You call them differently, because of the static keyword

Foo f = Foo.Create();
f.Save();

and I recommend this book - http://oreilly.com/catalog/9780596003760

Note that create is really not the best static method in the world because of things called constructors, but for the sake of examples...

Russell Steen
+1  A: 

If you're confused to how classes and objects work, try to read a book (as Russell Steen points out).

The terminology in tutorials and programming books may confuse beginners, if this is the case for you then try to fetch a programmer who does know and watch him code a class while asking as many questions you can until you get it. Programmers are generally a nice bunch of people.

If you really want to learn more about OOP (Object Oriented Programming), watch someone do some code kata or TDD.

Good luck writing your classes!

Spoike
thank you for the advice Spoike :D i wish i knew a programmer i could watch
baeltazor
yeah, those pesky programmers are usually hiding somewhere in some basement or something. but once you do find someone, they should be fairly helpful.
Spoike
A: 

Yay! I created my first Class! :D hehehehe...

Here's the complete code. I don't know if it's the "best" way, or the most "efficient" way of doing this. But it works. I hope it can help others too:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MyApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public String FilePath,
            FileContents = null;

        public class TextDocument
        {
            public void Save(string FilePath, String FileContents)
            {
                File.WriteAllText(FilePath, FileContents);
            }
        }

        private void toolStripButton337_Click(object sender, EventArgs e)
        {
            FilePath = "this.txt";
            FileContents = "testing";

            TextDocument Document = new TextDocument();
            Document.Save(FilePath, FileContents);
        }
    }
}

Thank you Russell Steen and Marc Gravell for your help! :D

baeltazor
You don't need to nest the TextDocument class inside the Form1 class. It can be also be declared outside the scope of Form1 or in another file.
Spoike
hmm wow thanks for that spoike! when you say "in another file", would i just reference that file in Form1 like this: using MyClassFile; where the other usings are?
baeltazor
Almost, you won't need to do if they are in the same namespace (from the code it is "namespace MyApp"). The "using" statement is used when a class is declared in another namespace.
Spoike