Cannot implicitly convert type 'Address' to 'String' in setter

user2344333

As in the title I try to make setter to Adress but I got this convert error . Its code of my Worker class .

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

    namespace MAS_MP1
    {
        class Worker
        {
            string name, surname;
            protected string adress;
            int phoneNumber;
            string birthDate;

            Worker(String name, String surname, String adress, int phoneNumber, string birthDate)
            {
                this.name = name;
                this.surname = surname;
                this.adress = adress;
                this.phoneNumber = phoneNumber;
                this.birthDate = birthDate;

            }

           static List<Worker> list = new List<Worker>();

            public  static void add(Worker p)
            {
                list.Add(p);
            }
            public void setA(Adress a)
            {
                this.adress = a;
            }
            public static  void showExtension()


 {
            Console.WriteLine("Class Extesion");
            foreach (Worker p in list)
            {
                Console.WriteLine(p);
            }
            Console.ReadLine();

        }
        override
        public string ToString()
        {
            return "Name " + name + " Surname " + surname;
        }

        public static void Main(string[] args)
        {
            Worker p = new Worker("Tom,","Smith","NewStreet 8 London",123456,"12-12-2012");
            Adress a = new Adress("NewStreet" , 9 , "London");
            add(p);
            showExtension();


        }
    }
}

And error was here :

public void setA(Adress a)
        {
            this.adress = a;
        }

I try to make a complex attribute so j make second class Adress only with constructor and toString override method. Adress got street , homeNumber and cityName attributes.

Grant Winney

How could this possibly work? You're trying to store an instance of Adress in a string...

protected string adress;

public void setA(Adress a)
{
    this.adress = a;
}

There's a couple fixes.

You could change the type of the field you're storing the value in:

protected Adress adress;

and change the constructor to accept an Adress as well:

Worker(String name, String surname, Adress adress, int phoneNumber, string birthDate)

Alternatively, modify your setter so you're storing a string:

public void setA(Adress a)
{
    this.adress = a.ToString();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot implicitly convert type 'string' to 'String'

From Dev

Cannot implicitly convert type 'string' to 'decimal'

From Dev

cannot implicitly convert type string to char

From Dev

Cannot implicitly convert type delegate to string

From Dev

Unity cannot implicitly convert type "string" to "bool"

From Dev

Cannot implicitly convert type 'string' to 'bool' [If statement]

From Dev

Cannot implicitly convert type Models to string issues

From Dev

Cannot implicitly convert type 'string' to 'integer'

From Dev

Enum cannot implicitly convert type string to enum

From Dev

Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

From Dev

Error cannot implicitly convert type 'void' to 'string' on string filepath

From Dev

Cannot implicitly convert type string to string[] in c# soap call

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert to type string to System.Net.Mail.MailAddress

From Dev

Error Cannot implicitly convert type 'string' to 'int' - for the iterator in a ForEach loop

From Dev

cannot implicitly convert type 'int' to 'string' c#

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert type string to int in C#

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

webservice won't build - "Cannot implicitly convert type 'string' to 'int'"

From Dev

Cannot implicitly convert type 'int' to 'string' on 2 intergers

From Dev

Cannot implicitly convert type void with no input params to string c#

From Dev

Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

From Dev

Error message "Cannot implicitly convert type 'String' to 'Int'"

From Dev

Cannot implicitly convert type string to System.Drawing.Color

From Dev

Cannot implicitly convert type 'string' to 'int' for comparing 2 variables

From Dev

Error 2 Cannot implicitly convert type 'int' to 'string' c#

From Dev

Acumatica conditional update - Cannot implicitly convert type 'string' to 'bool'

Related Related

  1. 1

    Cannot implicitly convert type 'string' to 'String'

  2. 2

    Cannot implicitly convert type 'string' to 'decimal'

  3. 3

    cannot implicitly convert type string to char

  4. 4

    Cannot implicitly convert type delegate to string

  5. 5

    Unity cannot implicitly convert type "string" to "bool"

  6. 6

    Cannot implicitly convert type 'string' to 'bool' [If statement]

  7. 7

    Cannot implicitly convert type Models to string issues

  8. 8

    Cannot implicitly convert type 'string' to 'integer'

  9. 9

    Enum cannot implicitly convert type string to enum

  10. 10

    Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

  11. 11

    Error cannot implicitly convert type 'void' to 'string' on string filepath

  12. 12

    Cannot implicitly convert type string to string[] in c# soap call

  13. 13

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  14. 14

    Cannot implicitly convert to type string to System.Net.Mail.MailAddress

  15. 15

    Error Cannot implicitly convert type 'string' to 'int' - for the iterator in a ForEach loop

  16. 16

    cannot implicitly convert type 'int' to 'string' c#

  17. 17

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  18. 18

    Cannot implicitly convert type string to int in C#

  19. 19

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  20. 20

    webservice won't build - "Cannot implicitly convert type 'string' to 'int'"

  21. 21

    Cannot implicitly convert type 'int' to 'string' on 2 intergers

  22. 22

    Cannot implicitly convert type void with no input params to string c#

  23. 23

    Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

  24. 24

    Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

  25. 25

    Error message "Cannot implicitly convert type 'String' to 'Int'"

  26. 26

    Cannot implicitly convert type string to System.Drawing.Color

  27. 27

    Cannot implicitly convert type 'string' to 'int' for comparing 2 variables

  28. 28

    Error 2 Cannot implicitly convert type 'int' to 'string' c#

  29. 29

    Acumatica conditional update - Cannot implicitly convert type 'string' to 'bool'

HotTag

Archive