Solved

Returning a true or false from a function that has boolean as return value

Posted on 2006-11-07
7
215 Views
Last Modified: 2010-04-16
I am calling the confirmuser method from a click event like this

bool answer = confirmUser();


Here is confirmuser() function below

      public bool confirmUser()
        {
            try
            {
                StreamReader sr = new StreamReader("C:\\list.txt");
                {
           
                    string line;
                    char[] delimeterchars = { ',' };
                    while ((line = sr.ReadLine()) != null)//begin while
                    {
                        String[] result = line.Split(delimeterchars);
                        if ((result[0] == GetUserName) && (result[1] == GetPassword))
                        {
                            break;
                            return true;
                        }
                                   
                    }//end while
                    return false;
                }
                 
            }
               
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());

            }


This method is reading text file and getting a username
and password entered on a modal form. Once the username
and password agrees with the one in the text file, I want the
program to break the loop and return a true otherwise
it should return a false.

I keep getting an error which says "Unreachable code detected" and
"not all code paths returns a value"

Can anyone assist please?

0
Comment
Question by:Sirdots
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 17892823
   public bool confirmUser()
        {
          bool result = false;
            try
            {
                StreamReader sr = new StreamReader("C:\\list.txt");
                {
           
                    string line;
                    char[] delimeterchars = { ',' };
                    while ((line = sr.ReadLine()) != null)//begin while
                    {
                        String[] result = line.Split(delimeterchars);
                        if ((result[0] == GetUserName) && (result[1] == GetPassword))
                        {
                            break;
                            result = true;
                        }
                                   
                    }//end while
                }
                 
            }
               
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());

            }
            finally
            {
                return result;
            }
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17892825
Try...

        public bool confirmUser()
        {
            bool ret = false;
            try
            {
                StreamReader sr = new StreamReader("C:\\list.txt");      
                string line;
                char[] delimeterchars = { ',' };
                while ((line = sr.ReadLine()) != null)
                {
                    String[] result = line.Split(delimeterchars);
                    if ((result[0] == GetUserName) && (result[1] == GetPassword))
                    {
                        ret = true;
                        break;
                    }                              
                }
                sr.Close();
            }            
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            return ret;            
        }
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17892853
Replace this code block:

        if ((result[0] == GetUserName) && (result[1] == GetPassword))
        {
             break;
             return true;
        }

With this code block, Just remove the break.

        if ((result[0] == GetUserName) && (result[1] == GetPassword))
        {
             return true;
        }

Fernando
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 29

Expert Comment

by:Nightman
ID: 17892873
Didn't see you already had a local variable called result.

Change to retval (or returnvalue or anything else you like) and return that
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17892895
Doh - can't return in finally! IdleMinds and FernandoSotos should work fine - no need for me to rewrite ;)
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17892912
Use Idle_Mind I forgot to move the other return to the end of the code block. ;=)
0
 

Author Comment

by:Sirdots
ID: 17893209
Thanks to everyone.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question