Jak ominąć tworzenie nowej kolekcji

0

mam metodę która dodaje mi profile logowania do kolekcji ale chciałbym żeby jak w kolekcji jest zapisany już profil to pomijał tworzenie nowej kolekcji a jedynie dodawał nowy profil. jak to mogę zrobić

  public void SaveProfile()   
        {
            HashSet<ProfileModel> profileList = new HashSet<ProfileModel>();
            profileList.Add(CurrentUser);
            Debug.WriteLine("Add new profile");
            var currentprofile = Newtonsoft.Json.JsonConvert.SerializeObject (profileList);
            FileManagerInstance.WriteTextToFile (FileManager.FileType.ProfileList, currentprofile);
        }
 
1

Zrób ten HashSet jako pole klasy tam zarządzaj profilami.

Jak widać powtarzający się profil nie wpada do kolekcji:

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

class UserProfile
{
    public string Username { get; private set; }
    public string Name { get; set; }

    public UserProfile(string username, string name)
    {
        this.Username = username;
        this.Name = name;
    }

    public override bool Equals(object obj)
    {
        var profile = obj as UserProfile;
        return profile.Username == this.Username && profile.Name == this.Name;
    }

    public override int GetHashCode()
    {
        return this.Username.GetHashCode() ^ this.Name.GetHashCode();
    }
}

class Server
{
    public HashSet<UserProfile> Profiles { get; private set; }
    public Server()
    {
        this.Profiles = new HashSet<UserProfile>();
    }
}

class Program
{
    public static void Main(string[] args)
    {
        var server = new Server();
        server.Profiles.Add(new UserProfile("grzesiek51114", "Grzesiek"));
        server.Profiles.Add(new UserProfile("grzesiek51114", "Grzesiek"));

        Console.WriteLine(server.Profiles.Count);
    }
}

BTW: ...to pomijał tworzenie nowej kolekcji... chyba pomijał tworzenie nowego obiektu w kolekcji? Nie wiem czy Cię dobrze zrozumiałem :)

1
Sierr napisał(a):

mam metodę która dodaje mi profile logowania do kolekcji ale chciałbym żeby jak w kolekcji jest zapisany już profil to pomijał tworzenie nowej kolekcji a jedynie dodawał nowy profil. jak to mogę zrobić

  public void SaveProfile()   
        {
            HashSet<ProfileModel> profileList = new HashSet<ProfileModel>();
            //...
        }
 

To jej nie twórz jej za każdym razem jak robisz SaveProfile tylko utwórz jedną instancję i jej używaj. Coś w stylu:

public class Foo
{
  static HashSet<ProfileModel> profileList = new HashSet<ProfileModel>();
  public void SaveProfile()  
  {
    // ...
  }
}
0

Dziękuje za odpowiedzi. @grzesiek51114 możesz mi pomóc jeszcze w jednej kwestii? Mam jeszcze metodę pobierającą profilr

 public async Task<List<ProfileModel>> GetProfileList()
        {
            var content = await FileManagerInstance.ReadFile(FileManager.FileType.ProfileList);

            if(string.IsNullOrEmpty(content)) return null;
            try 
            {
                _profileList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProfileModel>>(content);
            } 
            catch(Newtonsoft.Json.JsonException ex)
            {
                Debug.WriteLine("Result content is bad!");

                if (Debugger.IsAttached) 
                {
                    throw ex;
                }
            }

            return _profileList;

        } 

Jak zrobić do tego co mi napisałeś aby ta kolekcja przy uruchamianiu programu byłą zainicjowana z zapisanymi profilami?

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