-3

I want to use this method in an non-static class:

public class LogicalExpressionCalculator : MeasurementInterpreter
{
    private static Boolean Operator(this string logic, string x, string y)
    {
        switch (logic)
        {
            case "==": return x == y;
            case "!=": return x != y;
            case ">": return Int32.Parse(x) > Int32.Parse(y);
            case ">=": return Int32.Parse(x) >= Int32.Parse(y);
            case "<": return Int32.Parse(x) < Int32.Parse(y);
            case "<=": return Int32.Parse(x) <= Int32.Parse(y);
            case "&&": return bool.Parse(x) && bool.Parse(y);
            case "||": return bool.Parse(x) || bool.Parse(y);
            default: throw new Exception("invalid logic");
        }
    }
 }

How can I change this method to works in a non static class?

The method i want to used inside the class in this method:

protected void CalculateExpression()
{
    Stack variableStack = new Stack();
    Stack operatorStack = new Stack();
    foreach (string field in contextString)
    {
        switch (field)
        {
            case "(":
            case "<":
            case ">":
            case "==":
                operatorStack.Push(field);
                break;
            case ")":
                string operatorPop = null;
                while (operatorPop == "(")
                {

                    operatorPop = operatorStack.Pop().ToString();
                    resultStack = Operator(operatorPop, variableStack.Pop().ToString(), variableStack.Pop().ToString());
                    variableStack.Push(resultStack);
                }
                break;

            case "UserInput":
            case "EngineRpm":
            case "VehicleSpeed":
                variableStack.Push((string)hashtable[field]);
                break;

            default:
                int integer;
                if(Int32.TryParse(field, out integer)) variableStack.Push(integer);
                if (bool.Parse(field)) variableStack.Push(field);
                break;

        }
    }

    Result = resultStack;
    if (Result)
    {

    }
}

i get an error

Extension method must be static

M. Schena
  • 2,039
  • 1
  • 21
  • 29
  • Why do you need to change this method at all? It uses only variables passed as arguments so it is totally independent from class containing it. What is preventing you from using it "as is"? – Andrey Korneyev Feb 04 '16 at 08:47
  • Static members can be accessed from instances ones. Visibility rules are the same (I mention this, because your sample contains `private` method). What *exactly* do you want to do, and what is going wrong? – Dennis Feb 04 '16 at 08:47
  • i can not build the project because a n error 'Extension method must be static' – Adrian Moldovan Feb 04 '16 at 08:55
  • "just call it". Seriously. ClassName.MethodName(). Though - tell me how you can call a PRIVATE method from another class...? – TomTom Feb 04 '16 at 09:05
  • 1
    "Extension method must be static" - http://stackoverflow.com/questions/6096299/extension-methods-must-be-defined-in-a-non-generic-static-class ? See accepted answer. – Dennis Feb 04 '16 at 09:09
  • In this way you are using Operator, remote the `this` on `this string logic` and it works – M. Schena Feb 04 '16 at 09:15

1 Answers1

1

Remove this doesn't seem to be necessary.

private static Boolean Operator(string logic, string x, string y)
{
    switch (logic)
    {
        case "==": return x == y;
        case "!=": return x != y;
        case ">": return Int32.Parse(x) > Int32.Parse(y);
        case ">=": return Int32.Parse(x) >= Int32.Parse(y);
        case "<": return Int32.Parse(x) < Int32.Parse(y);
        case "<=": return Int32.Parse(x) <= Int32.Parse(y);
        case "&&": return bool.Parse(x) && bool.Parse(y);
        case "||": return bool.Parse(x) || bool.Parse(y);
        default: throw new Exception("invalid logic");
    }
}
M. Schena
  • 2,039
  • 1
  • 21
  • 29
  • it is visible inside the class because i used inside the class and i can call it but the error i get is Extension method must be static. – Adrian Moldovan Feb 04 '16 at 09:04
  • This is because an extension method must always be defined in a static class – Bidou Feb 04 '16 at 09:06
  • It is an exetnsion method because the first parameter is prefixed with this. Which means you call it on a string - "....".Operator("==", "xxx") - C# basics.... May I suggest learning the language. Not from howtos but by reading the syntax? – TomTom Feb 04 '16 at 09:07