[dba-VB] VB.Net - Option Strict disallows implicit conversion

jwcolby jwcolby at colbyconsulting.com
Thu Jul 19 10:48:41 CDT 2007


I used DirectCast.  And it all works.  AMAZING!!!

Charlotte, you probably use this all of the time.  The example code builds a
"serializable class" with functions that do the serialization in / out to a
data stream, and functions that get a stream from / to a file.  Then you
build a properties class that inherits the serializable class, which class
then allows you to define your properties.  Since the properties class
inherits the code to serialize itself, it can do so without further adoo.

I then use that in my form to store the control data into a properties class
instance and can now store that out to an xml file using the .net
serializable stuff embedded in the inherited "serializable class".  Likewise
retrieve it later.  

I have to say that the dim statement to do the restore is a mess though!

To save:
        Dim lclsIOData As New clsIOData
        PopulateIODataFromForm(lclsIOData)
        Dim strFileName As String = lclsIOData.mDataFileName("FormData")
        lclsIOData.mSave(strFileName)

To restore:
        Dim lclsIOData As clsIOData
        lclsIOData =
DirectCast(clsSerializableData.Load(clsIOData.mDataFileName("FormData"),
GetType(clsIOData)), clsIOData)
        mPopulateFormFromIOData(lclsIOData)

It is really a reasonably small amount of code setting up the serializable
class, but once done, any data class can be serialized to xml.  From what I
can understand I could also serialize it to a table if I wanted to simply by
having another function that opened a stream to / from a table instead of a
file.  I will leave that for another day.

Cool stuff.  

I was just about to ask about binding this to a form when your next email
came in.  The only question I have is, is it possible to bind to a class
where the open event of the form itself is going to load the data into the
class?  It seems kinda "cart before the horse".

John W. Colby
Colby Consulting
www.ColbyConsulting.com 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Charlotte Foust
Sent: Thursday, July 19, 2007 11:09 AM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] VB.Net - Option Strict disallows implicit conversion

That's one problem I always have with the books too, they tend to use a lot
of implicit conversion.  Of course, you learn a lot from making the examples
work with option strict on.  There are various ways to do it, John.  You can
use a Ctype() function to convert the object to a clsSomething like this:
Ctype(SerializeData(....), clsSomething).  You can also use a DirectCast
function.

Charlotte Foust 





More information about the dba-VB mailing list