• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

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
0
DaveMon
Asked:
DaveMon
1 Solution
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now