2013-01-09 2 views
0

저는 위대한 프로그래머는 아니지만, C#으로 맞춤형 Minecraft 실행 프로그램을 만들려고합니다. Soo 옵션로드 및 저장을위한 클래스가 있습니다. - 훌륭하게 작동하지만, 이제는 1 클래스의 2 변수가 어떻게 든 1 주소에 poiting하는 문제가 있습니다. 3 가지 팩 중에서 선택할 수 있으며 각각 개별 옵션이 있습니다. RAM, 디버그 모드, 버전 선택 모드 및 사용자 정의 버전을로드하고 저장할 수 있습니다. 하지만 MODE와 VERSION은 같은 주소에 poiting하고 있습니다. 그래서 하나를 변경하면 다른 변경도 가능합니다. 그러나 왜, 당신이 저를 도와 줄 수 있습니까? 옵션2 개의 다른 변수가 1 개의 주소를 가지고 있습니다.

 //Setting stream to beggining 
     optstr.Position = 0; 
     //Loading options file from the disc 
     opt = formatter.Deserialize(optstr) as ObjOptions; 
     //Setting version 
     opt.version[modpack] = comboBox1.SelectedIndex; 
     //Setting mode 
     if (radioButton1.Checked) opt.mode[modpack] = 0; 
     else if (radioButton2.Checked) opt.mode[modpack] = 1; 
     else opt.mode[modpack] = 2; 
     //Setting debug 
     opt.debug[modpack] = debug.Checked; 
     //Setting memory 
     opt.memory[modpack] = Convert.ToInt16(ram.Text); 
     //Setting stream position 
     optstr.Position = 0; 
     //Writing to file 
     formatter.Serialize(optstr, opt); 
     //Closing options window 
     this.Dispose(); 

ObjOptions 클래스를 저장할 때 어떻게됩니까

[Serializable] 
class ObjOptions 
{ 
    public List<int> version; 
    public List<int> mode; 
    public List<int> memory; 
    public List<bool> debug; 
    public ObjOptions(List<int> version, List<int> mode, List<int> memory, List<bool> debug) 
    { 
     this.version = version; 
     this.mode = mode; 
     this.memory = memory; 
     this.debug = debug; 
    } 
    public ObjOptions() 
    { 
     List<int> l1 = new List<int>(); 
     for (int i = 0; i < 3; i++) 
      l1.Add(0); 
     List<int> l2 = new List<int>(); 
     for (int i = 0; i < 3; i++) 
      l2.Add(0); 
     List<int> l3 = new List<int>(); 
     for (int i = 0; i < 3; i++) 
      l3.Add(1024); 
     List<bool> l4 = new List<bool>(); 
     for (int i = 0; i < 3; i++) 
      l4.Add(false); 
     this.version = l1; 
     this.mode = l2; 
     this.memory = l3; 
     this.debug = l4; 
    } 
} 

당신은 나에게 말하지 않는 더 나은 수행 할 수 있습니다 같은 느낌,하지만 같은 방식으로 서비스를 제공 아무것도 찾을 경우 . 난 그냥 ... opt.modeopt.version

답변

2

때문에 서로 연결되어 이유를 알고 싶어

this.version = l1; 
this.mode = l1; 

이를 모두 versionmode 필드에 동일한 목록 객체 참조를 할당합니다. 아마도 l1 대신 hovnothis.mode에 할당하겠습니까?

+0

그래도 작동하지 않습니다. 나는 그 문제가 당신이 거기에서 볼 수있는 것처럼 해결 될 것이라고 힘들다. (나의 포스트를 편집했다.) 여전히 효과가 없다. 만약 내가 이것을 이렇게 만들었다 고해도, 새로운 것 같아요 ** 새로운 목록 (l1); ** 등등 – DragonCz

+0

@DragonCz 당신이 깨진 코드로 직렬화 한 인스턴스를 비 직렬화하고 있습니까? 그렇다면 직렬화 된 인스턴스는 생성 된 잘못된 코드로 인해 손상됩니다. 그들을 삭제하고 다시 시작하십시오. – cdhowie

+0

대단히 고맙습니다. 내가 백만 번이나 같은 코드를 다시 작성했기 때문에 저는 거의 알지 못했습니다! 문제가 해결되었습니다. – DragonCz

0

대답은 간단합니다 :

this.version = l1; 
this.mode = l1; 

당신은 같은 목록에 versionmode 모두를 설정합니다. 다음과 같이 더해야합니다.

this.mode = new List<int>(l1); 
+0

문제의 진단이 정확합니다. 제안 된 솔루션이 맞지 않거나 나타나지 않습니다. 그는 변수 이름에 오타가 있고, 다른 목록을 복사하고 싶지 않습니다. – Servy

관련 문제