Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 31st, 2007, 02:46 PM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default implicit conversion

Ok, my current problem is that in my switch statement(below) I am having trouble with "implicitly trying to convert one type to another". At first I made a initiated a char that I used like

Code:
char testChar = myStack.Pop();

switch(testChar)
{
   /////
   code
   /////
}
 (From which I got an error about implicitly Converting char to str).
Additionally, I tried to just make the pop statement equal to a string value.

After that I just simply switched off of the value popped of the stack. (As seen in the code below). This time the error that was

: 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?

P.S. I am sure some of you have probably seen this before, I just cleaned it up a bit and made a thread exclusive to this particular problem...



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;

                }
            }
 
Old November 1st, 2007, 04:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

"1" is a string, whereas '1' is a character. You cannot implicitly convert a string to a char.

switch(testChar)
{
   case '1':
    // Do something
     break;
}

/- Sam Judson : Wrox Technical Editor -/
 
Old November 1st, 2007, 09:37 PM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

Actually, I fixed that before I posted the code. I copied this code out of my old thread. My bad. Anyways, I think I've figured out the differences in variable type, but the "if(operatorStack.Peek() = '*')" condition is throwing an error that states that
: The left-hand side of an assignment must be a variable, property or indexer. Any ideas on how to test this such that I can get away from these errors?

Also, what does it take to get an answer? I have done the work myself and made my problems as clear as possible, yet I'm not getting responses on this or other forums. I have been doing this project for nearly 2 weeks with only about 3 total responses. Would someone direct me to a beginners forum if that is the problem?? Or simply a forum where people will help me with this?
 
Old November 2nd, 2007, 03:58 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Should that not be "==" instead of "="?

If that was just a typo then let me know.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 3rd, 2007, 01:44 AM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

No, I tried it both ways.

If I use a single '=' i get the ": The left-hand side of an assignment must be a variable, property or indexer." error.

If I use '==' I get an error stating ": Operator '==' cannot be applied to operands of type 'object' and 'char'"

Any suggestions on a smarter way to word that? Or perhaps a better conditional?
 
Old November 3rd, 2007, 05:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

if('*' == (char)stack.Peek())

/- Sam Judson : Wrox Technical Editor -/
 
Old November 4th, 2007, 03:43 AM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

I had'nt thought of reversing it. Someone pointed out to me that I should probably convert my value popped off the stack to a string

i.e.
Code:
object nextItem = operatorStack.Peek();
if(nextItem.ToString() == "*" )
    {
        ///code
    }


It blew my mind that I never thought about that.

Seymour (AKA Simon)
 
Old November 4th, 2007, 05:42 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Either way you are looking at boxing the char into an object and then back out again. If possible (i.e. if you're using .Net 2.0) I'd use Stack<char>.



/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion... ankur_icfai Flash (all versions) 1 November 9th, 2006 10:38 AM
Conversion anukagni Access 2 September 6th, 2006 12:22 AM
ADO Conversion knowledge76 Access VBA 1 March 20th, 2005 07:35 AM
Error, Strict On disallows implicit conversions f vbGaz VB.NET 2002/2003 Basics 0 January 3rd, 2005 04:27 AM
conversion gbilios C++ Programming 6 August 6th, 2004 06:13 PM





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