CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

Rare MyClass case converts incorrectly

Open GrahamTheCoder opened this issue 3 years ago • 0 comments

Accessing backing field of the autogenerated property where MyClass is involved:

Class Foo 
    Overridable Property Prop As Integer = 5

    Sub Test()
        _Prop = 10 ' This should convert to MyClassProp = 10 not to Prop = 10
        Dim isCorrect = MyClass.Prop = 10 ' After conversion this will return 5instead of 10 because we wrote to Child.Prop
    End Sub
End Class
Class Child
    Inherits Foo
    Overrides Property Prop As Integer = 20
End Class
    public class Foo
    {
        public int MyClassProp { get; set; } = 5;

        public virtual int Prop
        {
            get => MyClassProp;

            set => MyClassProp = value;
        }

        public void Test()
        {
            Prop = 10; // This should convert to MyClassProp = 10 not to Prop = 10
            bool isCorrect = MyClassProp == 10; // After conversion this will return 5 instead of 10 because we wrote to Child.Prop
        }
    }

    public class Child : Foo
    {
        public override int Prop { get; set; } = 20;
    }

Originally posted by @Yozer in https://github.com/icsharpcode/CodeConverter/issues/822#issuecomment-1030053015

GrahamTheCoder avatar Feb 06 '22 09:02 GrahamTheCoder