Hello,

I am delving into OOP now and am having issues with creating properties. Below is my code. Basically what I did is created a custom class and am creating a property within that class. But what happens is it will put a red squiggly under 'Multiply' saying 'not all code paths return a value'. I've tried looking up this but not really finding the answer anywhere. I did try following an example but it still won't work. Not sure what I'm doing wrong. I appreciate any help.

 public class Arithmetic
    {
        private string x { get; set; }

        public string Multiply()
        {
            x = "";
        }         


    }

not all code paths return a value mean your string function does not yet return a string with the return statement On line 7 x appears to be a string but is not yet defined as such.

Yup, what ddanbe said. The simplest fix is to return x.

    public class Arithmetic
    {
        private string x { get; set; }
        public string Multiply()
        {
            x = "";
            return x;
        }
    }

I did not notice, but the property on line 3 is in fact used as a field. Why then not do so?

public class Arithmetic
    {
        private string x = string.Empty;

        public string Multiply()
        {
            x = "";
            return x;
        }
    }

Edited 2 Years Ago by ddanbe: typo

This question has already been answered. Start a new discussion instead.