これは基本的なC#の質問である必要があります。私はList<Pet>
あるPets
という名前のプロパティが含まれていPerson
クラスがあるとします。子オブジェクトへの新しいオブジェクトの割り当てによる子オブジェクトの更新
私はペットを更新したいのですが、そのペットを変数として取得してプロパティを操作できますが、新しいペットオブジェクトを作成して既存のペットオブジェクトに割り当てることはできません。私は "割り当てられた値は実行パスに使用されていません"という警告を受け取ります。私はこの問題を概説する非常にシンプルなコードをいくつか作成しました。
実際のコードでは、新しい子オブジェクトを使用して既存のオブジェクトを置き換えたいと考えています。下のサンプルのupdatedCatでどうすればいいのか教えてください。
class Program
{
static void Main(string[] args)
{
var program = new Program();
program.RunMe();
}
public void RunMe()
{
var myPerson = new Person() { Name = "John Doe" };
var dog = new Pet() { Type = "Dog", Name = "Woofie" };
var cat = new Pet() { Type = "Cat", Name = "Chester" };
myPerson.Pets.Add(dog);
myPerson.Pets.Add(cat);
Console.WriteLine("Initial Pet Status:");
ListPets(myPerson);
var currentDog = myPerson.Pets.SingleOrDefault(p => p.Type == "Dog");
currentDog.Name = "Snoopie";
Console.WriteLine("\r\nPet Status After Updating Dog Directly (name should be 'Snoopie'):");
ListPets(myPerson);
var updatedCat = new Pet() { Type = "Cat", Name = "Felix" };
var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
currentCat = updatedCat;
//Resharper shows "Value assigned is not used in any execution path" for the currentCat
//and the current cat is never updated
Console.WriteLine("\r\nPet Status After Trying to Update Cat by Assigning a New Cat to existing Cat (name should be 'Felix' but it's not):");
ListPets(myPerson);
Console.ReadLine();
}
public void ListPets(Person person)
{
foreach (var pet in person.Pets)
{
Console.WriteLine(string.Format(" {0} has a {1} named {2}", person.Name, pet.Type, pet.Name));
}
}
}
public class Person
{
public string Name { get; set; }
public List<Pet> Pets { get; set; }
public Person()
{
Pets = new List<Pet>();
}
}
public class Pet
{
public string Type { get; set; }
public string Name { get; set; }
}
編集ペットにID値を追加し、InsertOrUpdatePet方法 を作成する(注:私は簡潔のためConsole.WriteLineをコマンドを削除)
class Program
{
static void Main(string[] args)
{
var program = new Program();
program.RunMe();
}
public void RunMe()
{
var myPerson = new Person() { Name = "John Doe" };
var dog = new Pet() { Id = 1, Type = "Dog", Name = "Woofie" };
var cat = new Pet() { Id = 2, Type = "Cat", Name = "Chester" };
myPerson.Pets.Add(dog);
myPerson.Pets.Add(cat);
var updatedCat = new Pet() { Id = 2, Type = "Cat", Name = "Felix" };
InsertOrUpdatePet(myPerson, updatedCat);
var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
}
public void InsertOrUpdatePet(Person person, Pet pet)
{
var currentPet = person.Pets.SingleOrDefault(p => p.Id == pet.Id);
if(currentPet == null)
{
person.Pets.Add(pet);
}
else
{
currentPet = pet; // This doesn't work
person.Pets.SingleOrDefault(p => p.Id == pet.Id) = pet; //This throws an error
}
}
}
public class Person
{
public string Name { get; set; }
public List<Pet> Pets { get; set; }
public Person()
{
Pets = new List<Pet>();
}
}
public class Pet
{
public int Id { get; set; }
public string Type { get; set; }
public string Name { get; set; }
}
後者は動作しません。 – jason
オブジェクトとオブジェクト参照の違いをまだ理解していないようです。あなたはそれについていくつかの研究をしたいかもしれません。 – usr