VB.NET - Doesnt return a value on all code paths

Asked By Travis Smith
31-Jul-10 01:31 AM
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
 
  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
  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
  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
  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
help
code for the moment: Private Sub ComboBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus ComboBox1.Items.Clear() Dim m_fileStream As IO.StreamReader m_fileStream = New IO.StreamReader("USERS.txt") Dim headerLine As String = m_fileStream.ReadLine() While headerLine <> "" 'diviser headerline, utiliser que nom index se trouve en dehors des limites du tableau. Ã test.Form2.ComboBox1_GotFocus(Object sender, EventArgs e) dans Z: \ Projet \ Projet - NBV \ pocketPC \ test \ test \ Form2.vb:ligne 19 Ã System.Windows.Forms.Control.OnGotFocus(EventArgs e) Ã System.Windows.Forms.ComboBox.OnGotFocus(EventArgs e) Ã System.Windows.Forms.Control.WmSetFocus(Message& m) Ã System.Windows.Forms.Control.WndProc Message& m) Ã System.Windows.Forms.ComboBox.WndProc(Message& m) Ã System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) Ã System.Windows
reference required error Code has been simplified for clarity Form1: private void textBoxObject_TextChanged( object sender, EventArgs e) { Form2 .ClearTablesGrids(); } Form2: public static void ClearTablesGrids() { / / Fails on this object reference DataGridViewObject.DataSource another formhttp: / / stackoverflow.com / questions / 6080235 / calling-method-on-one-form-from-another-form-fill-combobox-on-form1-if-a-button hope this helps you hi, Use this code for solution : Form1: private void textBoxObject_TextChanged( object sender, EventArgs e) { Form2 f2 = new form2(); / / here you need to create object of form2 after that the method and it will work Try doing this instead: private void buttonRun_Click ( object sender , EventArgs e ) { StreamReader arrComputer = new StreamReader ( textBox1 . Text ); } When you OK your FileOpen dialog, you're storing the chosen filename on
Create a PaperSize and specify the custom paper size through the constructor and add to combobox. PaperSize pkCustomSize1 = new PaperSize("First custom size", 100, 200); comboPaperSize.Items.Add(pkCustomSize1); . . . / / Add list comboPaperSource.Items.Add(pkSource); } . . . / / Add list of printer resolutions found on the printer to the combobox. / / The PrinterResolution's ToString() method will be used to provide the display string. PrinterResolution pkResolution pkResolution = printDoc.PrinterSettings.PrinterResolutions[i]; comboPrintResolution.Items.Add(pkResolution); } . . . private void MyButtonPrint_Click(object sender, System.EventArgs e) { / / Set the paper size based upon the selection in the combo box. if (comboPaperSize also set Landscape or Portioret resettion like below one public void Printing() { try { streamToPrint = new StreamReader (filePath); try { printFont = new Font( "Arial" , 10); PrintDocument pd = new PrintDocument(); pd.PrintPage + = new PrintPageEventHandler PrinterSettings class for this. Try this code- public void Printing( string printer) { try { streamToPrint = new StreamReader (filePath); try { printFont = new Font( "Arial" , 10); PrintDocument pd = new PrintDocument(); pd.PrintPage + = new PrintPageEventHandler
private System.Windows.Forms.TextBox textBox1 = new System.Windows.Forms.TextBox(); private System.Windows.Forms.ComboBox comboBox1 = new System.Windows.Forms.ComboBox(); private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label(); private void generate_button_Click( object sender, System.EventArgs e) { CodeDomProvider provider = GetCurrentProvider(); CodeDomExample.GenerateCode(provider, CodeDomExample.BuildHelloWorldGraph()); / / Build the source file name with sourceFile = "TestGraph." + provider.FileExtension; } / / Read in the generated source file and / / display the source text. StreamReader sr = new StreamReader(sourceFile); textBox1.Text = sr.ReadToEnd(); sr.Close(); } private void compile_button_Click( object sender, System.EventArgs e) { CodeDomProvider provider = GetCurrentProvider(); / / Build the source file name with the appropriate / / language extension. String into " + cr.PathToAssembly + " with no errors." ; run_button.Enabled = true ; } } private void run_button_Click( object sender, System.EventArgs e) { Process.Start( "TestGraph.exe" ); } private CodeDomProvider GetCurrentProvider() { CodeDomProvider provider; switch (( string ) this .comboBox1.SelectedItem