Doesnt return a value on all code paths

Asked By Travis Smith
31-Jul-10 01:31 AM
Earn up to 0 extra points for answering this tough question.
I am getting an error that says the function doesn't return a value on all code paths, i am not exactly sure where the error is being cause, thanks for the help. The errors comes from the empName function. Thanks in advance


Public Class Form1
  Dim Reader As System.IO.StreamReader
  Dim line As String
 
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
    Reader = New System.IO.StreamReader("Program 3 Data.txt")
     
  End Sub
 
  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    'selecting the item from the ComboBox with selected item property
    Dim emp1 As String
    emp1 = ComboBox1.SelectedItem
 
    TextBox1.Text = emp1
    RichTextBox1.Text = empName(emp1)
    Me.RichTextBox1.Clear()
 
  End Sub
 
  Private Function empName(ByVal employee As String) As String
 
 
    line = Reader.ReadLine()
    While Reader.EndOfStream = False
 
      If InStr(line, employee) Then ' <> 0 Then
        empName = employee + line + vbLf
      End If
    End While
 
  End Function
 
 
End Class
 

  re: Doesnt return a value on all code paths

Reena Jain replied to Travis Smith
31-Jul-10 03:23 AM
Hello,

just put return empName after end while because you are using a string type function and string value should be return through funtion. here is your updated code

Private Function empName(ByVal employee As String) As String
 
 
  line = Reader.ReadLine()
  While Reader.EndOfStream = False
 
    If InStr(line, employee) Then ' <> 0 Then
    empName = employee + line + vbLf
    End If
  End While
Return empName
 
  End Function

Hope this will help

  re: Doesnt return a value on all code paths

Travis Smith replied to Reena Jain
31-Jul-10 02:07 PM
Reena,

   thanks for your replay but i have tried that, and i get an error, that empName variable is being used before its assigned a value, that's why i have had so much trouble, because what i thought should work , hasn't

  re: Doesnt return a value on all code paths

Reena Jain replied to Travis Smith
02-Aug-10 01:26 AM
hi,

you just declare the other var in function and assign the value of empname in it and return this variable. This will solve your problem.

Thanks
  re: Doesnt return a value on all code paths
wally eye replied to Travis Smith
02-Aug-10 01:18 PM
You might want to return text back to the calling function if no empname is found.  If you put a line in as:

empname = "Employee Not Found" + 0 + vbLf

before the while block, then empname will always have a value assigned to it, regardless of whether or not the right employee is found in the while statement.  You might then want to check in your calling code if "Employee Not Found" was returned and do something different.
Create New Account