Solved

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

Posted on 2006-11-07
7
211 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

21 Experts available now in Live!

Get 1:1 Help Now