Class Library Calculator

Moodhi

Member
Joined
Nov 7, 2019
Messages
5
I am new to C# and have access to a simple App that I am struggling to understand and wonder if someone can help please?
The App is a simple Class Library calculator App which is supposed to calculate result from a simple line read from a file.
e.g. the input line is: add 2,multiply 3,apply 3. So the App calculates a result of 15. i.e. (3 + 2) * 3 = 15.
The NUnit test it contains works fine but as I am new to C# I am struggling to understand how the App works.
I would like to design and develop a simple Form containing a textbox where the user can enter the line (as above) i.e. add 2,multiply 3,apply 3.
And when the user clicks on a button called "Calculate" the click event should go away and use the Calculator class (or combination of classes)
and then display the result 15 in a label box on the Form. However, when I try to instantiate the Calculator class I get various error which I
am struggling to fix, therefore request if someone can simply deign this simple form for me and perhaps with some explanation of how the App works?
I have attached the code.
 

Attachments

  • CodeOnlyBritAssignment.zip
    20 KB · Views: 40
when I try to instantiate the Calculator class I get various error which I
am struggling to fix
What errors are you getting? Copy and paste them here. Don't send us a screenshot.

therefore request if someone can simply deign this simple form for me and perhaps with some explanation of how the App works?
We are not a code writing service. We won't do your homework for you. Please post your current form code and we can try to help you move forward. The thing that you attached in your original post looks to be the code that you "have access to"
 
OK, so I have added a new Console App project to the existing Class Library App as follows:

C#:
using System;
using BritAssignment;
using BritAssignment.Common;
using Microsoft.Extensions.Logging;

namespace ConsoleApp1
{
    class Program
    {
        Calculator calculator;
        static void Main(string[] args)
        {
            string script = "add 2,multiply 3,apply 3";

            StringScript Script = new StringScript(script);

            Console.WriteLine(calculator.Process(Script));
        }
    }
}


But this gives me an error -

Error CS0120 An object reference is required for the non-static field, method, or property 'Program.calculator' ConsoleApp1 C:\Work\02_Brit\ConsoleApp1\Program.cs 19 Active
 
Last edited by a moderator:
In the future, please put your code in code tags.

The problem is that you declared an instance variable Calculator calculator; on line 10, but you are trying to use it in a static method (e.g your Main()). Why not simply declare it within the Main()?

Also, before you ask, be sure to use the CalculatorFactory to instantiate an instance of the calculator before you try to use it.
 
Out of curiosity, if you paid good money to BritAssignment.co.uk for them to write the parser and evaluator for you, shouldn't your money have also covered getting some documentation or a tutorial on how to use the code they wrote for you? Or did you steal this code from someone else who paid for it?
 
C#:
var calc = new CalculatorFactory().CreateCalculator();
 
Out of curiosity, if you paid good money to BritAssignment.co.uk for them to write the parser and evaluator for you, shouldn't your money have also covered getting some documentation or a tutorial on how to use the code they wrote for you? Or did you steal this code from someone else who paid for it?

That's very harsh comment, its really sad. I thought this Forum is to get help not accusations. BTW there is no money involved, no stealing, I am simply trying to learn C# so I can get a job.
 
I guess if you got the code for free then you get what you paid for. :)

If you are trying to learn C# it is much better to go through learning the language step by step and build up your skills. The code written in the BritAssignment is something at a very advanced level implementing a lot of concepts that go beyond just C#. Your trying to wrap that code in a simple form is like trying to attach a paper airplane to a modern drone to tackle a flight endurance test. Yes, it is do-able, but if you are only at the level of learning how to fold paper, it is much better to just stick with the paper airplanes first and see how far you can get those to fly.

It is quite possible that the assignment you were given was to really learn how to interface with an existing library. Your teacher maybe well intentioned and trying to get you to experience how a real job will feel like when you have a complicated existing system and you have to somehow bolt something on to it. Unfortunately, it's like being thrown into fast flowing river and being told to sink or swim. You're instructor should have thrown you first into a shallow pond, and then build up to deeper lake, before trying the river.
 
I guess if you got the code for free then you get what you paid for. :)

If you are trying to learn C# it is much better to go through learning the language step by step and build up your skills. The code written in the BritAssignment is something at a very advanced level implementing a lot of concepts that go beyond just C#. Your trying to wrap that code in a simple form is like trying to attach a paper airplane to a modern drone to tackle a flight endurance test. Yes, it is do-able, but if you are only at the level of learning how to fold paper, it is much better to just stick with the paper airplanes first and see how far you can get those to fly.

It is quite possible that the assignment you were given was to really learn how to interface with an existing library. Your teacher maybe well intentioned and trying to get you to experience how a real job will feel like when you have a complicated existing system and you have to somehow bolt something on to it. Unfortunately, it's like being thrown into fast flowing river and being told to sink or swim. You're instructor should have thrown you first into a shallow pond, and then build up to deeper lake, before trying the river.


Thanks got your point.

And I got it working but I now have another problem. I am getting and error - Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
I only have one Console App with "Main" so not sure why I am getting this error?
 
You should be creating new topics for new problems. I suggest opening a new topic with your new problem, providing your code, and the error you receive.

You should note however, that a program can only have one main, and if you have got two, then you need to specify to the compiler which one is the legit one.
 
You should be creating new topics for new problems. I suggest opening a new topic with your new problem, providing your code, and the error you receive.

You should note however, that a program can only have one main, and if you have got two, then you need to specify to the compiler which one is the legit one.


OK, many thanks for your kind advice and help. I created a new solution from scratch, installed all required NuGet packages, created all classes, created new NUnit test project, created a dummy class to test the Unit Test, and finally created a form with combo box containing the input data and a button clicking which instantiates the necessary classes and shows the expected result on the screen.

Once again thank you.
 
Yay! Congratulations!
 
To answer you question here:
And I got it working but I now have another problem. I am getting and error - Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
I only have one Console App with "Main" so not sure why I am getting this error?

It's because you have a Main() in BritAssignment.Tests.Program.cs.
 
Back
Top Bottom