VB2005 Express "Function does not return a value"?

Hello Experts,  I need help fixing a apperantly harmless error.....the app still runs  but  I get this warning on two functions

Function 'GetString' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.


CODE
************************
    Public Function GetString(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As String) As String
        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, _
           [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then GetString = _
           Left(objResult.ToString, intCharCount)
    End Function
***************************

thanks  all  for your  assistance
LVL 2
DaveMonAsked:
Who is Participating?
 
vadim63Commented:
Public Function GetString(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As String) As String
        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, _
           [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then

          Return Left(objResult.ToString, intCharCount)
    End Function
0
 
vadim63Commented:
Public Function GetString(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As String) As String
        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, _
           [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then

          Return Left(objResult.ToString, intCharCount)
else return "something"

    End Function
0
 
pauljk1619Commented:
Because your return statement only occurs if intCharCount > 0.....   Add an else with a different return value....  Otherwise if intCharCount is not > 0, you could get a null exception error.  


        If intCharCount > 0 Then
          Return Left(objResult.ToString, intCharCount)
        else
           Return ""
        end if
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sometimes its nice to declare a variable specifically for the return and then return that variable:

    Public Function GetString(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As String) As String

        Dim retString As String = "" ' or use Nothing instead of ""

        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, _
           [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then
          retString = Left(objResult.ToString, intCharCount)
        End If

        Return retString
    End Function
0
 
DaveMonAuthor Commented:
thanks to everyone for their assistance on this post !!!
Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.