locked
Why can I not get a return RRS feed

  • Question

  • Here is my code I am trying to get it to return a value for oType. Why can I not return a value from a if statement????

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.IO;

    using Microsoft.Win32;

    using System.Threading;

    using System.Text.RegularExpressions;

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main(string[] args)

    {

    int oType;

    if(File.Exists(@"C:\test.txt))

    {

    using (StreamReader sr = new StreamReader(@"C:\test.txt"))

    {

    string[] line = new string[305];

    string nLine;

    int c = 0;

    string officeType;

    string pattern = "OfficeType=.";

    while ((nLine = sr.ReadLine()) != null)

    {

    line[c] = nLine;

    c++;

    }

    foreach (string i in line)

    {

    if(Regex.IsMatch(i, pattern, RegexOptions.IgnoreCase))

    {

    officeType = i;

    officeType = officeType.Replace("OfficeType=","");

    oType = Convert.ToInt32(officeType);

    return 1;

    }

    if (oType == 1)

    {

    Console.WriteLine(oType);

    }

    }

    Thread.Sleep(5000);

    }

    }

    }

    }

    }

     
    Monday, July 7, 2008 5:07 PM

Answers

  •  Your oType is undefined when it reaches the final "if" statement.  The only way for the code to reach the final if statement is when the Regex.IsMatch returns false.  If that happens, then oType is undefined, since it is never defined outside of the if(Regex.IsMatch(i, pattern, RegexOptions.IgnoreCase)) statement.
     
                        foreach (string i in line)  
                        {  
                            if (Regex.IsMatch(i, pattern, RegexOptions.IgnoreCase))  
                            {  
                                officeType = i;  
                                officeType = officeType.Replace("OfficeType=""");  
                                oType = Convert.ToInt32(officeType);  
     
                                //this will cause an exit -  the '1' will cause a compiler error  
                                return;  
                            }  
     
                            //if Regex is not a match, then oType is undefined and  
                            //the following if statement is always false.  
                            if (oType == 1)  
                            {  
                                //Be specific with your output add the ToString call  
                                Console.WriteLine( oType.ToString() );  
                            }  
                        } 

    Talmadge

    • Marked as answer by jack 321 Thursday, July 10, 2008 8:02 AM
    Monday, July 7, 2008 7:37 PM
  • TW,

    The OP is also attempting to return a value in a method declared as "void".  That's why I asked what "Doesn't work" mean.

    Rudedog

    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by jack 321 Thursday, July 10, 2008 8:03 AM
    Monday, July 7, 2008 8:11 PM
    Moderator
  • I understood the OP to mean, "Why isn't the IF statement returning the value", I understood it to be that they were treating the if statement like method, rather than like a conditional branch. 

    • Marked as answer by jack 321 Thursday, July 10, 2008 8:03 AM
    Monday, July 7, 2008 8:57 PM

All replies

  • I don't think you're understanding the language correctly.  An if statement is a conditional statement, the code inside the if is only executed if the condition is met.  Ie;

    bool isRunning = false
     
    ... 
     
    if( isRunning) 
      Console.WriteLine( "here"); 
     else 
     Console.WriteLine( "Not running!); 


    if you use a return statement, the currently executing method will be returned from (if there is a return type, otherwise the compiler will whine at you).  In this case, your program will terminate.
    • Edited by ToBeDecided Monday, July 7, 2008 5:15 PM Added a little more info
    Monday, July 7, 2008 5:13 PM
  • Quarinteen said:

    Here is my code I am trying to get it to return a value for oType. Why can I not return a value from a if statement????

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.IO;

    using Microsoft.Win32;

    using System.Threading;

    using System.Text.RegularExpressions;

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main(string[] args)

    {

    int oType;

    if(File.Exists(@"C:\test.txt))

    {

    using (StreamReader sr = new StreamReader(@"C:\test.txt"))

    {

    string[] line = new string[305];

    string nLine;

    int c = 0;

    string officeType;

    string pattern = "OfficeType=.";

    while ((nLine = sr.ReadLine()) != null)

    {

    line[c] = nLine;

    c++;

    }

    foreach (string i in line)

    {

    if(Regex.IsMatch(i, pattern, RegexOptions.IgnoreCase))

    {

    officeType = i;

    officeType = officeType.Replace("OfficeType=","");

    oType = Convert.ToInt32(officeType);

    return 1;

    }

    if (oType == 1)

    {

    Console.WriteLine(oType);

    }

    }

    Thread.Sleep(5000);

    }

    }

    }

    }

    }

     

    I would suggest not writing long pieces of code in your Main method.  Do you see the problem yet?

    Rudedog




    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, July 7, 2008 6:20 PM
    Moderator
  •  Your oType is undefined when it reaches the final "if" statement.  The only way for the code to reach the final if statement is when the Regex.IsMatch returns false.  If that happens, then oType is undefined, since it is never defined outside of the if(Regex.IsMatch(i, pattern, RegexOptions.IgnoreCase)) statement.
     
                        foreach (string i in line)  
                        {  
                            if (Regex.IsMatch(i, pattern, RegexOptions.IgnoreCase))  
                            {  
                                officeType = i;  
                                officeType = officeType.Replace("OfficeType=""");  
                                oType = Convert.ToInt32(officeType);  
     
                                //this will cause an exit -  the '1' will cause a compiler error  
                                return;  
                            }  
     
                            //if Regex is not a match, then oType is undefined and  
                            //the following if statement is always false.  
                            if (oType == 1)  
                            {  
                                //Be specific with your output add the ToString call  
                                Console.WriteLine( oType.ToString() );  
                            }  
                        } 

    Talmadge

    • Marked as answer by jack 321 Thursday, July 10, 2008 8:02 AM
    Monday, July 7, 2008 7:37 PM
  • TW,

    The OP is also attempting to return a value in a method declared as "void".  That's why I asked what "Doesn't work" mean.

    Rudedog

    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by jack 321 Thursday, July 10, 2008 8:03 AM
    Monday, July 7, 2008 8:11 PM
    Moderator
  • I understood the OP to mean, "Why isn't the IF statement returning the value", I understood it to be that they were treating the if statement like method, rather than like a conditional branch. 

    • Marked as answer by jack 321 Thursday, July 10, 2008 8:03 AM
    Monday, July 7, 2008 8:57 PM