[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-11-07
7
Medium Priority
?
219 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 64

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 64

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

872 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