40

I have a class called Pin.

public class Pin
{
    private string title;

    public Pin() { }

    public setTitle(string title) {
        this.title = title;
    }
    public String getTitle()
    {
        return title;
    }
}

From another class I add Pins objects in a List<Pin> pins and from another I want to iterate the List pins and get the elements. So I have this code.

foreach (Pin obj in ClassListPin.pins)
{
     string t = obj.getTitle;
}

With this code I cannot retrieve the title. Why?

(Note: ClassListPin is just a static class which contains some elements and one of these, is the List<Pin> pins)

gts13
  • 1,048
  • 1
  • 16
  • 29

7 Answers7

74

You need to add parentheses after a method call, else the compiler will think you're talking about the method itself (a delegate type), whereas you're actually talking about the return value of that method.

string t = obj.getTitle();

Extra Non-Essential Information

Also, have a look at properties. That way you could use title as if it were a variable, while, internally, it works like a function. That way you don't have to write the functions getTitle() and setTitle(string value), but you could do it like this:

public string Title // Note: public fields, methods and properties use PascalCasing
{
    get // This replaces your getTitle method
    {
        return _title; // Where _title is a field somewhere
    }
    set // And this replaces your setTitle method
    {
        _title = value; // value behaves like a method parameter
    }
}

Or you could use auto-implemented properties, which would use this by default:

public string Title { get; set; }

And you wouldn't have to create your own backing field (_title), the compiler would create it itself.

Also, you can change access levels for property accessors (getters and setters):

public string Title { get; private set; }

You use properties as if they were fields, i.e.:

this.Title = "Example";
string local = this.Title;
antonijn
  • 5,702
  • 2
  • 26
  • 33
8

getTitle is a function, so you need to put () after it.

string t = obj.getTitle();
Bobson
  • 13,498
  • 5
  • 55
  • 80
8

As @Antonijn stated, you need to execute getTitle method, by adding parentheses:

 string t = obj.getTitle();

But I want to add, that you are doing Java programming in C#. There is concept of properties (pair of get and set methods), which should be used in such cases:

public class Pin
{
    private string _title;

    // you don't need to define empty constructor
    // public Pin() { }

    public string Title 
    {
        get { return _title; }
        set { _title = value; }
    }  
}

And even more, in this case you can ask compiler not only for get and set methods generation, but also for back storage generation, via auto-impelemented property usage:

public class Pin
{
    public string Title { get; set; }
}

And now you don't need to execute method, because properties used like fields:

foreach (Pin obj in ClassListPin.pins)
{
     string t = obj.Title;
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
6

As mentioned you need to use obj.getTile()

But, in this case I think you are looking to use a Property.

public class Pin
{
    private string title;

    public Pin() { }

    public setTitle(string title) {
        this.title = title;
    }

    public String Title
    {
        get { return title; }
    }
}

This will allow you to use

foreach (Pin obj in ClassListPin.pins)
{
     string t = obj.Title;
}
eandersson
  • 25,781
  • 8
  • 89
  • 110
6

You can simplify your class code to this below and it will work as is but if you want to make your example work, add parenthesis at the end : string x = getTitle();

public class Pin
{
   public string Title { get; set;}
}
legrandviking
  • 2,348
  • 1
  • 22
  • 29
4

Because getTitle is not a string, it returns a reference or delegate to a method (if you like), if you don't explicitly call the method.

Call your method this way:

string t= obj.getTitle() ; //obj.getTitle()  says return the title string object

However, this would work:

Func<string> method = obj.getTitle; // this compiles to a delegate and points to the method

string s = method();//call the delegate or using this syntax `method.Invoke();`
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
3

To execute a method you need to add parentheses, even if the method does not take arguments.

So it should be:

string t = obj.getTitle();
Andre Loker
  • 8,368
  • 1
  • 23
  • 36