Solved

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

Posted on 2006-11-07
7
213 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
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 85

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 62

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 62

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now