2013-12-09 88 views
1

我即將創建一個應用程序,我要測量並因此能夠存儲大量加速度計的設置。問題是,我不知道現在有多少,因此我希望能夠爲每個加入的加速度計創建一個加速度計設置的新實例(加速計的名稱,頻率範圍等)系統。爲了能夠配置設置,我添加了一個ListBox,它將顯示加速度計的名稱,並且當點擊此框中的項目時,頁面上的文本框應該填充設置。創建設置的幾個實例

...至少這是理論。有誰知道如何基於文本框中的字符串創建AccelerometerSettings類的實例?那就是,而不是;

AccelerometerSettings SomeRandomAccelerometer = New AccelerometerSettings(); 

我想有

AccelerometerSettings TheNameTypedInTheTextBox = New AccelerometerSettings(); 

爲每個新的加速度計。還是我完全誤解了SettingsClass的工作原理?

如何在列表框中顯示我所有的加速度計,並在單擊時使設置顯示在文本框中?

非常感謝提前!

+1

您提供的示例僅僅修改變量的名稱。可能不是你的意圖。您可以選擇使用傳遞給AcceleromterSettings構造函數的外部配置文件,這個文件基於它所保存的傳感器類型。然後構造函數將使用該文件來確定其設置。配置文件路徑可以以textBox值作爲關鍵字保存在字典中,但這會導致每個新傳感器都需要更改源文件。 – Michael

+1

對AccelerometerSettings不太熟悉,但是如果我有一個任務將一個on對象與一個字符串聯繫起來,我可能會使用Dictionary 。還有其他一些方法可以完成,但我認爲這是最簡單的方法。 – Evgeni

+0

@邁克爾:謝謝,但我不太理解!我已經有了使用Visual Studios工具創建的設置文件。但是你的意思是我應該添加一些其他類型的配置文件?如果是這樣,那麼什麼類型的配置文件,以及我應該保存在那個文件中?通過修改源代碼,你的意思是我必須修改我添加的每個傳感器的代碼。 – user2950764

回答

1

您似乎有一些基本的理解問題。

AccelerometerSettings TheNameTypedInTheTextBox = New AccelerometerSettings();

是類型爲AccelerometerSettings的變量的c#聲明,該變量也被初始化爲新的對象實例。

要在運行時維護命名的設置集合,您應該使用集合類。

例如,你可以使用一本字典類

Dictionary<TKey,TValue> 

聲明並初始化你的字典作爲

var MySettings = new Dictionary<string,AccelerometerSettings>(); 

使用上MySettings的Add()方法添加一個新實例名稱(由用戶提供)。字典

+0

謝謝加里!毫無疑問,我對這種編程相當陌生!我的想法是,因爲我用Visual Studio工具創建了一套新的設置,所以要手動完成它,我會創建一個設置文件的新實例並對其進行重命名。你和Eugenes的解決方案看起來很棒,但是他們提出了一些新的問題。首先,我如何以簡單的方式存儲這些設置,以便設置從會話到會話保持不變?我是否必須在啓動時閱讀所有這些設置,或者是否自動保存?提前致謝! – user2950764

+1

@ user2950764您可能需要爲此創建一個新問題,並標記/上傳您喜歡的答案。 – Evgeni

+0

@Eugene:你可能是對的。對不起,我忘了! – user2950764

1

這是一個非常基本的,天真的實現的東西,將實現接近你在找什麼。它可以大大改善,但它應該讓你在正確的軌道上。

using System.Collections.Generic; 

class SettingsManager 
{ 
    Dictionary<string, AcceleromterSettings> settings; 

    // contructor for SettingsManager class; a fundamental C# concept 
    public SettingsManager() 
    { 
     LoadSettings(); 
    } 

    // creates new instances of AccelerometerSettings and stores them 
    // in a Dictionary<key, value> object for later retrieval. The 
    // Dictionary object uses a unique-key to access a value 
    // which can be any object. 
    void LoadSettings() 
    { 
     settings = new Dictionary<string, AcceleromterSettings>(); 

     AcceleromterSettings horizontal = new AcceleromterSettings(); 
     horizontal.AttributeOne = 101; 
     horizontal.AttributeTwo = 532; 
     horizontal.AttributeThree = 783; 
     settings.Add("Horizontal", horizontal); 

     AcceleromterSettings vertical = new AcceleromterSettings(); 
     vertical.AttributeOne = 50; 
     vertical.AttributeTwo = 74; 
     vertical.AttributeThree = 99; 
     settings.Add("Vertical", vertical); 
    } 

    // Retrieves settings from the Dictionary while hiding 
    // specific implementation details. 
    public AcceleromterSettings GetSettings(string name) 
    { 
     AcceleromterSettings setting = null; 

     // this method of Dictionary returns a Boolean value indicating 
     // whether or not the value retrieval worked. If the Key is found 
     // in the dictionary, the 'out' parameter is modified reflect 
     // the value, and the method returns true. If the key is not found 
     // the 'out' parameter is not modified, and the method returns false. 
     if (!settings.TryGetValue(name, out setting)) 
      throw new ArgumentException(string.Format("Settings not found for settings name {0}", name)); 

     return setting; 
    } 
} 
+0

非常感謝!但是,只有一些問題,SettingsManager()方法和LoadSettings有什麼作用?在啓動時閱讀所有設置?什麼是設置= null;在GetSettings()中,因爲我看到它只是返回null? – user2950764

+1

@ user2950764請參閱編輯。如果您還有其他問題,請告訴我。 – Michael

+0

再次@邁克爾,非常感謝!雖然我收到了很多奇怪的錯誤,但是「類型'Program.AccelerometerSettings'已經包含了對'defaultInstance'(一個我從未創建和使用過的變量)的defenition,並且對於我嘗試的幾乎所有的屬性都是一樣的當我雙擊錯誤時,我得到自動生成的代碼。任何想法是什麼? – user2950764

相關問題