Accessing a method in another file
-
Hi, I usually put all my codes in one .cs file. But im now trying a bit organized programming. I made a method in another file, an xml writer file. And i want to send commands to it from my form.cs file. So how can i access a method which is in for example alfa.cs file from beta.cs file? Both files are in the same project. Thanks in advance!
-
Hi, I usually put all my codes in one .cs file. But im now trying a bit organized programming. I made a method in another file, an xml writer file. And i want to send commands to it from my form.cs file. So how can i access a method which is in for example alfa.cs file from beta.cs file? Both files are in the same project. Thanks in advance!
Hi, The only thing that you need to do for using a class/method from another file from the same project is to use the NameSpace inside which is the method. Nothing else. Hope it helps
Do your best to be the best
-
Hi, I usually put all my codes in one .cs file. But im now trying a bit organized programming. I made a method in another file, an xml writer file. And i want to send commands to it from my form.cs file. So how can i access a method which is in for example alfa.cs file from beta.cs file? Both files are in the same project. Thanks in advance!
If a method is static, then you can call it from anywhere in your project. If it's not static, you need an instance of the class to call the methods on. Class C1 { private int n = 0; private static int s = 0; public int Int { get { return n; } } public int IntS { get { return s; } } public void Add(int i) { n += i; } public static AddS(int i) { s+=i; } } Class C2 { public DoStuff() { C1.AddS(10); Console.WriteLine(C1.IntS.ToString()); // 10 C1 c1 = new C1(); c1.Add(5); Console.WriteLine(c1.Int.ToString()); // 5 C1 c2 = new C1(); c2.Add(52); // c2.Int now = 52, c1.Int still = 5, C1.IntS still = 10 C1.AddS(10); Console.WriteLine(C1.IntS.ToString()); // 20 c1.Add(12); Console.WriteLine(c1.Int.ToString()); // 17 Console.WriteLine(c2.Int.ToString()); // 52 } } The most common mistake I've seen is for people to have one instance of a class, to be in another class where they don't currently have access to that instance, so they create a new instance ( often of a form ) and try to change variables on that. this won't ever change variables on any instance bar the new one, unless they are static.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hi, The only thing that you need to do for using a class/method from another file from the same project is to use the NameSpace inside which is the method. Nothing else. Hope it helps
Do your best to be the best
-
If a method is static, then you can call it from anywhere in your project. If it's not static, you need an instance of the class to call the methods on. Class C1 { private int n = 0; private static int s = 0; public int Int { get { return n; } } public int IntS { get { return s; } } public void Add(int i) { n += i; } public static AddS(int i) { s+=i; } } Class C2 { public DoStuff() { C1.AddS(10); Console.WriteLine(C1.IntS.ToString()); // 10 C1 c1 = new C1(); c1.Add(5); Console.WriteLine(c1.Int.ToString()); // 5 C1 c2 = new C1(); c2.Add(52); // c2.Int now = 52, c1.Int still = 5, C1.IntS still = 10 C1.AddS(10); Console.WriteLine(C1.IntS.ToString()); // 20 c1.Add(12); Console.WriteLine(c1.Int.ToString()); // 17 Console.WriteLine(c2.Int.ToString()); // 52 } } The most common mistake I've seen is for people to have one instance of a class, to be in another class where they don't currently have access to that instance, so they create a new instance ( often of a form ) and try to change variables on that. this won't ever change variables on any instance bar the new one, unless they are static.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hi, Nothing in my project is static. The method is a void one, its not returning anything (as it should). This: C1 c2 = new C1(); Helped! Thank you very much!
Yustme wrote:
Nothing in my project is static.
OK, then you need to keep a specific instance of the class you want to call, so you can work with it. If the method you're calling doesn't rely on any member variables, then it should be static.
Yustme wrote:
The method is a void one, its not returning anything (as it should).
OK, that makes no sense. If it's got void, then it doesn't return anything. I think it's time you posted some code.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Yustme wrote:
Nothing in my project is static.
OK, then you need to keep a specific instance of the class you want to call, so you can work with it. If the method you're calling doesn't rely on any member variables, then it should be static.
Yustme wrote:
The method is a void one, its not returning anything (as it should).
OK, that makes no sense. If it's got void, then it doesn't return anything. I think it's time you posted some code.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Hi, I know it doenst return anything when its void, but it was more like extra info to your example where you showed me 2 methods that returned something. This is my method: public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); if (this.writer != null) this.writer.Close(); }
-
Hi, I know it doenst return anything when its void, but it was more like extra info to your example where you showed me 2 methods that returned something. This is my method: public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); if (this.writer != null) this.writer.Close(); }
Dude, that's some strange code. You check if writer is null, after using it twice, and if it's not, you call close on it. What does close do ? Does it stop WriteElementString from working later ? If this is the object I think it is, WriteEndElement is paired with WriteStartElement, so you're using it wrong.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Dude, that's some strange code. You check if writer is null, after using it twice, and if it's not, you call close on it. What does close do ? Does it stop WriteElementString from working later ? If this is the object I think it is, WriteEndElement is paired with WriteStartElement, so you're using it wrong.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Hi, There is actually more code. Complete class: class XMLWriter { XmlWriterSettings settings = new XmlWriterSettings(); DateTime dateTimeNow = DateTime.Now; XmlWriter writer = null; public XMLWriter() { this.settings.Indent = true; this.settings.IndentChars = "\t"; this.settings.OmitXmlDeclaration = true; this.settings.NewLineOnAttributes = true; this.settings.Encoding = Encoding.Default; this.settings.ConformanceLevel = ConformanceLevel.Fragment; this.writer = XmlWriter.Create("Testing.xml", settings); this.writer.WriteStartElement("Information"); this.writer.WriteElementString("DateOfBuilding", dateTimeNow.ToShortDateString()); this.writer.WriteElementString("TimeOfBuilding", dateTimeNow.ToLongTimeString()); } public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); this.writer.Flush(); if (this.writer != null) this.writer.Close(); } } writer.Close(); is to stop writing and used for disposal.
-
Hi, There is actually more code. Complete class: class XMLWriter { XmlWriterSettings settings = new XmlWriterSettings(); DateTime dateTimeNow = DateTime.Now; XmlWriter writer = null; public XMLWriter() { this.settings.Indent = true; this.settings.IndentChars = "\t"; this.settings.OmitXmlDeclaration = true; this.settings.NewLineOnAttributes = true; this.settings.Encoding = Encoding.Default; this.settings.ConformanceLevel = ConformanceLevel.Fragment; this.writer = XmlWriter.Create("Testing.xml", settings); this.writer.WriteStartElement("Information"); this.writer.WriteElementString("DateOfBuilding", dateTimeNow.ToShortDateString()); this.writer.WriteElementString("TimeOfBuilding", dateTimeNow.ToLongTimeString()); } public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); this.writer.Flush(); if (this.writer != null) this.writer.Close(); } } writer.Close(); is to stop writing and used for disposal.
If both your classes are in the same namespace, simply create an instance of your class and make a call to the method ... XMLWriter myObject = new XMLWriter(); myObject.WritingToXMLDoc("elementString", "writeToXMLDoc"); ... I'm not sure if im on the right path here but i hope it helps
-
Hi, There is actually more code. Complete class: class XMLWriter { XmlWriterSettings settings = new XmlWriterSettings(); DateTime dateTimeNow = DateTime.Now; XmlWriter writer = null; public XMLWriter() { this.settings.Indent = true; this.settings.IndentChars = "\t"; this.settings.OmitXmlDeclaration = true; this.settings.NewLineOnAttributes = true; this.settings.Encoding = Encoding.Default; this.settings.ConformanceLevel = ConformanceLevel.Fragment; this.writer = XmlWriter.Create("Testing.xml", settings); this.writer.WriteStartElement("Information"); this.writer.WriteElementString("DateOfBuilding", dateTimeNow.ToShortDateString()); this.writer.WriteElementString("TimeOfBuilding", dateTimeNow.ToLongTimeString()); } public void WritingToXMLDoc(string elementString, string writeToXMLDoc) { this.writer.WriteElementString(elementString, writeToXMLDoc); this.writer.WriteEndElement(); this.writer.Flush(); if (this.writer != null) this.writer.Close(); } } writer.Close(); is to stop writing and used for disposal.
Yustme wrote:
DateTime dateTimeNow = DateTime.Now;
Why ?
Yustme wrote:
writer.Close(); is to stop writing and used for disposal.
But that reads as if the method can only be called once - why ? What are you expecting and what are you getting ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Yustme wrote:
DateTime dateTimeNow = DateTime.Now;
Why ?
Yustme wrote:
writer.Close(); is to stop writing and used for disposal.
But that reads as if the method can only be called once - why ? What are you expecting and what are you getting ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
If both your classes are in the same namespace, simply create an instance of your class and make a call to the method ... XMLWriter myObject = new XMLWriter(); myObject.WritingToXMLDoc("elementString", "writeToXMLDoc"); ... I'm not sure if im on the right path here but i hope it helps
-
Hi, To write the date and time in the xml file too. What do you suggets instead of using Close()? Havent tested it all yet, but it writes the start and end element and the time and date.
Yustme wrote:
To write the date and time in the xml file too.
But it's more than redundant, it will continue to point to the moment the file was created, which is not what Now means. And, if you only use it in one method, why is it even a member, assuming you had to create it at all ? Just use DateTime.Now.
Yustme wrote:
What do you suggets instead of using Close()?
If you need to close the file, expose a method to do this, otherwise calling your other method once will close the file, but not make this clear.
Yustme wrote:
Havent tested it all yet
OK, then at what point did you have a problem with what it's doing ? Or did I misunderstand you ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Yustme wrote:
To write the date and time in the xml file too.
But it's more than redundant, it will continue to point to the moment the file was created, which is not what Now means. And, if you only use it in one method, why is it even a member, assuming you had to create it at all ? Just use DateTime.Now.
Yustme wrote:
What do you suggets instead of using Close()?
If you need to close the file, expose a method to do this, otherwise calling your other method once will close the file, but not make this clear.
Yustme wrote:
Havent tested it all yet
OK, then at what point did you have a problem with what it's doing ? Or did I misunderstand you ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog