Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 19, 2012 06:21 AM by Knecke
Apr 16, 2012 03:03 PM|LINK
I've written the following code but for some reason vb is throwing a wobbly (see image below) but don't understand why, because my Risks array is not 'being used before value is assigned'
Private Sub getUsersRisks(ByVal intUserID As Integer)
Dim SQLconn As New SqlConnection
Dim SQLcmd As New SqlCommand
Dim strConn As String
Dim mySQLReader As SqlDataReader
Dim returnval As Integer
Dim Risks() As Integer
Dim intCount As Integer = 0
strConn = ConfigurationManager.ConnectionStrings("MFM_CRM_ConnectionString").ConnectionString
SQLconn.ConnectionString = strConn
SQLcmd.Connection = SQLconn
SQLcmd.CommandType = Data.CommandType.StoredProcedure
SQLcmd.CommandText = "some stored procedure"
SQLcmd.Parameters.Add("@Returnstep", Data.SqlDbType.Int).Direction = ParameterDirection.ReturnValue
SQLcmd.Parameters.Add("@UserID", Data.SqlDbType.Int).Direction = ParameterDirection.Input
SQLcmd.Parameters("@UserID").Value = intUserID
mySQLReader = SQLcmd.ExecuteReader
returnval = SQLcmd.Parameters("@Returnstep").Value
If Not IsDBNull(mySQLReader(0)) Then
ReDim Preserve Risks(intCount)
Risks(intCount) = mySQLReader(0)
intCount = intCount + 1
For r As Integer = 0 To Risks.Length - 1
SQLconn = Nothing
Catch ex As Exception
Image showing error message:
Please help/advise me.
Apr 16, 2012 03:07 PM|LINK
It could be unassigned if this line is true:
Apr 17, 2012 08:25 AM|LINK
Yes, as Knecke says, you could get to usage of Risks without it being assigned; remember that declaration of an array isn't assignment. You can easily get rid of the error by assigning Risks at declaration time: eg
Dim Risks() As Integer = Nothing
But you need to ensure that the rest of the code takes care of the case that could occur - if that first column is null.
Apr 19, 2012 06:21 AM|LINK
Did you solve this issue?