Member Avatar for lithium112

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;
        }
    }
Member Avatar for lithium112

Thanks guys! That makes sense. Much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.