[dba-VB] .net piddly warnings

John W. Colby jwcolby at colbyconsulting.com
Sun Jul 24 20:16:57 CDT 2005


Doris,

I decided to make my SysVar data object a structure rather than a class.  I
haven't really decided whether that was a good idea or not, but the
structure for a SysVar just holds 6 pieces of data:

The PKID from the table - Long
The SysVar name - String
The SysVar value - Object
The SysVar memo - String
And a pair of boolean flags - UserEditable and AllowOverride

I have run into a handful of places in the past where I built a system where
I would load a table, each record represented by a data class, and a
collection class that holds a data class instance for every record in the
table.  SysVars is one of these places.

So it seemed that a structure was a good idea for the data class.  However I
still wanted a single method in the data class that I called to initialize
every variable in the data class, passing in (in this case) all 6 pieces of
data from a record to the data class.  Thus the data class init code looked
something like:

    Public Sub Init(ByVal lintID As Int32, ByVal lstrName As String, ByVal
lobjValue As Object, ByVal lstrMemo As String, _
    ByVal lblnUserEditable As Boolean, ByVal lblnAllowOverride As Boolean)
        mintID = lintID
        mstrName = lstrName.ToLower
        mobjValue = lobjValue
        mstrMemo = lstrMemo.ToLower
        mblnUserEditable = lblnUserEditable
        mblnAllowOverride = lblnAllowOverride
    End Sub

With the collection class method that creates these things looked something
like:

        mOleDBReader = mOleDBCmd.ExecuteReader
        With mOleDBReader
            While .Read
                NewSysVar(mOleDBReader("SV_ID"), mOleDBReader("SV_VarName"),
mOleDBReader("SV_VarValue"), mOleDBReader("SV_Memo"),
mOleDBReader("SV_UserEditable"), mOleDBReader("SV_AllowOverride"))
            End While
        End With

    Public Sub NewSysVar(ByVal lintID As Int32, ByVal lstrName As String,
ByVal lobjValue As Object, ByVal lstrMemo As String, _
        ByVal lblnUserEditable As Boolean, ByVal lblnAllowOverride As
Boolean)
        Dim lSysVar As stcSysVar
        lSysVar.Init(lintID, lstrName, lobjValue, lstrMemo,
lblnUserEditable, lblnAllowOverride)
        Add(lSysVar)
    End Sub

The line:

        lSysVar.Init(lintID, lstrName, lobjValue, lstrMemo,
lblnUserEditable, lblnAllowOverride)

Gives me this error I am discussing - "lSysVar is used before it is assigned
a value."

This is one reason I am not sure that the Structure is necessarily better
than a class at least in this case.  If I had a class I would just have a
New() constructor that I fed the data values into.  A structure doesn't have
a New (or a constructor), and it "expects" that each data item of the
structure would be accessed individually, set, and then read.  Because I
call a method with all values at once, I never "set" a property first and I
get this annoying error that never goes away.

Sigh.

Yea, I could set a property just to cause the error message to go away but I
don't want to.  I could also have gotten rid of NewSysVar and just done each
property individually, but I don't want to.

John W. Colby
www.ColbyConsulting.com 

Contribute your unused CPU cycles to a good cause:
http://folding.stanford.edu/

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Mike & Doris
Manning
Sent: Sunday, July 24, 2005 2:29 PM
To: ebarro at afsweb.com; dba-vb at databaseadvisors.com
Subject: RE: [dba-VB] .net piddly warnings


In VB.Net, you can combine the declaration and the initialization on one
line as follows:

Dim str as String = vbNullString
Dim bol as Boolean = False
Dim int as Integer = 0

Doris Manning
Database Administrator
Hargrove Inc.





More information about the dba-VB mailing list