다음과 같은 클래스가 주어집니다.
public class Dinosaur
{
public string Specie { get; set; }
public int Age { get; set; }
public int Weight { get; set; }
public Point Location { get; set; }
// Constructor
public Dinosaur()
{
}
그리고 다음과 같은 목록 :
public static List<Dinosaur> Dinosaurs = new List<Dinosaur>();
목록의 마지막 항목에서 값을 변경하는 올바른 방법은 무엇입니까? 이 경우 오류가 발생합니다 ( " 'System.Collections.Generic.List.this [int]'에 대한 최상의 오버로드 된 메서드 일치에 잘못된 인수가 있습니다.").
Dinosaurs[Dinosaurs.Last()].Location.X = pixelMousePositionX;
언제나처럼 미리 감사드립니다! Stackoverflow는이 프로젝트에서 생명의 은인이었습니다.
Dinosaurs.Last()
이미 마지막 항목을 반환하므로 인덱서가 전혀 필요하지 않습니다.
Dinosaurs.Last().Location.X = pixelMousePositionX;
이제이 오류가 발생합니다. 'DinosaurIsland.Dinosaur.Location'의 반환 값은 변수가 아니므로 수정할 수 없습니다.
은 Point
참조 유형이 아니라 구조체 이기 때문 입니다. 그래서 당신은 새로운 점을 만들어야합니다.
Point oldLocation = Dinosaurs.Last().Location;
Dinosaurs.Last().Location = new Point { X = pixelMousePositionX, Y = oldLocation.Y };
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다