C#에서 간단한 작업을 수행하려고했지만 개체를 목록에 넣으려고했는데 이전에이 작업을 수행했지만이 문제가 발생하지 않았습니다. 검색을 몇 번 수행 한 후에 비슷한 문제가있는 사람들과 해결책을 찾았지만 문제가 해결되지 않은 경우 코드가 있습니다.목록에 반복되는 항목이 있습니다.
코멘트 한 후static void GenerateRooms(int RoomsNumber)
{
int randomWidth;
int randomHeight;
Room newRoom = null;
for (int i = 0; i < RoomsNumber; i++)
{
//Create new rooms and store it on the list
randomWidth = rand.Next(_MinRoomW, _MaxRoomW + 1);
randomHeight = rand.Next(_MinRoomH, _MaxRoomH + 1);
//Room(x, y, id)
newRoom = new Room(randomWidth, randomHeight, i);
//1
_RoomsL.Insert(i, newRoom);
}
}
, 사실은 목록을 검색하고, 모든 개체는 0에서 마지막에,이,하지만 때 나는 예를 들어 하나처럼, 다른이 기능을 종료 :
그리스트에static void CheckList()
{
foreach(Room nextRoom in _RoomsL)
{
Console.WriteLine(" This room have the id: " + nextRoom.GetId());
}
}
모든 객체
... 동일한 ID는, 그 경우에, 번호가 첫 번째 방법의 목록에 추가 된 마지막 오브젝트와 동일한가 너무 그와 같은
GenerateRooms(RoomsNumber); << at the end of this function, the list is ok.
CheckList(); << just after exiting the last function and checking the same list, all the objects are the same.
list.Insert
도 사용해 보았지만 아무 것도 변경하지 않았습니다. 나는 정말로 무엇을해야할지 모른다.
룸 클래스. 그것은 정적 변수가 있다면
class Room
{
//This is random.
public static Random rand = new Random();
//Room variables
public static int rWIDTH, rHEIGHT;
public static int ROOMID;
public Room(int X, int Y, int id)
{
rWIDTH = X;
rHEIGHT = Y;
ROOMID = id;
}
public int GetWidth()
{
return rWIDTH;
}
public int GetHeight()
{
return rHEIGHT;
}
public int GetId()
{
return ROOMID;
}
}
Room.GetId() 메소드를 게시 할 수 있습니까? 이것은 도움이 될 것입니다. – c0d3b34n
또는 Room 클래스 –
각 메서드가 정적 인 이유는 무엇입니까? 클래스 안에 무국적자가있는 것 같지 않습니다. 따라서 정적 인 모든 것이 좋은 디자인이 아닐 수 있습니다. 어쨌든 이것은 코드 리뷰 플랫폼이 아닙니다 ;-) – Mat