473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Serialization in C# - is there another way to solve my problem?

Paul Johnson
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
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Xml.Serialization;
  5.  
  6. namespace serialize
  7. {
  8.         [Serializable]
  9.         public class testing
  10.         {
  11.                 public double a;
  12.                 public float b;
  13.                 public int c;
  14.                 public List<string> d = new List<string>();
  15.                 public string e;
  16.  
  17.                 public double A
  18.                 {
  19.                         get { return a; }
  20.                         set { a = value; }
  21.                 }
  22.  
  23.                 public float B
  24.                 {
  25.                         get {return b; }
  26.                         set {b = value;}
  27.                 }
  28.  
  29.                 public int C
  30.                 {
  31.                         get {return c;}
  32.                         set {c = value;}
  33.                 }
  34.  
  35.                 public string E
  36.                 {
  37.                         get {return e;}
  38.                         set {e = value;}
  39.                 }
  40.         }
  41.  
  42.         class MainClass
  43.         {
  44.                 public static void Main (string[] args)
  45.                 {
  46.                         Console.WriteLine ("Serialize test");
  47.                         testing t = new testing();
  48.                         XmlSerializer x  = new XmlSerializer(t.GetType
  49. ());
  50.                         StringWriter o = new StringWriter();
  51.                         x.Serialize(o, t);
  52.                         Console.WriteLine("Done - Serialized data =
  53. {0}", o.ToString());
  54.                         class1 c1 = new class1();
  55.                         c1.printsomething(o.ToString());
  56.                         class2 c2 = new class2();
  57.                         t = c2.dosomething(o.ToString());
  58.                         Console.WriteLine (t.B); // should read 3.14
  59.                         foreach(string s in t.d)
  60.                                 Console.WriteLine (s); // should read
  61. wibble and Darn
  62.                         Console.ReadKey();
  63.                 }
  64.         }
  65.  
  66.         class class1
  67.         {
  68.                 static testing t;
  69.                 public void printsomething(string s)
  70.                 {
  71.                         XmlSerializer x = new
  72. XmlSerializer(typeof(testing));
  73.                         StringReader m = new StringReader(s);
  74.                         t = (testing)x.Deserialize(m);
  75.                         fireoff();
  76.                         Console.WriteLine (t.E);
  77.                         Console.WriteLine (t.C);
  78.                 }
  79.  
  80.                 private void fireoff()
  81.                 {
  82.                         t.E = "Wibble";
  83.                         t.C = 3;
  84.                 }
  85.         }
  86.  
  87.         class class2
  88.         {
  89.                 testing t;
  90.                 public testing dosomething(string s)
  91.                 {
  92.                         XmlSerializer x = new
  93. XmlSerializer(typeof(testing));
  94.                         StringReader m = new StringReader(s);
  95.                         t = (testing)x.Deserialize (m);
  96.                         t.B = 3.14f;
  97.                         t.d.Add("wibble");
  98.                         t.d.Add ("Darn");
  99.                         return t;
  100.                 }
  101.         }
  102. }
  103.  
Nov 30 '11 #1
0 1156

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mikeb | last post by:
I have a class (settings, etc.) which is derived from CollectionBase and contains a number of fields and a few events. When I create an instance of this class in my app, and serialize it, it works....
5
by: Tamir Khason | last post by:
Hi, all Two classes Foo1 and Foo2 Foo1 uses Foo2 as reference Both are strong name signed with the same key pair I'm performing Binary Serialization of object inside Foo2 from Foo1 as following:...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
6
by: Federico | last post by:
Hi, this is what I can do: - Create new solutions using VS.Net ASP.Net - Save the solutions, build the solution, view in browser with the solution still open. But, once I close the solution, I...
3
by: | last post by:
Is there some mental blockage with OpenFileDialog not being able to navigate to the required directory? What do we do instead?
0
by: Jitesh | last post by:
I am facing a problem in webservice, I want to know what will be the exact procedure to solve the problem............. What I want to do............ I have a table named order in SQL Server....
1
by: gkellymail | last post by:
I currently have a class with 29 data adapters and I will probably add a few more as time goes on. When I was testing earlier, i had 28 of them pointing to one connection and 1 pointing to a...
9
by: norvinl | last post by:
Hi, I'm serializing a class and using shared memory / deserialization to send it to other processes. I can serialize with one app and deserialize with another instance of the same app. But...
17
by: khajeddin | last post by:
the problem is: Create a class HugeInteger which use 40-element array of digits to store integers as larg as 40 digits each.Provide methods input, output, add, subtract how to add to arrays ?
5
by: rsingh | last post by:
HI guies can u help me to solve this problem. Write a program that prints a table of the binary equivalents of the decimal numbers in the range 1 through 256.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.