0

I'm starting to learn C# and I'm stuck with a little problem. I don't know if there's a solution for what I want to do or I have to do it in a different way. Anyway, I'm trying to modify the value of a class variable returned by a method. The code below shows more or less what I want to do.

public Class AClass
{
    private SomeClass A;
    private SomeClass B;
    private SomeClass C;
    private SomeClass D;

    private enum SomeEnum {A, B, C, D};

    private void SomeMethod(SomeEnum theEnum, SomeClass theNewValue){
        SomeClass oldValue = GetSomeClass(theEnum);
        oldValue = theNewValue; 
    }

    private SomeClass GetSomeClass (SomeEnum theEnum){
        switch(theEnum){
            case A:
                return A;
            case B:
                return B;
            case C:
                return C;
            case D:
                return D;
        }

        return null;
    }
}

Do you guys have any solution or alternative?

3
  • 1
    It's unclear what you are trying to do. Can you explain what the purpose of doing this is, so we can better understand what you are trying to do and point you to a solution? Commented Jul 12, 2015 at 11:49
  • Anything that is declared private cannot be addressed from the "outside" of this class, e.g. if you create an instance of this class, you can basically do nothing at all with that instance, because there are no public methods or properties to access! Commented Jul 12, 2015 at 11:52
  • What i'm trying to do is: The SomeEnum A,B,C,D are directions, like: UP, DOWN, LEFT, RIGHT. And SomeClass A,B,C,D represents instances of SomeClass there are above, under, and by the sides of AClass. I'm trying to check their position and set their values. The position checking is okay and return a SomeEnum. Commented Jul 12, 2015 at 12:06

3 Answers 3

2

You are almost there. You just need to directly overwrite A, B, C or D:

public Class AClass
{
    private SomeClass A;
    private SomeClass B;
    private SomeClass C;
    private SomeClass D;

    private enum SomeEnum {A, B, C, D};

    public void UpdateInstance (SomeEnum theEnum, SomeClass newClass)
    {
        switch(theEnum)
        {
          case SomeEnum.A:
            A = newClass;
            break;
          case SomeEnum.B:
            B = newClass;
            break;
          case SomeEnum.C:
            C = newClass;
            break;
          case SomeEnum.D:
            D = newClass;
            break;
        }
    }
}

The switch statement is ugly though and can only grow as the complexity of the code does. Better to replace it with a dictionary of functions:

private readonly Dictionary<SomeEnum, Action<SomeClass>> instanceUpdaters =
    new Dictionary<SomeEnum, Action<SomeClass>>
    {
        { SomeEnum.A, x => A = x },
        { SomeEnum.B, x => B = x },
        { SomeEnum.C, x => C = x },
        { SomeEnum.D, x => D = x }
    };

public void UpdateInstance (SomeEnum theEnum, SomeClass newClass)
{
    instanceUpdaters[theEnum](newClass);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Your solution is amazing :) It uses Action and Lambda expression right? I was really wondering when to use them. Thank you so much!
Not sure I agree about refactoring the switch into a dictionary... switch statements are very understood and a staple in almost any language. Abstracting it out into a dictionary doesn't really make it prettier (it's not straight forward to figure out what the dictionary is actually doing until you spend a few moments thinking about it).
@Rob, A search on the topic of switch/case versus polymorphism will throw up no end of articles criticising the former and praising the latter. Yet it's the growing popularity in functional patterns that's led to people moving away from using switch, as a map of functions achieves that polymorphism without all the boilerplate of lots of classes. switch is going the way of goto in my experience.
This isn't really polymorphism, though.. :) so the arguments raised in these articles don't really apply. Haven't seen much debate between switch vs mapping, but I guess in the end it's just a style thing.
@Rob, of course it's polymorphism. It's an example of ad hoc polymorphism. There is way more to polymorphism than subtyping (eg C#'s generics is is parametric polymorphism).
0
public interface ISomeClass
    {
        /// <summary>
        /// eample class method
        /// </summary>
        /// <returns></returns>
        string doSomething();
    }

    public class SomeClassA : ISomeClass
    {
        public string doSomething()
        {
            return "A";
        }
    }

    public class SomeClassB : ISomeClass
    {
        public string doSomething()
        {
            return "B";
        }
    }

    public class SomeClassC : ISomeClass
    {
        public string doSomething()
        {
            return "C";
        }
    }

    public class SomeClassD : ISomeClass
    {
        public string doSomething()
        {
            return "D";
        }
    }

    public class Class1
    {
        private SomeClassA A;
        private SomeClassB B;
        private SomeClassC C;
        private SomeClassD D;

        private enum SomeEnum { A, B, C, D };

        private void SomeMethod(SomeEnum theEnum, ISomeClass theNewValue)
        {
            ISomeClass oldValue = GetSomeClass(theEnum);
            oldValue = theNewValue;
        }

        private ISomeClass GetSomeClass(SomeEnum theEnum)
        {
            switch (theEnum)
            {
                case SomeEnum.A:
                    return A;
                case SomeEnum.B:
                    return B;
                case SomeEnum.C:
                    return C;
                case SomeEnum.D:
                    return D;
            }

            return null;
        }
    }

Also you can use dynamic class creation for not using switch.

Comments

-1

One possible ways is to go with reflection:

private SomeClass GetSomeClass(SomeEnum theEnum)
{
    return (SomeClass)GetType().GetField(theEnum.ToString(), BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this);
}

Comments

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.