[Mono-list] Serialized data Q

Paul F. Johnson paul at all-the-johnsons.co.uk
Tue Nov 29 13:53:09 EST 2011


Hi,

I've got a really dumb and probably very simple question to answer.

I've serialized a class and am passing it between other classes (see
below). Is there a way *other than what I have done* so that when
control is return to Main, the output is what it should be (see the
comments).

Ideally, I'd set in class 2 for Main to be able access the information.

Thanks

Paul

using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace serialize
{
	[Serializable]
	public class testing
	{
		public double a;
		public float b;
		public int c;
		public List<string> d = new List<string>();
		public string e;
		
		public double A
		{
			get { return a; }
			set { a = value; }
		}
		
		public float B
		{
			get {return b; }
			set {b = value;}
		}
		
		public int C
		{
			get {return c;}
			set {c = value;}
		}
		
		public string E
		{
			get {return e;}
			set {e = value;}
		}
	}
		
	class MainClass
	{
		public static void Main (string[] args)
		{
			Console.WriteLine ("Serialize test");
			testing t = new testing();
			XmlSerializer x  = new XmlSerializer(t.GetType ());
			StringWriter o = new StringWriter();
			x.Serialize(o, t);
			Console.WriteLine("Done - Serialized data = {0}", o.ToString());
			class1 c1 = new class1();
			c1.printsomething(o.ToString());
			class2 c2 = new class2();
			t = c2.dosomething(o.ToString());
			Console.WriteLine (t.B); // should read 3.14
			foreach(string s in t.d)
				Console.WriteLine (s); // should read wibble and Darn
			Console.ReadKey();
		}
	}
	
	class class1
	{
		static testing t;
		public void printsomething(string s)
		{
			XmlSerializer x = new XmlSerializer(typeof(testing));
			StringReader m = new StringReader(s);
			t = (testing)x.Deserialize(m);
			fireoff();
			Console.WriteLine (t.E);
			Console.WriteLine (t.C);
		}
		
		private void fireoff()
		{
			t.E = "Wibble";
			t.C = 3;
		}
	}
	
	class class2
	{
		static testing t;
		public testing dosomething(string s)
		{
			XmlSerializer x = new XmlSerializer(typeof(testing));
			StringReader m = new StringReader(s);
			t = (testing)x.Deserialize (m);
			t.B = 3.14f;
			t.d.Add("wibble");
			t.d.Add ("Darn");
			return t;
		}
	}
}
-- 
Vertraue mir, ich weiss, was ich mache...



More information about the Mono-list mailing list