2012-11-13 21 views
5

बदलने की कोशिश कर रहे विभिन्न वर्गों का निर्माण करता है। मैं एक फ़ंक्शन बनाने की कोशिश कर रहा हूं जो मेरी सेटिंग्स फ़ाइल से डेटा प्राप्त करता है (हाईस्कोरस्वेड विच हाईस्कॉर्क्सलिस्ट सरणी में डाल दिया जाता है) और फिर तारों में शामिल हो जाते हैं और उन्हें टेक्स्टबॉक्स में लिखते हैं (हाईस्कॉर .text)विंडोज़ टेक्स्टबॉक्स.टेक्स्ट

हालांकि जब मैं समारोह कुछ भी नहीं पर कॉल

होता है तो यहाँ मेरी कोड है: Form1

private void button4_Click_1(object sender, EventArgs e) 
    { 
     Highscore.Fetch(); 
     Highscore.Set(); 
    } 

    public void highscoreText (string value) 
    { 
      this.highScore.Text = value; 
    } 

और यहाँ वर्ग thats के नाम से जाना चाहिए Highscore.Fetch द्वारा() , और Highscore.Set () लेकिन कुछ भी नहीं जब मैं उन पर फोन मेरे पाठ बॉक्स

public static class Highscore 
    { 
     public static void Fetch() 
     { 
      Form1.highscoreList[0] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper1 + "\t\t" + HighscoreSaved.Default.highscoreScore1; 
      Form1.highscoreList[1] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper2 + "\t\t" + HighscoreSaved.Default.highscoreScore2; 
      Form1.highscoreList[2] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper3 + "\t\t" + HighscoreSaved.Default.highscoreScore3; 
      Form1.highscoreList[3] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper4 + "\t\t" + HighscoreSaved.Default.highscoreScore4; 
      Form1.highscoreList[4] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper5 + "\t\t" + HighscoreSaved.Default.highscoreScore5; 
      Form1.highscoreList[5] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper6 + "\t\t" + HighscoreSaved.Default.highscoreScore6; 
      Form1.highscoreList[6] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper7 + "\t\t" + HighscoreSaved.Default.highscoreScore7; 
      Form1.highscoreList[7] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper8 + "\t\t" + HighscoreSaved.Default.highscoreScore8; 
      Form1.highscoreList[8] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper9 + "\t\t" + HighscoreSaved.Default.highscoreScore9; 
      Form1.highscoreList[9] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper10 + "\t\t" + HighscoreSaved.Default.highscoreScore10; 

      Form1.highscoreInt[0] = HighscoreSaved.Default.highscoreScore1; 
      Form1.highscoreInt[1] = HighscoreSaved.Default.highscoreScore2; 
      Form1.highscoreInt[2] = HighscoreSaved.Default.highscoreScore3; 
      Form1.highscoreInt[3] = HighscoreSaved.Default.highscoreScore4; 
      Form1.highscoreInt[4] = HighscoreSaved.Default.highscoreScore5; 
      Form1.highscoreInt[5] = HighscoreSaved.Default.highscoreScore6; 
      Form1.highscoreInt[6] = HighscoreSaved.Default.highscoreScore7; 
      Form1.highscoreInt[7] = HighscoreSaved.Default.highscoreScore8; 
      Form1.highscoreInt[8] = HighscoreSaved.Default.highscoreScore9; 
      Form1.highscoreInt[9] = HighscoreSaved.Default.highscoreScore10; 

      Form1.highscoreKeeper[0] = HighscoreSaved.Default.highscoreKeeper1; 
      Form1.highscoreKeeper[1] = HighscoreSaved.Default.highscoreKeeper2; 
      Form1.highscoreKeeper[2] = HighscoreSaved.Default.highscoreKeeper3; 
      Form1.highscoreKeeper[3] = HighscoreSaved.Default.highscoreKeeper4; 
      Form1.highscoreKeeper[4] = HighscoreSaved.Default.highscoreKeeper5; 
      Form1.highscoreKeeper[5] = HighscoreSaved.Default.highscoreKeeper6; 
      Form1.highscoreKeeper[6] = HighscoreSaved.Default.highscoreKeeper7; 
      Form1.highscoreKeeper[7] = HighscoreSaved.Default.highscoreKeeper8; 
      Form1.highscoreKeeper[8] = HighscoreSaved.Default.highscoreKeeper9; 
      Form1.highscoreKeeper[9] = HighscoreSaved.Default.highscoreKeeper10;     
     } 
     public static void Set() 
     { 
      Form1 mainForm = new Form1(); 
      string[] highscoreImported = new string[10]; 
      Array.Copy(Form1.highscoreList, highscoreImported, 10); 
      string highscores = string.Join("\n", highscoreImported); 
      mainForm.highscoreText(highscores); 
     } 

उत्तर

8

आप अपने Set विधि में Form1 का एक नया उदाहरण बना रहे हैं में बदल जाता है, यही कारण है कि आप परिवर्तन नहीं देख रहे हैं है। आपको विधि के बजाय विधि का एक उदाहरण पास करना चाहिए।

कुछ की तरह (अपने वर्ग Highscore में):

public static void Set(Form mainForm) 
{ 
    string[] highscoreImported = new string[10]; 
    Array.Copy(Form1.highscoreList, highscoreImported, 10); 
    string highscores = string.Join("\n", highscoreImported); 
    mainForm.highscoreText(highscores); 
} 

बाद में आप इसे पसंद कॉल कर सकते हैं:

private void button4_Click_1(object sender, EventArgs e) 
{ 
    Highscore.Fetch(); 
    Highscore.Set(this); //notice "this" 
} 
+2

वाईई यह काम करने लग रहा था, धन्यवाद –

1

एक बेहतर दृष्टिकोण एक बार में उन सभी को ऐसा करने के लिए किया जाएगा।

using System.Linq; 

public static void FetchAndSet() 
{ 
    Form1.highscoreList[0] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper1; 
    . 
    . 

    Form1.highscoreInt[0] = HighscoreSaved.Default.highscoreScore1; 
    . 
    . 

    Form1.highscoreKeeper[0] = HighscoreSaved.Default.highscoreKeeper1; 
    . 
    . 

    //Use LINQ's Take to Pull out the Top 10. 
    string highscores = string.Join("\n", highscoreList.Take(10)); 
    this.highscoreText(highscores);  
}