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 a开发者_开发问答bout doing it can somebody please help me with this?
thank you, jase
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)
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...
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!
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
精彩评论