locked
C# - I need to assign the property "name" of the objects in "myPersons" list to the objects of "myBooks" list RRS feed

  • Question

    1. class Book {public string Titel;}
    2. class Novel : Book { }
    3. class ComicBook : Book { }
    4. list <Book> myBooks = new list<Book>(){
    5. new Novel {Titel="HP 1"},
    6. new Novel {Titel="HP2"},
    7. new Novel {Titel="HP3"},
    8. new ComicBook {Titel="Naruto"},
    9. new ComicBook {Titel="Naruto2"}
    10. }
    11. class Person {public string Name;}
    12. class Author : Person { }
    13. class Artist : Person { }
    14. List<Person> myPersons = new list(){
    15. new Author {Name = JK.Rawling}
    16. new Artist {Name = Masashi Kishimoto} }

    I need to assign the property "Name = JK.Rawling" to some objects of "myBooks" list and "Name = Masashi Kishimoto" to other objects of "myBooks" list in order to have:

    obj1 Novel {Titel = "HP1", Author.Name = JK.Rawling}
    obj2 Novel {Titel = "HP2", Author.Name = JK.Rawling}
    obj3 Novel {Titel = "HP3", Author.Name = JK.Rawling}
    obj4 ComicBook {Titel = "Naruto", Artist.Name = Masashi Kishimoto}
    obj5 ComicBook {Titel = "Naruto2", Artist.Name = Masashi Kishimoto}

    Monday, October 26, 2020 2:33 PM

All replies

  • Please edit your post and place code into code blocks and when doing so please format your code.

    Here is where code block button is


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 26, 2020 2:39 PM
  • In order for that to work, your Novel class would need a field called Author, and your ComicBook class would need a field called Artist.  You can't add new fields to an existing class.

    If you want to add arbitrary attributes, perhaps you should look at a Dictionary instead.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, October 27, 2020 4:27 AM
  • Hi Gab,

    There are some irrationals in the code.

    1. Public fields.

    We should not use public fields, but should use attributes to encapsulate them.

    public class Book
    {
        private string _title;
    
        public string Title
        {
              get{ return _title;  }
              set{ _title = value; }
        }
    }
    
    or 
    
    public class Book
    {
        public string Title {get; set;} 
    }


    2.  According to your final desired result, the Novel class should have an Author property, and the ComicBook class should have an Artist property.

        class Novel : Book
        {
            public Author Author { get; set; }
        }
    
        class ComicBook :  Book
        {
            public Artist Artist { get; set; }
        }
    

    3. Basic code format.

    "new list" should be "new List", and the names of elements in myPersons should be enclosed in double quotes.

    After completing these three steps, we can try to get the final result:

                foreach (var book in myBooks)
                {
                    if (book.GetType()==typeof(Novel))
                    {
                        ((Novel)book).Author = (Author)myPersons[0] ;
                    }
                    else
                    {
                        ((ComicBook)book).Artist = (Artist)myPersons[1];
                    }
                }

    The elements in myBooks and myPersons have no connection, so I am afraid we can only manually specify the value of the property in this way.

    Therefore, this code is only applicable to the current data.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 27, 2020 6:17 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 11, 2020 6:52 AM