GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-May-2009, 15:20
Falcon Eyes Falcon Eyes is offline
New Member
 
Join Date: Mar 2006
Posts: 14
Falcon Eyes has a little shameless behaviour in the past

Serialization problem in C#


Hi Every Body
I tried to write phone index Application,but i got a problem with the serialization of my object,and i don't know why can some body help please and below is my code in detail and i attached the error message

main form code

C-SHARP / C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace PHIndex
{
    public partial class MainFrm : Form
    {
        CTeleRecord phoneRecord;
        public MainFrm()
        {
            InitializeComponent();
            phoneRecord = new CTeleRecord();
            phoneRecord.errorEvnt += new errorEventHandler(ErrorHnd);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            phoneRecord.GSName = textBox1.Text;
            phoneRecord.GShomeNumber = textBox2.Text;
            FileStream outPutSt = new FileStream("test.bin", FileMode.Create, FileAccess.Write);
            BinaryFormatter outBinFormater = new BinaryFormatter();
            outBinFormater.Serialize(outPutSt, phoneRecord);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BinaryFormatter inpuBinFormater = new BinaryFormatter();
            FileStream inPutSt = new FileStream("test.bin", FileMode.Open, FileAccess.Read);
            phoneRecord = (CTeleRecord)inpuBinFormater.Deserialize(inPutSt);
             textBox1.Text= phoneRecord.GSName;
             textBox2.Text= phoneRecord.GShomeNumber;
        }
        void ErrorHnd(object sender, myEventsArg  e)
            {
                MessageBox.Show(e.GetErrorMessage,"Error Code"+e.GetErrorCode);
            }

   }
}

and the telephone Record Class code

C-SHARP / C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhoneIndex;

namespace PHIndex
{
    ///class for holding telephone index object
    public delegate void errorEventHandler(object sender, myEventsArg e);   
        [Serializable]
    class CTeleRecord
    
        {
            string name;
            string homeNumber;
            string mobileNumber;
            string workNumber;
            string emailAddress;
            bool errorFlag;
            public event errorEventHandler errorEvnt;
       
        /// <summary>
        /// This Fn will fire the error event
        /// </summary>
        /// <param name="e"></param>
        void fireError(myEventsArg e)
            {
                if (errorEvnt != null)
                    {
                     ///invoke the delegate object   
                     errorEvnt(this,e);
                    }
            }


        /// <summary>
        /// reset error flag
        /// </summary>
        public void ResetErrorFlag()
        { errorFlag = false; }

        public int getRecordLength()
        {
            

            return (name.Length + homeNumber.Length + mobileNumber.Length + workNumber.Length 
                    + emailAddress.Length);
          
        }

        public void resetTelRecordObj()
        {
            name = "";
            homeNumber = "";
            mobileNumber = "";
            workNumber = "";
            emailAddress = "";
        }

        /// <summary>
        /// Property for Get or Set home phone number
        /// </summary>
        public string GShomeNumber
        {
            get
            {
                return homeNumber;
            }
            set
            {
                if (value.Length > 0)
                    try
                    {
                        //This property for checking validation of number;
                                 Convert.ToInt64(value);
                    }

                    catch (FormatException)
                    {
                        // System.Windows.Forms.MessageBox.Show("Numbers can't be a string");
                        //errorFlag = true;

                        myEventsArg e1 = new myEventsArg(1, "Numbers can't be a string");
                        fireError(e1);
                    }

                try
                {
                    if (value.Length == 0 || value.Length == 8 || value.Length == 10 || value.Length == 13)
                        homeNumber = value;
                    else
                        throw new Exception("Invalid Home Telephone Number Length (8 or 10 or 13 number)");
                }
                catch (Exception)
                {
                    myEventsArg e11 = new myEventsArg(1, "Invalid Home Telephone Number Length");
                    fireError(e11);
                }

            }
        }

