Solved

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

Posted on 2006-11-07
7
214 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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