|
Hi,
They all have the same namespace.
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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,
Thats exactly what i have
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
Hi,
Not having any problems atm.
|
|
|
|
|
Hi
i have to access data from a file located on a remote pc on the network. Can anybody suggest me which method to use, either asynchronous sockets or remotig. and what will be the pros and cons of using remoting or asynchronous sockets.
sAqIb
|
|
|
|
|
Hi,
My reply regards sockets. They could help you but to use sockets you have to create a server and a client application. This could be a problem sometimes. I've worked a lot with sockets and they suited well in my applications. I don't know much about remoting but sockets require a server which listens for a connection and that could pe a con sometimes
Do your best to be the best
|
|
|
|
|
Can you not use network file-sharing to achieve this?
Regards,
Rob Philpott.
|
|
|
|
|
Hi,
I have DataTable that is binded to a DataGridView. How to I retrieve the values in the columns/rows dynamically? I want to retrieve the string representation of each cell. Clearification: When user have finished entering data in the datagridview he/she hits the save button every data in each cell is saved into a objects fields.
Thanx in advance
Regards
/Christoffer
|
|
|
|
|
Hi all,
I need to covert a video file to a .flv file,
How do i do that?
Thanks & Regards,
Pramod
"Everyone is a genius at least once a year"
|
|
|
|
|
import the video in Macromedia Flash. flv is a Flash Video.
regards,
g00fy
|
|
|
|
|
sorry i didn't get u,
Here is what i want to achieve.
I have created a web Page, now i want to play a video file on this page.
For this i want to convert a video file to a .flv file.
how do i convert a video file to a .flv file?
Thanks & Regards,
Pramod
"Everyone is a genius at least once a year"
|
|
|
|
|
To convert the video, import it in flash and from within. Moreover there are external FLV converters also available.
Excelsior
Arjun Bahree
"By The Might of Mjolnir"
I Came! I Coded! I Conquered!
|
|
|
|
|
I think u didn't get wht actually i am trying to do.
I have a web application, on my ftp server there are video files which i
need to play on my page.
Now when i get the files from the ftp server they are video files,
And i want them to convert to .flv files so that i can play them in the browser.
Hope i am able to explain it now.
Thanks & Regards,
Pramod
"Everyone is a genius at least once a year"
|
|
|
|
|
Hi,
i have a desktop application in which i have a datagrid.
each row of the grid have few dropdowns.
now after the user make the changes in the grid, i want to just bulk update the grid in the database, i dont want to include the rows in the update which have not been updated.
Thanks.
Deepak Surana
|
|
|
|
|
Well for this, you can use the datagrid row properties to find out which are the changed/modified rows and likewise only update those rows in the DB.
Does that answer what you looking for?
Excelsior
Arjun Bahree
"By The Might of Mjolnir"
I Came! I Coded! I Conquered!
|
|
|
|
|
Hi,
I think it can solve my problem,
can you please provide me a code snippet for the same.
|
|
|
|