Serializing with Attributes

   

The first step to perform in serialization is to mark an object as serializable. To do this, place the attribute [Serializable] preceding the class declaration for your Windows Form. Believe it or not, that is all you need to do to enable object serialization. You still need to invoke the serialization process, but as of right now your object is ready to be serialized. Well, sort of. If you were to serialize your Windows Form object right now, you would get a SerializationException, as shown in Figure 24.2. This occurs because serialization is attempted on the base Form class, which has not been marked with the Serializable attribute.

Figure 24.2. Serialization exception notification dialog.

graphics/24fig02.jpg

To circumvent this problem, you will create a separate managed class that will serve as the serializable object. Add a new managed class named StateInfo before the class declaration of your main Windows Form. Apply the Serializable attribute to the class and remove the Serializable attribute from the ObjectSerializationForm class. The StateInfo class will keep state information for four properties within the form class. These properties correspond to the two values of the form's text boxes you created as well as the Size and Location properties of the form.

Within the private section of the StateInfo class, create four variables corresponding to the four properties you will be serializing. This can be seen on line 50 of Listing 24.2. Next, add the four properties to the public section of the StateInfo class, creating a get and set function for each property. These properties will simply return or set the values of the private data members you created earlier.

Listing 24.2 Creating a Serializable Object
 1: [Serializable]  2: __gc class StateInfo  3: {  4: public:  5:  6:     StateInfo()  7:     {  8:     }  9:     __property String* get_SerializedString() 10:     { 11:         return m_pstrSerialized; 12:     } 13: 14:     __property void set_SerializedString( String* pString ) 15:     { 16:         m_pstrSerialized = pString; 17:     } 18: 19:     __property String* get_NonSerializedString() 20:     { 21:         return m_pstrNonSerialized; 22:     } 23: 24:     __property void set_NonSerializedString( String* pString ) 25:     { 26:         m_pstrNonSerialized = pString; 27:     } 28: 29:     __property Size get_Form_Size() 30:     { 31:         return m_pFormSize; 32:     } 33: 34:     __property void set_Form_Size( Size pSize ) 35:     { 36:         m_pFormSize = pSize; 37:     } 38: 39:     __property Point get_Form_Location() 40:     { 41:         return m_pFormLocation; 42:     } 43: 44:     __property void set_Form_Location( Point pPoint ) 45:     { 46:         m_pFormLocation = pPoint; 47:     } 48: 49: private: 50:     String* m_pstrSerialized; 51:     [NonSerialized] String* m_pstrNonSerialized; 52:     Size m_pFormSize; 53:     Point m_pFormLocation; 54: }; 

   
Top


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net