StackOverFlow????

shawne

Well-known member
Joined
Feb 22, 2006
Messages
103
Location
Pennsylvania
Programming Experience
10+
Anyone have a clue why the top function works fine, but the bottom function results in a StackOverFlow? dbServer, dbName, dbIdentification and dbPassword are the same values when either function is called. I'm sure the answer is staring me right in the face, but i'll be damned if I can see it.


VB.NET:
    Private Function sqlConnStr() As String
        sqlConnStr = "Server=" & dbServer & ";" & "Database=" & dbName & ";Integrated Security=SSPI"
        If InStr(UCase(dbIdentification), "N/A", CompareMethod.Binary) = False Then
            sqlConnStr = sqlConnStr & ";Uid=" & dbIdentification & ";Pwd=" & dbPassword
        End If
    End Function

    Private Function ADOConnStr() As String
        ADOConnStr = "Driver=" & dbDriver & ";Server=" & dbServer & ";Database=" & dbName
        If InStr(UCase(dbIdentification), "N/A", CompareMethod.Binary) = False Then
            ADOConnStr = ADOConnStr() & ";Uid=" & dbIdentification & ";Pwd=" & dbPassword
        End If
    End Function
 
Anyone have a clue why the top function works fine, but the bottom function results in a StackOverFlow? dbServer, dbName, dbIdentification and dbPassword are the same values when either function is called. I'm sure the answer is staring me right in the face, but i'll be damned if I can see it.


VB.NET:
    Private Function sqlConnStr() As String
        sqlConnStr = "Server=" & dbServer & ";" & "Database=" & dbName & ";Integrated Security=SSPI"
        If InStr(UCase(dbIdentification), "N/A", CompareMethod.Binary) = False Then
            sqlConnStr = sqlConnStr & ";Uid=" & dbIdentification & ";Pwd=" & dbPassword
        End If
    End Function
 
    Private Function ADOConnStr() As String
        ADOConnStr = "Driver=" & dbDriver & ";Server=" & dbServer & ";Database=" & dbName
        If InStr(UCase(dbIdentification), "N/A", CompareMethod.Binary) = False Then
            ADOConnStr = [SIZE=6][B]ADOConnStr[COLOR=red]()[/COLOR][/B][/SIZE] & ";Uid=" & dbIdentification & ";Pwd=" & dbPassword
        End If
    End Function

As the final line of ADOConnStr, you actually call the method again


I offer the following advice:

To return a value from a function do not use the old VB6 syntax of:
FunctionName = SomeValue

Use the new syntax of:
Return SomeValue


Do not use the function name as a variable
VB.NET:
Private Function Whatever() as String
  Dim retVal as String = "to be returned"
 
  Return retVal
End Function


Do not use functions for simplistic value returns. Functions are supposed to have names that are Verbs (actions) like Append(), Sort(), Reverse(), ToString(). For nouns, like MyConnStr, YourConnStr, PinkConnStr use properties:
VB.NET:
Private ReadOnly Property ADOConnStr as String
  Get
    Dim retVal as String
    'code
    Return retVal
  End Get
End Property


Use string.Format to build strings, not repeated concatenation. Do not use InStr, use string.Contains (case sensitive), string.Equals or string.IndexOf (can choose sensitive/insensitive) instead:
VB.NET:
Private ReadOnly Property ADOConnStr As String
  Get
    Dim retVal as String = String.Format("Driver={0};Server={1};Database={2}", dbDriver, dbServer, dbName)
 
    'use one of the following
    If dbIdentification.IndexOf("N/A", CurrentCultureIgnoreCase) = -1 Then
    If dbIdentification.ToUpper().Contains("N/A") Then
    If dbIdentification.ToUpper().Equals("N/A") Then
      retVal = retVal.Concat(string.Format("Uid={0};Pwd={1}", dbIdentification, dbPassword ))
    EndIf
 
    Return retVal
  End Get
End Property
 
Last edited:
Superb! Thanks for all the tips! Makes total sense. Only thing is your code sample seems to be a mix of C# and VB. Thanks again!
 
Back
Top