-2

I'm practicing on setters and getters, got this error message:

Cannot assign to 'GetnewName' because it is a'method group'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{

    public class Program
    {
        public static void Main(string[] args)
        {
            Bird b = new Bird();
            b.GetnewName = "Tweety";
            b.Chirp();

            Bird b2 = new Bird();
            b2.GetnewName = "Woody";
            b2.Chirp();
        }
    }
    public class Bird
    {
        private string name;
        private double weight = 30.5d;

        public void SetName(string newName)
        {
            if (newName != null && newName.Length > 2)
            {
                System.Console.WriteLine("Bird already has a name");
                this.name = newName;
            }
            else if (newName.Length < 3)
            {
                System.Console.WriteLine("New name must be longer than two chars");
            }
            else
            {
                name = newName;
            }
        }

        public string GetnewName()
        {
            return this.name;
        }

    public void Chirp()
        {
            System.Console.WriteLine(name + " says chirp!");
        }
    }
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88
waleedd32
  • 473
  • 2
  • 9
  • 23
  • 1
    `GetnewName` is neither a getter nor a setter nor even a property -- it's a method. And you can't assign a value to a method. Read up on the topic of properties; getters and setters should follow naturally. – Jeroen Mostert Sep 12 '18 at 15:53
  • 1
    `GetnewName` is a method, not a property. You can only call functions, you can't assign values to them. – gunr2171 Sep 12 '18 at 15:53
  • 1
    In C#, you don't write methods as setters and getters (unlike Java). You can, but that is a messy way to go about things. Instead, we typically use [properties](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties). – mason Sep 12 '18 at 15:54
  • 3
    Possible duplicate of [Cannot Assign to 'Money' Because It Is a 'Method Group'](https://stackoverflow.com/questions/26164043/cannot-assign-to-money-because-it-is-a-method-group) – gunr2171 Sep 12 '18 at 15:55
  • @gunr2171 i want to have access to 'private string name;' with get and set how to do it from my code – waleedd32 Sep 12 '18 at 15:55

4 Answers4

1

You need to set name using the SetName method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{

    public class Program
    {
        public static void Main(string[] args)
        {
            Bird b = new Bird();
            b.SetName("Tweety");
            b.Chirp();

            Bird b2 = new Bird();
            b2.SetName("Woody");
            b2.Chirp();

            Console.ReadLine();
        }
    }
    public class Bird
    {
        private string name;
        private double weight = 30.5d;

        public void SetName(string newName)
        {
            if (newName != null && newName.Length > 2)
            {
                System.Console.WriteLine("Bird already has a name");
                this.name = newName;
            }
            else if (newName.Length < 3)
            {
                System.Console.WriteLine("New name must be longer than two chars");
            }
            else
            {
                name = newName;
            }
        }

        public string GetnewName()
        {
            return this.name;
        }

        public void Chirp()
        {
            System.Console.WriteLine(name + " says chirp!");
        }
    }
}
psj01
  • 3,075
  • 6
  • 32
  • 63
  • 3
    While that works in some sense, it's not how this code would typically be written in C#. Using explicit (non-property) getters and setters would be considered an anti-pattern in most code bases. – Jeroen Mostert Sep 12 '18 at 15:57
  • I agree with @JeroenMostert's commet above, you should be using properties here.. but with your code as it is now.. the error is caused because you tried to use GetnewName instead of SetName. – psj01 Sep 12 '18 at 15:59
  • @Jeroen Mostert this is what i wanted, is this wrong ? if it is why it is working ? – waleedd32 Sep 12 '18 at 16:15
  • I'm not sure if "wrong" would be the correct term. If it works, it's the right way. -- However, it may not be the best practice as there are more acceptable and sustainable approaches. – Jaskier Sep 12 '18 at 17:26
  • @waleedd32 its not that its wrong... maybe best to say its not best practice. see dasblinkenlight's answer to see how the code can be improved. if you use c# properties you significantly improve the readability of the code for one thing... – psj01 Sep 12 '18 at 17:27
  • this is not actually using setters and getters here ? because when i remove gets and sets from the code it still works like setter and getter, name is private but still accessible when removing set and gets. @psj01 – waleedd32 Sep 15 '18 at 12:06
1

You cannot assign a string to a method, this causes the error. I c# we don't use getters and setters as in Java or C++. Instead we have properties which we use. You can assign and read values as if they where fields but they have special methods called accessors. You should refactor your class like this:

public class Bird
{
    private string name;
    private double weight = 30.5d;

    public string Name
    {
        get => name;
        set
        {
            if (value != null && value.Length > 2)
            {
                Console.WriteLine("Bird already has a name");
                name = value;
            }
            else if (value != null && value.Length < 3)
            {
                Console.WriteLine("New name must be longer than two chars");
            }
            else
            {
                name = value;
            }
        }
    }

    public void Chirp()
    {
        System.Console.WriteLine(name + " says chirp!");
    }
}

Then you can use it as:

var bird = new Bird();
// assign a value (equivalent to SetName method in your original code)
bird.Name = "Woody";
// read a value (equivalent to GetName method in your original code)
Console.WriteLine(bird.Name);
ChristianMurschall
  • 1,621
  • 15
  • 26
1

You are using GetnewName as if it were a property, but you defined it as a pair of methods.

Property syntax combines the getter and the setter under a single name. Then C# re-routs assignments to the setter, and reads to the getter method:

public string Name {
    get => name
    set {
         if (value != null && value.Length > 2) {
             System.Console.WriteLine("Bird already has a name");
             name = value;
         } else if (value.Length < 3) {
             System.Console.WriteLine("New name must be longer than two chars");
         } else {
             name = value;
         }
    }
}

Note: get => name above uses the new syntax. Old syntax for the same was get { return name; }

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
-1

It seems like you want this:

public string Name 
{
    get; private set;
}
public void SetName(string newName)
{
    if (newName != null && newName.Length > 2)
    {
        System.Console.WriteLine("Bird already has a name");
        Name = newName;
    }
    else if (newName.Length < 3)
    {
        System.Console.WriteLine("New name must be longer than two chars");
    }
    else
    {
        Name = newName;
    }
}

The get; will automatically return the value of your property, without the need for a private backing property, and typically you could use the setter inside the property to set the value, but since you need to pass a parameter too it, it seems like making the setter private and creating a separate method to actually set it would suit your needs.

Jacob
  • 628
  • 5
  • 17
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – gunr2171 Sep 12 '18 at 15:57