        /// <summary>
        /// Property for Get or Set work telephone number
        /// </summary>
        public string GSWorkNumber
        {
            get{return workNumber;}
            
            set
            {
                if (value.Length > 0)
                    try
                     {
                        //This property for checking validation of number;
                        Convert.ToInt64(value)
                          ;
                     }

                    catch (FormatException)
                    {
                        //System.Windows.Forms.MessageBox.Show("Numbers can't be a string");
                        myEventsArg e3 = new myEventsArg(3, "Numbers can't be a string");
                        fireError(e3);
                    }

                try
                {
                    if (value.Length == 0 || value.Length == 8 || value.Length == 10 || value.Length == 13)
                        workNumber = value;
                    else
                        throw new Exception("Invalid Work Telephone Number Length (8 or 10 or 13 number)");
                }
                catch (Exception error)
                {
                    System.Windows.Forms.MessageBox.Show(error.Message, "Invalid Work Telephone Number Length");
                    myEventsArg e33 = new myEventsArg(3, "Invalid Work Telephone Number Length");
                    fireError(e33);

                }

            }
        }

        /// <summary>
        /// Property for Get or Set mobile phone number
        /// </summary>
        public string GSmobileNumber
        {
            get
            {
                return mobileNumber;
            }
            set
            {
                if (value.Length > 0)
                    try
                    {
                        //This property for checking validation of number;
                        Convert.ToInt64(value)
                            ;
                    }

                    catch (FormatException)
                    {
                        //System.Windows.Forms.MessageBox.Show("Numbers can't be a string");
                        myEventsArg e2 = new myEventsArg(2, "Numbers can't be a string");
                        fireError(e2);
                    }

                try
                {
                    if (value.Length == 0 || value.Length == 10 || value.Length == 13)
                        mobileNumber = value;
                    else
                        throw new Exception("Invalid Home Telephone Number Length (8 or 10 or 13 number)");
                }
                catch (Exception)
                {
                    //System.Windows.Forms.MessageBox.Show(error.Message, "Invalid Home Telephone Number Length");
                    myEventsArg e22 = new myEventsArg(2, "Invalid Home Telephone Number Length");
                    fireError(e22);
                }
             }
        }
        /// <summary>
        /// Property for Get or Set user name
        /// </summary>
        public string GSName
        {
            get
            {
                return name;
            }

            set
            {
                try
                {
                    if (value.Length == 0 || value.Length <= 50)
                        name = value;
                    else
                        throw new Exception("Invalid Name Length (Max 50 Character)");
                }
                catch (Exception)
                {
                    //System.Windows.Forms.MessageBox.Show("You Wrote "+ value.Length + "char",errorName.Message);
                    myEventsArg e0 = new myEventsArg(0, "You Wrote " + value.Length + "char" + " (Max 30 Character)");
                    fireError(e0);
                }
            }
        }
        /// <summary>
        /// Property for Get or Set user email
        /// </summary>

        public string GSEmail
        {
            get
            {
                return emailAddress;
            }
            set
            {
                try
                {
                    if (value.Contains('@') && value.Contains('.') || value.Contains(""))
                        emailAddress = value;

                    else
                        throw new Exception("Invalid Email format");
                }

                catch (Exception)
                {

                    myEventsArg e4 = new myEventsArg(4, "Invalid Email format");
                    fireError(e4);
                }
            }
        }
        /// <summary>
        /// get error flag status    
        /// </summary>
        public bool ErrorFlag
        { get { return errorFlag; } }
        
        /// <summary>
        /// Return the Record Length
        /// </summary>
        public int GetRecordLength
        {get{ return getRecordLength(); }}


    }
}
 
  
        
 

Attached Images
File Type: png Error.png (22.7 KB, 19 views)
Last edited by LuciWiz : 13-May-2009 at 00:19. Reason: Please insert your C# code between [c#] & [/c#] tags
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Web Server newbie problem niss3 Apache Web Server Forum 1 13-Apr-2009 19:38
Torrents Download Problem chandeep Computer Software Forum - Linux 7 09-Oct-2006 23:37
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 13:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 11:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 08:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.