0

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 = "";
        }         


    }
3
Contributors
4
Replies
18
Views
2 Years
Discussion Span
Last Post by lithium112
Featured Replies
  • 2
    ddanbe 2,525   2 Years Ago

    `not all code paths return a value` mean your string function does not yet return a string with the [return statement](http://msdn.microsoft.com/en-us/library/1h3swy84.aspx) On line 7 `x` appears to be a string but is not yet defined as such. Read More

  • 1

    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; } } Read More

2

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.

1

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;
        }
    }
0

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 by ddanbe: typo

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.