473,394 Members | 2,048 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,394 software developers and data experts.

Persisten Objects

Hi, i'm designing an 3 tier application, and i got the following question,
let's say that on my bussines layer i have an object Box that has inside an
object ItemCollection , that can store many Item objects, now let's say i'm
adding items to my box and suddenly my machine hangs up, making me lose my
box and all the items inside of it, there is any way that i can make my
objects to be persistent so if my system goes down i can reload that data??,
i was using a Dataset that i gave the same shape that my object logic, and
saving it to XML every time that an item was added, that make the trick, but
every time a added a property i had to add a lot of code, what can i do??

Thanks.
Alex.
Nov 20 '05 #1
1 1233
Hi Ale,

Here's a simple example that uses serialization. For this to work the class
must be serializable. The class I save in this example is at the end. I
hope this helps.

Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonSave.Click
Dim Test As New Sample("Craig", 33, 27.99D)
MsgBox(Test.Tax.ToString())

' Opens a file and serializes the object into it in binary format.
Dim ObjectWriter As Stream = File.Open(Me.Filename, FileMode.Create)
Dim Formatter As New BinaryFormatter()

Formatter.Serialize(ObjectWriter, Test)
ObjectWriter.Close()

End Sub

Private Sub ButtonRead_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonRead.Click

' Opens file "data.xml" and deserializes the object from it.
Dim ObjectWriter As Stream = File.Open(Me.Filename, FileMode.Open)
Dim Formatter As New BinaryFormatter()

formatter = New BinaryFormatter()

Dim Test As Sample = CType(Formatter.Deserialize(ObjectWriter),
Sample)
ObjectWriter.Close()

MsgBox(test.Tax.ToString())
End Sub

Option Strict On
Option Explicit On

<Serializable()> _
Public Class Sample

Public Sub New(ByVal Name As String, ByVal ID As Integer, ByVal Value
As Decimal)
m_Name = Name
m_ID = ID
m_Value = Value
End Sub

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Public Property ID() As Integer
Get
Return m_ID
End Get
Set(ByVal Value As Integer)
m_ID = Value
End Set
End Property

Public Property Value() As Decimal
Get
Return m_Value
End Get
Set(ByVal Value As Decimal)
m_Value = Value
End Set
End Property

Public ReadOnly Property Tax() As Decimal
Get
Return 0.085D * Value
End Get
End Property

Private m_Name As String
Private m_ID As Integer
Private m_Value As Decimal

End Class

Craig, VB.Net Team
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Ale K." <_N******@AleK.com>
Subject: Persisten Objects
Date: Fri, 5 Sep 2003 08:26:31 -0400
Lines: 14
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <ew*************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: nsc66.147.74-145.newsouth.net 66.147.74.145
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:134624
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi, i'm designing an 3 tier application, and i got the following question,
let's say that on my bussines layer i have an object Box that has inside an
object ItemCollection , that can store many Item objects, now let's say i'm
adding items to my box and suddenly my machine hangs up, making me lose my
box and all the items inside of it, there is any way that i can make my
objects to be persistent so if my system goes down i can reload that data??,i was using a Dataset that i gave the same shape that my object logic, and
saving it to XML every time that an item was added, that make the trick, butevery time a added a property i had to add a lot of code, what can i do??

Thanks.
Alex.

--
<My_Name>, <My_Community_Room> Team
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
6
by: Bart Ogryczak | last post by:
Hi, I´ve got a problem creating persistent cache, that would be shared between modules. There a supermodule, which calls submodules. I´d like submodules to use cache created in the supermodule....
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.