1

Obligatory I'm new to C# and am working on a large project for work and have run into a wall google can't seem to help me solve.

I understand that case's need to be constant and I am using a variable to declare a case but I can't seem to find the right way to correct the issue.

I am using the code to determine whether or not to send an order message to a market.

The defined variable

private string[] m_oKeys = new string[1];

The switch with the issue

        public void m_ts_OrderAdded(object sender, OrderAddedEventArgs e)
    {
        string key = e.Order.SiteOrderKey;
        switch (key)
        {
            case m_oKeys[1]:
                m_oObject = new e.Order();
                m_oKeys = new e.Order.SiteOrderKey();
                m_InFlight = new (false);
                break;
            case m_oKeys[]:
                m_oObject[] = e.Order;
                m_oKeys(1) = e.Order.SiteOrderKey;
                m_InFlight(1) = false;
                break;
            default:
                break;
                //handle order not found...
        }
    }
2
  • 6
    What is case m_oKeys[] intended to match given there is no index specified ...? Commented Jan 23, 2017 at 16:50
  • 7
    Well, as you've already pointed out, the literals used in the case statements need to be constants. As such the only syntax you're allowed to use are constant literals, such as case "key1":. You can't use a variable here. Instead write out the switch-statement as a series of if-statements. Also, what is this supposed to match? case m_oKeys[]: <-- no index Commented Jan 23, 2017 at 16:51

1 Answer 1

1

You've defined the array to contain a single string.

private string[] m_oKeys = new string[1];

So you can only reference the first element m_oKeys[0]. As others have said, you must use constants for the case label. Maybe if-else will work better for you.

public void m_ts_OrderAdded(object sender, OrderAddedEventArgs e)
    {
        string key = e.Order.SiteOrderKey;
        if (key == m_oKeys[0])
        {
            // Do something
        }
        else
        {
            // Do something else
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I think I may have tried to get overly complex using the switch logic. Using if and else if statements is a much better idea. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.