What's the actual error you are getting? I thought you could only set static variables inside a static method, but I might be wrong...
I try to convert a VB.net class into a C# class but i have an error !
VB.NET class:
C# class converted:Code:Option Strict Off Option Explicit On Imports System.IO Imports System.Xml Imports System.Xml.Serialization Imports System.Text Public Class cMySettings Public FTPUsername As String Public FTPPassword As String Public FTPHostname As String Public FTPHostpath As String Public FTPDirectorypath As String Private Shared XMLFilename As String Public Sub New() SetToDefault(Me) End Sub Public Sub New(FileName As String) XMLFilename = FileName If Path.GetExtension(XMLFilename).ToLower() <> "xml" Then XMLFilename = XMLFilename + ".xml" If File.Exists(XMLFilename) = False Then SerializeToXML(New cMySettings()) End Sub Public Sub New(ByRef Settings As cMySettings) Me.FTPUsername = Settings.FTPUsername Me.FTPPassword = Settings.FTPPassword Me.FTPHostname = Settings.FTPHostname Me.FTPHostpath = Settings.FTPHostpath Me.FTPDirectorypath = Settings.FTPDirectorypath End Sub Public Shared Sub SerializeToXML(ByRef Settings As cMySettings) Dim xmlSerializer As New XmlSerializer(GetType(cMySettings)) Using xmlTextWriter As New XmlTextWriter(XMLFilename, Encoding.UTF8) xmlTextWriter.Formatting = Formatting.Indented xmlSerializer.Serialize(xmlTextWriter, Settings) xmlTextWriter.Close() End Using End Sub Public Shared Sub DeseralizeFromXML(ByRef Settings As cMySettings) Dim fs As FileStream = Nothing ' do i have settings? If File.Exists(XMLFilename) = True Then Try fs = New FileStream(XMLFilename, FileMode.Open, FileAccess.Read) Dim xmlSerializer As New XmlSerializer(GetType(cMySettings)) Settings = xmlSerializer.Deserialize(fs) Catch 'load error of some sort, or OBJECT deserialize error 'do we tell anyone? Exit Sub Finally If Not fs Is Nothing Then fs.Close() fs = Nothing End Try End If End Sub Public Shared Sub Copy(ByRef SourceSettings As cMySettings, ByRef DestSettings As cMySettings) DestSettings.FTPUsername = SourceSettings.FTPUsername DestSettings.FTPPassword = SourceSettings.FTPPassword DestSettings.FTPHostname = SourceSettings.FTPHostname DestSettings.FTPHostpath = SourceSettings.FTPHostpath DestSettings.FTPDirectorypath = SourceSettings.FTPDirectorypath End Sub Public Shared Sub SetToDefault(ByRef Settings) Settings.FTPUsername = "FTPUsername" Settings.FTPPassword = "FTPPassword" Settings.FTPHostname = "FTPHostname" Settings.FTPHostpath = "Disque dur/Enregistrements/" Settings.FTPDirectorypath = "C:\temp\FTPCopy\" End Sub Public Shared Function Compare(ByRef Settings1 As cMySettings, ByRef Setting2 As cMySettings) As Boolean If Settings1.FTPUsername <> Setting2.FTPUsername Then Compare = False : Exit Function If Settings1.FTPPassword <> Setting2.FTPPassword Then Compare = False : Exit Function If Settings1.FTPHostname <> Setting2.FTPHostname Then Compare = False : Exit Function If Settings1.FTPHostpath <> Setting2.FTPHostpath Then Compare = False : Exit Function If Settings1.FTPDirectorypath <> Setting2.FTPDirectorypath Then Compare = False : Exit Function Compare = True End Function End Class
The SetToDefault function return me an error !Code:using System.IO; using System.Xml; using System.Xml.Serialization; using System.Text; public class cMySettings { public string FTPUsername; public string FTPPassword; public string FTPHostname; public string FTPHostpath; public string FTPDirectorypath; private static string XMLFilename; public cMySettings() { SetToDefault(ref this); } public cMySettings(string FileName) { XMLFilename = FileName; if (Path.GetExtension(XMLFilename).ToLower() != "xml") XMLFilename = XMLFilename + ".xml"; if (File.Exists(XMLFilename) == false) SerializeToXML(ref new cMySettings()); } public cMySettings(ref cMySettings Settings) { this.FTPUsername = Settings.FTPUsername; this.FTPPassword = Settings.FTPPassword; this.FTPHostname = Settings.FTPHostname; this.FTPHostpath = Settings.FTPHostpath; this.FTPDirectorypath = Settings.FTPDirectorypath; } public static void SerializeToXML(ref cMySettings Settings) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings)); using (XmlTextWriter xmlTextWriter = new XmlTextWriter(XMLFilename, Encoding.UTF8)) { xmlTextWriter.Formatting = Formatting.Indented; xmlSerializer.Serialize(xmlTextWriter, Settings); xmlTextWriter.Close(); } } public static void DeseralizeFromXML(ref cMySettings Settings) { FileStream fs = null; // do i have settings? if (File.Exists(XMLFilename) == true) { try { fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read); XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings)); Settings = xmlSerializer.Deserialize(fs); } catch { //load error of some sort, or OBJECT deserialize error //do we tell anyone? return; } finally { if ((fs != null)) fs.Close(); fs = null; } } } public static void Copy(ref cMySettings SourceSettings, ref cMySettings DestSettings) { DestSettings.FTPUsername = SourceSettings.FTPUsername; DestSettings.FTPPassword = SourceSettings.FTPPassword; DestSettings.FTPHostname = SourceSettings.FTPHostname; DestSettings.FTPHostpath = SourceSettings.FTPHostpath; DestSettings.FTPDirectorypath = SourceSettings.FTPDirectorypath; } public static void SetToDefault(ref Settings) { Settings.FTPUsername = "FTPUsername"; Settings.FTPPassword = "FTPPassword"; Settings.FTPHostname = "FTPHostname"; Settings.FTPHostpath = "Disque dur/Enregistrements/"; Settings.FTPDirectorypath = "C:\\temp\\FTPCopy\\"; } public static bool Compare(ref cMySettings Settings1, ref cMySettings Setting2) { bool functionReturnValue = false; if (Settings1.FTPUsername != Setting2.FTPUsername){functionReturnValue = false;return functionReturnValue;} if (Settings1.FTPPassword != Setting2.FTPPassword){functionReturnValue = false;return functionReturnValue;} if (Settings1.FTPHostname != Setting2.FTPHostname){functionReturnValue = false;return functionReturnValue;} if (Settings1.FTPHostpath != Setting2.FTPHostpath){functionReturnValue = false;return functionReturnValue;} if (Settings1.FTPDirectorypath != Setting2.FTPDirectorypath){functionReturnValue = false;return functionReturnValue;} functionReturnValue = true; return functionReturnValue; } }
Where is my error please !
Thanks
Last edited by pierrotm777; 01-31-2013 at 04:51 AM.
What's the actual error you are getting? I thought you could only set static variables inside a static method, but I might be wrong...
Current Worklog: TBA - '05 Rav4 (Mobo Dead).
www.rav4world.com - Forums for Rav4 owners
www.flavorfeasts.com - Cooking website/forums
-Basic forums live atm, come contribute!
I have found my issue with the msdn help !!!
the good translated class is :
http://msdn.microsoft.com/fr-fr/libr.../d15aekdk.aspxCode:using System.IO; using System.Xml; using System.Xml.Serialization; using System.Text; public class cMySettings { public string FTPUsername; public string FTPPassword; public string FTPHostname; public string FTPHostpath; public string FTPDirectorypath; private static string XMLFilename; public cMySettings() { SetToDefault(this); } public cMySettings(string FileName) { XMLFilename = FileName; if (Path.GetExtension(XMLFilename).ToLower() != "xml") XMLFilename = XMLFilename + ".xml"; if (File.Exists(XMLFilename) == false) SerializeToXML(new cMySettings()); } public cMySettings(cMySettings Settings) { this.FTPUsername = Settings.FTPUsername; this.FTPPassword = Settings.FTPPassword; this.FTPHostname = Settings.FTPHostname; this.FTPHostpath = Settings.FTPHostpath; this.FTPDirectorypath = Settings.FTPDirectorypath; } public static void SerializeToXML(cMySettings Settings) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings)); using (XmlTextWriter xmlTextWriter = new XmlTextWriter(XMLFilename, Encoding.UTF8)) { xmlTextWriter.Formatting = Formatting.Indented; xmlSerializer.Serialize(xmlTextWriter, Settings); xmlTextWriter.Close(); } } public static void DeseralizeFromXML(cMySettings Settings) { FileStream fs = null; // do i have settings? if (File.Exists(XMLFilename) == true) { try { fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read); XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings)); XmlReader reader = XmlReader.Create(fs); cMySettings i; i = (cMySettings)xmlSerializer.Deserialize(reader); } catch { //load error of some sort, or OBJECT deserialize error //do we tell anyone? return; } finally { if ((fs != null)) fs.Close(); fs = null; } } } public static void Copy(cMySettings SourceSettings,cMySettings DestSettings) { DestSettings.FTPUsername = SourceSettings.FTPUsername; DestSettings.FTPPassword = SourceSettings.FTPPassword; DestSettings.FTPHostname = SourceSettings.FTPHostname; DestSettings.FTPHostpath = SourceSettings.FTPHostpath; DestSettings.FTPDirectorypath = SourceSettings.FTPDirectorypath; } public static void SetToDefault(cMySettings Settings) { Settings.FTPUsername = "FTPUsername"; Settings.FTPPassword = "FTPPassword"; Settings.FTPHostname = "FTPHostname"; Settings.FTPHostpath = "Disque dur/Enregistrements/"; Settings.FTPDirectorypath = "C:\\temp\\FTPCopy\\"; } public static bool Compare(cMySettings Settings1,cMySettings Setting2) { bool functionReturnValue = false; if (Settings1.FTPUsername != Setting2.FTPUsername){functionReturnValue = false;return functionReturnValue;} if (Settings1.FTPPassword != Setting2.FTPPassword){functionReturnValue = false;return functionReturnValue;} if (Settings1.FTPHostname != Setting2.FTPHostname){functionReturnValue = false;return functionReturnValue;} if (Settings1.FTPHostpath != Setting2.FTPHostpath){functionReturnValue = false;return functionReturnValue;} if (Settings1.FTPDirectorypath != Setting2.FTPDirectorypath){functionReturnValue = false;return functionReturnValue;} functionReturnValue = true; return functionReturnValue; } }
and
http://msdn.microsoft.com/fr-fr/libr.../he66c7f1.aspx
I have always an issue with the deserialer function!
I have tried:
and also:Code:public static void DeseralizeFromXML(cMySettings Settings) { FileStream fs = null; // do i have settings? if (File.Exists(XMLFilename) == true) { try { fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read); XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings)); Settings = (cMySettings)xmlSerializer.Deserialize(fs); } catch { //load error of some sort, or OBJECT deserialize error //do we tell anyone? return; } finally { if ((fs != null)) fs.Close(); fs = null; } } }
But when i try to read my variable , by example PluginSettings.COMPort , the variable is empty !Code:public static void DeseralizeFromXML(cMySettings i) { FileStream fs = null; // do i have settings? if (File.Exists(XMLFilename) == true) { try { fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read); XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings)); XmlReader reader = XmlReader.Create(fs); //cMySettings i; i = (cMySettings)xmlSerializer.Deserialize(reader); } catch { //load error of some sort, or OBJECT deserialize error //do we tell anyone? return; } finally { if ((fs != null)) fs.Close(); fs = null; } } }
Ok, If i convert the VB.NET code that follow , i obtain an error for the red line !!!
In c# i obtain these lines:Code:Public Shared Sub DeseralizeFromXML(ByRef Settings As cMySettings) Dim fs As FileStream = Nothing ' do i have settings? If File.Exists(XMLFilename) = True Then Try fs = New FileStream(XMLFilename, FileMode.Open, FileAccess.Read) Dim xmlSerializer As New XmlSerializer(GetType(cMySettings)) Settings = xmlSerializer.Deserialize(fs) Catch 'load error of some sort, or OBJECT deserialize error 'do we tell anyone? Exit Sub Finally If Not fs Is Nothing Then fs.Close() fs = Nothing End Try End If End Sub
the error is 'can't convert the object in cMySettingsCode:public static void DeseralizeFromXML(cMySettings Settings) { FileStream fs = null; // do i have settings? if (File.Exists(XMLFilename) == true) { try { fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read); XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings)); Settings = xmlSerializer.Deserialize(fs); } catch { //load error of some sort, or OBJECT deserialize error //do we tell anyone? return; } finally { if ((fs != null)) fs.Close(); fs = null; } } }
Thanks for your help !!!
not seeing this completely
but in c# classes that you want to serialize should have a Serializable attribute
[Serializable]
then serial/deserial is easy!
that code looks like my code
anyway, there is tons of C# examples outside of this forum...
attached is example
hey I got some C# books, can you read English well enough?, id give you a book for free, just pay shipping
good books.... ill get you more info... and titles
(yea, real books, made of paper!)
-Thanks
Mitch
www.rush2112.net
"Did you test it in carwings??"
Sun, Come shine my way
May healing waters bury all my pain
Wind, Carry me home
The fabric of reality is tearing apart
The piece of me that died
Will return To live again
micthjs, you are the best :-)
Into my Deserialize command , i have just added a 'ref' i don't know why and all is running well know so my code is :
And i initialise the xml file like that:Code:public static void DeseralizeFromXML(ref cMySettings Settings) { FileStream fs = null; // do i have settings? if (File.Exists(XMLFilename) == true) { try { fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read); XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings)); //cMySettings i; Settings = (cMySettings)xmlSerializer.Deserialize(fs); } catch { //load error of some sort, or OBJECT deserialize error //do we tell anyone? return; } finally { if ((fs != null)) fs.Close(); fs = null; } } }
Code:DPath = pluginDataPath; //Settings with Xml file PluginSettings = new cMySettings(DPath + "iCarDuino"); // read in defaults cMySettings.DeseralizeFromXML(ref PluginSettings); // copy to temp TempPluginSettings = new cMySettings(PluginSettings);
Bookmarks