Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 22nd, 2007, 07:54 PM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default accesibility.....

OK while I understand this is not a complete program, Im just trying to read an expression from a file and the pop each letter of the expression onto a stack. This code, however, does not compile saying that :


 'prefix.infix.GetLine()': not all code paths return a value

                  AND

: The type or namespace name 's1' could not be found (are you missing a using directive or an assembly reference?)

S1 contains the string when I run it without the stack implementation, and I think that somehow it cannot "see" s1.


ANY HELP PLEASE?
***CODE BELOW****




Code:
public class Class1
    {
        /// The main entry point for the application.

        [STAThread]
        static void Main(string[] args)
        {
            string userChoice;
            //string s1;
            Stack myStack = new Stack();

            GetLine();

            for(int i=0 ; i<s1.Length; i++)
            {
                myStack.Push(s1.Substring(i, 1));
            }

            //Console.WriteLine("Would you like to convert this expression to:  \n1. prefix \n2. postfix \n3. infix");
            //userChoice = Console.ReadLine();    
        }

        public string GetLine()
        {
            string s1;
            try 
            {
                // Create an instance of StreamReader to read from a file
                using (StreamReader sr = new StreamReader("expression test.txt")) 
                {
                    if(sr.Peek() != -1)
                    {
                        s1 = sr.ReadLine();
                        return s1;
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }

    }
Reply With Quote
  #2 (permalink)  
Old October 22nd, 2007, 08:57 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

As Holems said to Watson "It's elementary". ;]

Ok. The reason that you are getting your first error (not all code paths return a value) is because all code paths don't return a value. Simply put, all functions MUST return a value even if it is: return null; but, move so, your code must return a value through all possible branches of your code.

So in this code
public string GetLine()
        {
            string s1;
            try
            {
                // Create an instance of StreamReader to read from a file
                using (StreamReader sr = new StreamReader("expression test.txt"))
                {
                    if(sr.Peek() != -1)
                    {
                        s1 = sr.ReadLine();
                        return s1;
                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
you have a conditional statement (your if) that may or may not execute but, consider that the if doesn't execute and there is no error, code execution will continue to the end of your function and never find a return value. Moveover, if your if DOES execute but an execption is thrown inside the if, the return value will be missed, code execution will pass to your catch and, again, will flow to the end of the function without having a value returned.

So you will want to modifiy your code to be something like:
public string GetLine()
        {
            try
            {
                // Create an instance of StreamReader to read from a file
                using (StreamReader sr = new StreamReader("expression test.txt"))
                {
                    if(sr.Peek() != -1)
                    {
                        return sr.ReadLine();
                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                //Console.WriteLine("The file could not be read:");
                //Console.WriteLine(e.Message);
                return e.Message.ToString();
            }
         return null;
        }

if you seriously want to write an exception out to the console you might want to do so as an out parameter such as:

...
int ErrorCode;
string foo = GetLine(out ErrorCode);

if(ErrorCode == 1){Console.WriteLine(foo);}
else if(ErrorCode == -1)(Console.WriteLine("No Data Returned!");}
...

public string GetLine(out int ErrorCode)
        {
            ErrorCode = 0;
            try
            {
                // Create an instance of StreamReader to read from a file
                using (StreamReader sr = new StreamReader("expression test.txt"))
                {
                    if(sr.Peek() != -1)
                    {
                        return sr.ReadLine();
                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                //Console.WriteLine("The file could not be read:");
                //Console.WriteLine(e.Message);
                ErrorCode = 1;
                return e.Message.ToString();
            }
         ErrorCode = -1;
         return null;
        }

so ya, you should be good to go!

hth

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
Reply With Quote
  #3 (permalink)  
Old October 22nd, 2007, 11:04 PM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

the problem with that is that I need to keep the readline from the streamwriter in a variable so that I can use it later. Essentially what I am doing is the shunting yard thing. Gotta read an expression from a file (( 3+2)*4 for example) and convert it to prefix and postfix notations. Also If you have any good links that might help me in that aspect, It would be appreciated.
Reply With Quote
  #4 (permalink)  
Old October 23rd, 2007, 07:23 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Why are you not utilizing the return value from the method?

You should have something like this:

            string s1;
            Stack myStack = new Stack();

            s1 = GetLine();

-Peter
Reply With Quote
  #5 (permalink)  
Old October 23rd, 2007, 11:49 PM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

Point taken, by both of you. I think that planoies recommendation will fix the problem while allowing me to do what I want with the returned string. However I still cannot get the GetLine method to compile. I've changed the method to ensure (as far as I know) that a value is returned either way, yet I am getting the same message. I am getting another error stating that I am missing an object reference for the nonstatic field, property or method. Maybe that helps? I think that if I can finally get to where I can start storing my chars onto a stack I will be ok.

Code:
public class Class1
    {
        /// The main entry point for the application.

        [STAThread]
        static void Main(string[] args)
        {
            string userChoice;
            //string s1;
            Stack myStack = new Stack();

            string s1 = GetLine();

            for(int i=0 ; i<s1.Length; i++)
            {
                myStack.Push(s1.Substring(i, 1));
            }

            //Console.WriteLine("Would you like to convert this expression to:  \n1. prefix \n2. postfix \n3. infix");
            //userChoice = Console.ReadLine();    
        }

        public string GetLine()
        {
            try 
            {
                // Create an instance of StreamReader to read from a file
                using (StreamReader sr = new StreamReader("expression test.txt")) 
                {
                    if(sr.Peek() != -1)
                    {
                        return sr.ReadLine();
                    }
                    else
                    {    
                        Console.WriteLine("End of File.");
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                //Console.WriteLine("The file could not be read:");
                //Console.WriteLine(e.Message);
                return e.Message.ToString();
            }
        }

    }
Reply With Quote
  #6 (permalink)  
Old October 24th, 2007, 12:00 AM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

Ok, sorry about the double post. I've got the returning a value thing fixed. However I dont understand the issue with:

: An object reference is required for the nonstatic field, method, or property 'Prog1.Class1.GetLine()'

Seymour.
Reply With Quote
  #7 (permalink)  
Old October 24th, 2007, 12:08 AM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

.........I apologize. I fixed it just moments later.

making my method static makes it compile and I have checked to see that my stack is operating properly and all that.

Why do I need this to be static to compile? If I didn't want it to be static what might I have done?
Sorry, Im a relatively new programmer(if I can even be deemed such)

Seymour
Reply With Quote
  #8 (permalink)  
Old October 24th, 2007, 02:43 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

static means shared amongst all classes so you can call using
Code:
Class1.GetLine();
. Non-static means you need an instance of the class, so
Code:
Class1 myInstance = new Class1();
myInstance.GetLine();
You need to read some basic C# tutorials.


--

Joe (Microsoft MVP - XML)
Reply With Quote
  #9 (permalink)  
Old October 24th, 2007, 10:34 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Seymour,

A console app has a static main method as required by the framework to serve as the entry point to the application.

Often when creating a console app this technique will be used:
Code:
public class Class1
    {
        /// The main entry point for the application.

        [STAThread]
        static void Main(string[] args)
        {
            new Class1().Run(args);
        }

        public void Run(string[] args)
        {
            //do actual program logic here
        }
    }
This way you have an instantiated class in which to actually run your program. You then don't need to make anything static to be functional.

-Peter
Reply With Quote
  #10 (permalink)  
Old October 25th, 2007, 12:14 AM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

Ok, my current problem is that in my switch statement I have having trouble with "implicitly trying to convert one type to another". at first I made a char that was equal to myStack.Pop to switch off. (Converting char to str). Then I just used to pop statement to switch off, unsure if that would work either. Can I get a suggestion? I do not understand why the value popped off the stack is assumed to be a string? A char would seem more likely.

anyways, the error that using the pop statement that i get is

: A value of an integral type expected

Also, this is part of a program designed to convert infix to prefix or postfix notation and vice versa... maybe that will provide a little insight as to my intentions.......
Help please?


Code:
while(myStack.Count != 0)
            {
                switch(myStack.Pop())
                {
                    case "+":

                        if(operatorStack.Peek() == "*")
                        {
                            //lowerPrecedence = true;
                            strPostfix += operatorStack.Pop();
                        }
                        else if(operatorStack.Peek() == "/")
                        {
                            strPostfix += operatorStack.Pop();
                        }
                        else
                            operatorStack.Push(myStack.Pop());
                        break;

                    case "-":
                        if(operatorStack.Peek() == "*")
                        {
                            //lowerPrecedence = true;
                            strPostfix += operatorStack.Pop();
                        }
                        else if(operatorStack.Peek() == "/")
                        {
                            strPostfix += operatorStack.Pop();
                        }
                        else
                            operatorStack.Push(myStack.Pop());
                        break;

                    case "/":
                        //code
                        break;

                    case "*":
                        //code
                        break;

                    case "1":
                        strPostfix += myStack.Pop();
                        break;

                    case "2":
                        strPostfix += myStack.Pop();
                        break;

                    case "3":
                        strPostfix += myStack.Pop();
                        break;

                    case "4":
                        strPostfix += myStack.Pop();
                        break;

                    case "5":
                        strPostfix += myStack.Pop();
                        break;

                    case "6":
                        strPostfix += myStack.Pop();
                        break;

                    case "7":
                        strPostfix += myStack.Pop();
                        break;

                    case "8":
                        strPostfix += myStack.Pop();
                        break;

                    case "9":
                        strPostfix += myStack.Pop();
                        break;

                    case "0":
                        strPostfix += myStack.Pop();
                        break;

                }
            }
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 12:57 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.