Click here to Skip to main content
15,887,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I've been trying to get this to work for 3 days and I'm close to it working, I think! I have tried making it in the console and in GUI of Visual Studio, when I make it in the GUI I am getting a error in visual studio "The class name '?' is not a valid identifier for this language." - I think the compiler is erroring for some reason

When I make it as a console application I get my string array erroring " (Variable name) Does not exist in current context" - I know what this means but I have made the array available globally and also made it it's own class, like so: "public static class Global"
In the console version I also get: "Use of unassigned local variable 'desc1'" I have tried putting my local variables in various places to no adieu.

Here is my console code, I'm trying to get it to work there then convert it to a GUI so I need some help with the GUI error I'm getting. The ""The class name '?' is not a valid identifier for this language." error. I looked it up on the MSDN and they recommended closing and opening it (solution) again which didn't work.

(Console code):
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnotherFlexibleOrder
{
    public static class Global
    {
         public static int[] itemNumber = { 112, 123, 134, 213, 224, 235 };
         public static string[] description = { plate, cup, bowl, vase, planter, statue };
         public static double[] price = { 16.95, 10.95, 14.25, 21.95, 45.99, 89.99 };
         public static bool isValidOption = false;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int number;
            Console.WriteLine("Please enter an item number or description.");
            string item = Console.ReadLine();
            if (int.TryParse(item, out number))
            {
                GetDetails(number);
            }
            else
            {
                GetDetails(item);
            }
        }
        private static void GetDetails(int num)
        {
            string desc1;
            double price1;
            for (int x = 0; x < Global.itemNumber.Length; ++x)
            {

                if (num == Global.itemNumber[x])
                {
                    Global.isValidOption = true;
                    desc1 = Global.description[x];
                    price1 = Global.price[x];
                }
                if (Global.isValidOption)
                {
                    Console.WriteLine("You selected item # {0} which is {1} and costs {2}.", num, desc1, price1);

                }
                else
                {
                    Console.WriteLine("Invalid item #.");
                }
            }
        }
        private static void GetDetails(string word)
        {
            string desc1;
            int item1;
            double price1;
            for (int x = 0; x < Global.description.Length; ++x)

            {

                if (word == Global.description[x])
                {
                    Global.isValidOption = true;
                    desc1 = Global.description[x];
                    price1 = Global.price[x];
                    item1 = Global.itemNumber[x];
                }
                if (Global.isValidOption)
                {
                    Console.WriteLine("You selected {0} which is item # {1} and costs {2}.", desc1, item1, price1);
                }
                else
                {
                    Console.WriteLine("Invalid item1 #!");
                }
            }
        }
    }
}


It's that string in the global class that is giving me the "does not exist in current context" errors: "The name 'plate' does not exist in the current context"

Then my local variables in the GetDetails() methods are giving me this error: "Use of unassigned local variable 'price1'"

This is for my homework, we have to overload the GetDetails method. I was able to get the code working more or less but I just can't figure these errors out I swear I did everything right.

What am I missing? I have tried putting the variables in several places and I debugged to the best of my abilities. There might be other logical errors but I can't get it to compile to debug any further.

What I have tried:

I have tried putting the variables and arrays in different places to get them to be global or local.

Tried restarting solution in visual studio when I get "The class name '?' is not a valid identifier for this language."
Posted
Updated 4-Mar-16 15:33pm
v2

String literals need to be enclosed in quotes - "plate", not plate.

Variables need to be definitely assigned before they can be used. The compiler isn't smart enough to work out that the price and description will always be assigned by the time that isValidOption is set to true. It would be easier to write the success message inside the previous if block instead.

You're continuing to loop over the items even after you've found a valid item. You should break out of the loop as soon as the item is found.

You're writing the Invalid item message too soon - what happens if the user enters "cup", for example?

isValidOption should really be a local variable within the GetDetails methods. You only need it to know whether or not to write the Invalid item message after the loop has finished.

Pseudo-code:
bool isValidItem = false;

For each index in the range:
{
    If the item at this index matches the entered item:
    {
        Write the "found it" message
        Set isValidItem to true
        Break out of the for loop
    }
}

If isValidItem is false:
{
    Write the "invalid item" message
}
 
Share this answer
 
Ok *facepalm* I should have known that about the quotes in the string array. Thank you so much for the reply!

So I tried what you said about my method and it was giving me a "cannot be declared in this scope because it would give a different meaning to 'isValidItem'" error. The error went away when I kept it as isValidOption = true; but I don't think the isValidOption variable was getting changed to true because my output came out wrong. When I went back and changed the line of code to "bool isValidItem = true;" (trying to change it to true) it would spit out that different meaning error.

This is my current code, Like I said I'm not sure how else to change the boolean variable to true, or why this isn't working.

C#
private static void GetDetails(string word)
        {
            bool isValidOption = false;
            string desc1;
            int item1;
            double price1;
            for (int x = 0; x < Global.description.Length; ++x)

            {

                if (word == Global.description[x])
                {

                    desc1 = Global.description[x];
                    price1 = Global.price[x];
                    item1 = Global.itemNumber[x];
                    bool isValidOption = true;
                    Console.WriteLine("You selected {0} which is item # {1} and costs {2}.", desc1, item1, price1);
                    break;
                }
            }
                    if (isValidOption == false)
                    {
                        Console.WriteLine("Invalid item # String");

                    }
        }


This is the current error from the 2 instances where I'm trying to change the isValidOption to true and it's not working:

"Error 1 A local variable named 'isValidOption' cannot be declared in this scope because it would give a different meaning to 'isValidOption', which is already used in a 'parent or current' scope to denote something else"

Maybe I can't change it because it's inside a for and an if statement? I tried putting "public" in front of the boolean declaration too though.

Like I said my output was wrong.

(By wrong output I mean the invalid item # was always showing and sometimes multiple times)

Thanks again!
 
Share this answer
 
v2
Comments
Member 12370098 5-Mar-16 9:06am    
Nevermind, I figured out what I did wrong. I nested the invalid item message when I shouldn't have and then was able to set the bool variable = true after that and the output is correct now. Thanks!
Richard Deeming 7-Mar-16 4:53am    
You're trying to redeclare the isValidOption variable inside the loop, when you should be setting the variable instead.

Remove the bool from the start of the line within the loop:
isValidOption = true;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900