Dictionary<> i obiekty

0

Z tego co się orientuję, to w Dictionary<>, można zainicjalizować na zasadzie <object1, object2>. Czy ktoś się orientuje jak można dodać w takiej konstrukcji object1, a portem przyporządkować object2. Coś na zasadzie:

object1. nazwisko
    object2.title1
    object2.title2
    object3.title3

Co bym nie wymyślił to się sypie.

0

O takie coś ci chodzi ?
:

using System;
using System.Collections.Generic;


namespace ConsoleApp80
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> slownik = new Dictionary<string, string>() { ["a"] = "b", ["c"] = "d"};

            Console.WriteLine(slownik["a"]);
            Console.WriteLine(slownik["c"]);
        }
    }
}

3

https://stackoverflow.com/questions/634826/using-the-field-of-an-object-as-a-generic-dictionary-key

tl;dr

musisz przeciązyć

public override int GetHashCode();

public override bool Equals(object obj);
    
public bool Equals(TwojTyp obj);
1

Jeśli masz element o tym samym kluczu w Dictionary to się wysypie.

    class Obj1
    {
        public string lastname { get; set; }
    }

    class Obj2
    {
        public string title1 { get; set; }
        public string title2 { get; set; }
        public string title3 { get; set; }
    }
Dictionary<Obj1, Obj2> dic = new Dictionary<Obj1, Obj2>();

Obj1 obj = new Obj1();
obj.lastname = "Kowalski";
Obj1 obj1 = new Obj1();
obj.lastname = "Kowalski";

Obj2 obj2 = new Obj2();
obj2.title1 = "tytuł";
obj2.title2 = "tytuł2";
obj2.title3 = "tytuł3";

dic.Add(obj, obj2);
dic.Add(obj1, obj2);
dic.Add(obj, obj2); //TUTAJ RZUCI EXCEPTION

Jest możliwe, że dodajesz ten sam obiekt do słownika?

0
AdamWox napisał(a):

Jeśli masz element o tym samym kluczu w Dictionary to się wysypie.

    class Obj1
    {
        public string lastname { get; set; }
    }

    class Obj2
    {
        public string title1 { get; set; }
        public string title2 { get; set; }
        public string title3 { get; set; }
    }
Dictionary<Obj1, Obj2> dic = new Dictionary<Obj1, Obj2>();

Obj1 obj = new Obj1();
obj.lastname = "Kowalski";
Obj1 obj1 = new Obj1();
obj.lastname = "Kowalski";

Obj2 obj2 = new Obj2();
obj2.title1 = "tytuł";
obj2.title2 = "tytuł2";
obj2.title3 = "tytuł3";

dic.Add(obj, obj2);
dic.Add(obj1, obj2);
dic.Add(obj, obj2); //TUTAJ RZUCI EXCEPTION

Jest możliwe, że dodajesz ten sam obiekt do słownika?
Masz błędy w przykładzie, chyba powinno być: obj1.lastname = "Kowalski" ;

1

Dictionary<Obj1, List<Obj2>>

Dictionary<string, List<string>>

A tak?

0

Potrzebuję dokładnie dodając nowego użytkownika pobrać jego id i przypisać listę książek. To powinno wyeliminować powtórzenia ale coś nie pykło a dodaniem użytkownika, i przebudowuję ten słownik. jak widać klasa jest rozgrzebana, więc wszystko się jeszcze może zmienić..

class RemoteUser : User, IVerifiable
    {
        public RemoteUser(string username, string email, string mobilenumber) : base(username, email, mobilenumber)
        {
        }

        Dictionary<User, List<Book>> remoteUser = new Dictionary<User, List<Book>>();

        
        
    #region Methods
        /// <summary>
        /// Metoda obsługująca utworzenie użytkownika
        /// </summary>
        /// <param name="username">Nazwa</param>
        /// <param name="email">E-mail</param>

        public User CreateUser(string username, string email, string mobilenumber)
        {
            username = Console.ReadLine();
            email = Console.ReadLine();
            mobilenumber = Console.ReadLine();

            User remUsr = new User(username, email, mobilenumber);
            return remUsr;
        }

        /// <summary>
        /// Metoda obsługująca zapisanie użytkownika
        /// </summary>
        /// <param name="username">Nazwa</param>
        /// <param name="email">E-mail</param>
        override public void SaveUser(string username, string useremail, string mobilenumber)
        {
            //Not implemented yet
        }

        /// <summary>
        /// Metoda obsługująca weryfikację nowego użytkownika
        /// </summary>
        /// <param name="username">Nazwa</param>
        /// <param name="email">E-mail</param>
        /// <param name="mobilenumber">Numer telefonu komórkowego</param>
        public void UserVerification(string username, string email, string mobilenumber)
        {
            //Not implemented yet
        }
        #endregion

        

    }
class  User
    {
        /// <summary>
        /// Klasa bazowa obsługujaca użytkowników
        /// </summary>
        /// 
        private string _username;
        private string _useremail;
        private string _mobilenumber;
        private static int counter = 0;

        private int UserId { get; set;  }


        public User(string username, string useremail, string mobilenumber)
        {
            counter++;
            UserId = counter;
            _username = username;
            _useremail = useremail;
            _mobilenumber = mobilenumber;
        }


        #region Methods

        /// <summary>
        /// Metoda obsługująca zapisanie użytkownika
        /// </summary>
        /// <param name="username">Nazwa</param>
        /// <param name="email">E-mail</param>
        virtual public void SaveUser(string username, string email, string mobilenumber)
        {
            //Not implemented yet
        }
        #endregion
        #region Properties
        public string UserName
        {
            get
            { return _username; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    _username = value;
                }
            }
        }
        public string UserEmail
        {
            get
            { return _useremail; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    _useremail = value;
                }
            }
        }
        public string MobileNumber
        {
            get
            { return _mobilenumber; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    _mobilenumber = value;
                }
            }
        }
        #endregion

    }
class Book
    {
        private string _title;
        private string _author;
        public static int counter = 0;
        public static int BookId { get; set; }

        
        public Book(string title, string author)
        {
            counter++;
            BookId = counter;
            _title = title;
            _author = author;
        }

        public string Title
        {
            get
            { return _title; }
            set
            {
                if(!String.IsNullOrEmpty(value))
                {
                    _title = value;
                }
            }
        }
        public string Author
        {
            get
            { return _author; }
            set
            {
                if(!String.IsNullOrEmpty(value))
                {
                    _author = value;
                }
            }
        }
        
        public static DateTime DateAddToLibrary(DateTime date)
        {
            return date;
        }
    }
}
0

Tak jak ty to zrobiłeś to jedynie tak się da zrobić . Źle podszedłeś do tematu .

Dictionary<User, List<Book>> remoteUser = new Dictionary<User, List<Book>>();
            User user1 = new User("Jan", "[email protected]", "504480007");

            remoteUser.Add(user1 , new List<Book>() { new Book("Potp", "Henryk Sienkiewicz") });


            Console.WriteLine( remoteUser[user1][0].Author);
            Console.WriteLine(remoteUser[user1][0].Title);

1 użytkowników online, w tym zalogowanych: 0, gości: 1