Persisting fixed data

  • Thread starter Thread starter John Dann
  • Start date Start date
J

John Dann

I'm stretching my fairly elementary vb.net experience by trying to
write a report generator for a specialised utility. The report will
contain a simple tabular output, but there could be up to 100 or more
column types from which the user can choose to include on a specific
report and each type may have eg 10 attributes: name, value, width,
formatting etc etc

All the types and attributes are known at design time and could be
used to populate eg a structure array, though there obviously are
other alternatives.

But the question is about how best to persist this data. I don't
really want to assign the attributes one at a time, ie one line of
code per attribute, because that would create 1000 lines of code or
more and be pretty cumbersome to maintain. But equally it would be
nice to avoid a more structured solution such as eg an external
database that could bloat the program. In fact being able to embed the
data within the main assembly would seem to be a more elegant
solution, but I guess simplicity is more important than elegance.

Anyone have any advice please, bearing in mind that I'm not an expert
vb.net user.

Thanks
JGD
 
If you create a class called "columntype", containing your attributes - you
can populate an array of column types and output them to an XML file with a
single loop. Simple example below, that doesn't fill in many of the blanks:


class myColumnType

private m_Name as String
private m_Value as Object
private m_Width as integer

public sub WriteXML (theNode as XmlNode)
...
... write my attributes to the xml tree
...
end sub

public sub ReadXML (theNode as XmlNode)
...
... read my attributes from the xml tree
...
end sub

end class


private m_Array as New ArrayList

m_Array.Add ( new MyColumnType ( bla bla bla ) )
m_Array.Add ( new MyColumnType ( foo foo foo ) )

for each theColumn as MyColumnType In m_Array
theColumn.WriteXML ( theXMLDocument)
Next
 
Serialize the data to a stream... the basics are pretty easy
0) decorate your class with <Serializable()>
1) create a stream: Dim fstream As New FileStream("MyFile.bin",
FileMode.Create, FileAccess.Write, FileShare.None)
2) create a formatter: Dim bf as New BinaryFormatter()
3) serialize: bf.Serialize(fstream,YourClass)
4) clean-up: fstream.Close()

there are some special consideration about the deserialization -- you
will need a special class constructor... the .NET Framework will
serialize an object graph (i.e. it will serialize objects within your
object).

here are some links -- the first has a demo...
Object Serialization in Visual Basic .NET (Adventures in .NET)
http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet09252001.asp?frame=true

Serializing Objects (.NET Framework Developer's Guide)
http://msdn.microsoft.com/library/en-us/cpguide/html/cpovrserializingobjects.asp?frame=true

good links to related topics here...
Binary Serialization (.NET Framework Developer's Guide)
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconbinaryserialization.asp?frame=true
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top