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'
VB code...
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
Try
strConn = ConfigurationManager.ConnectionStrings("MFM_CRM_ConnectionString").ConnectionString
SQLconn.ConnectionString = strConn
SQLconn.Open()
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
ddlRiskTitle.Items.Clear()
ddlRiskTitle.Items.Add("")
While mySQLReader.Read
If Not IsDBNull(mySQLReader(0)) Then
ReDim Preserve Risks(intCount)
Risks(intCount) = mySQLReader(0)
intCount = intCount + 1
End If
End While
For r As Integer = 0 To Risks.Length - 1
Call getRiskDetails(Convert.ToInt32(Risks(r)))
Next
SQLconn.Close()
SQLconn = Nothing
Catch ex As Exception
End Try
End Sub
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.
Marked as answer by sconly on Apr 20, 2012 11:56 AM
sconly
Member
62 Points
112 Posts
Help with Arrays, please.
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'
VB code...
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 Try strConn = ConfigurationManager.ConnectionStrings("MFM_CRM_ConnectionString").ConnectionString SQLconn.ConnectionString = strConn SQLconn.Open() 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 ddlRiskTitle.Items.Clear() ddlRiskTitle.Items.Add("") While mySQLReader.Read If Not IsDBNull(mySQLReader(0)) Then ReDim Preserve Risks(intCount) Risks(intCount) = mySQLReader(0) intCount = intCount + 1 End If End While For r As Integer = 0 To Risks.Length - 1 Call getRiskDetails(Convert.ToInt32(Risks(r))) Next SQLconn.Close() SQLconn = Nothing Catch ex As Exception End Try End SubImage showing error message:
Please help/advise me.
Thanks.
Knecke
Contributor
3712 Points
838 Posts
Re: Help with Arrays, please.
Apr 16, 2012 03:07 PM|LINK
It could be unassigned if this line is true:
If Not IsDBNull(mySQLReader(0)) Then
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
Dave Sussman
All-Star
37716 Points
5005 Posts
ASPInsiders
MVP
Re: Help with Arrays, please.
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
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.
Knecke
Contributor
3712 Points
838 Posts
Re: Help with Arrays, please.
Apr 19, 2012 06:21 AM|LINK
Did you solve this issue?
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter