简体   繁体   中英

In C# => Executing Class Library in Console Application

I wish to create a menu-driven program which will be classified on the basis of some aspects (say operators or any other thing).

I have created a console application project and given "sample" as the namespace. In the sample, I have created a menu in the Main()

public static void Main()
    {
        Console.Clear();
        Console.WriteLine("\nMenu Driven Program");
        Console.WriteLine("\n<--------List of Available Options-------->");
//Creating a list to Select from
        Console.WriteLine("\n  Select Any of the Following Function :");
        Console.WriteLine("\n_________________________________________");
        Console.WriteLine("\n1. Arithmatic Operators\n2. Logical Operators");
        Console.WriteLine("\n3.Misc1\n4. Misc2");
//Accepting a normal string
        string b = Console.ReadLine();
        int d;
// Following converts string into an integer
        Int32.TryParse(b, out d);

//Following code is used to create menu-driven program
        switch (d)
        {
            case 1:
//Here I wish to call one class library 
                break;

            case 2:
//Here I wish to call another class library
                break;
//And so on.    

            default:
                Console.WriteLine("\n\nWhat you wish to do?\na. Continue the Program\n\nb. Exit the Program");
                char c = Convert.ToChar(Console.ReadLine());
                if(c=='a'||c=='A')
                {
                    Main();
                }
                else
                {
                    Exit();
                }                  
                break;
        }
    }

Now, I created a class library in which I will store three classes (let's just consider) increment, division, and multiplication.

But, I am unable to call that class library. When I directly input the name of class library in Switch "Case" it gives me CS0118 error stating that "Sample" is just a namespace but used like variable.

In my class library, I have created three different class and again a Main() which will show the list of available operations. Eg.If arithmetic operators, then the Main in class library will consist Addition, Subtraction, Multiplication, etc.

You need to add a reference to the Class library project. I'm assuming you created them in the same solution. Make sure both are busing for the same. Net version say 4.6. To add a reference, expand the project explorer, look for references, right click, and choose Add, then find the class library and check the checkbox. If you have the productivity tool Resharper installed, it makes fixing these sorts of things much faster.

CS0118 error stating that "Sample" is just a namespace but used like variable

Error CS0118 occurs when you try to create an instance of a namespace, or otherwise use the namespace as though it's an instance of a class. See this documentation post for more information.

From you description my assumption is that you need to call the main in each of your 3 classes depending on what the user selected. Consider the following code...

switch(d)
{
    case 1:
        Sample.Class1.Main();
        break;
    case 2:
        Sample.Class2.Main();
        break;
    case 3:
        Sample.Class3.Main();
        break;
    default:
        // default processing here
}

This is assuming your namespace is called Sample and the methods are defined as static in the classes in your library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM