What is a good way to keep track of strings for dictionary lookups?

I am working through the Windows 8 app tutorial.

They have some code about saving app data like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> private void NameInput_TextChanged(object sender, TextChangedEventArgs e)
{
Windows.Storage.ApplicationDataContainer roamingSettings =
Windows.Storage.ApplicationData.Current.RoamingSettings;
roamingSettings.Values["userName"] = nameInput.Text;
}
</code>
<code> private void NameInput_TextChanged(object sender, TextChangedEventArgs e) { Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; roamingSettings.Values["userName"] = nameInput.Text; } </code>
 private void NameInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            Windows.Storage.ApplicationDataContainer roamingSettings = 
                Windows.Storage.ApplicationData.Current.RoamingSettings;
            roamingSettings.Values["userName"] = nameInput.Text;
        }

I have worked with C# in the past and found that things like using constant string values (like “userName” in this case) for keys could get messy because auto-complete did not work and it was easy to forget if I had made an entry for a setting before and what it was called. So if I don’t touch code for awhile I end up accidentally creating multiple entries for the same value that are named slightly differently.

Surely there is a better way to keep track of the strings that key to those values. What is a good solution to this problem?

2

I would encapsulate it somehow. Maybe like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Settings
{
Windows.Storage.ApplicationDataContainer roamingSettings;
public Settings()
{
roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
}
public String UserName {
get { return (String)roamingSettings.Values["userName"]; }
set { roamingSettings.Values["userName"] = value ;}
}
// etc..
}
</code>
<code>class Settings { Windows.Storage.ApplicationDataContainer roamingSettings; public Settings() { roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; } public String UserName { get { return (String)roamingSettings.Values["userName"]; } set { roamingSettings.Values["userName"] = value ;} } // etc.. } </code>
class Settings
{
    Windows.Storage.ApplicationDataContainer roamingSettings;

    public Settings()
    {
        roamingSettings =  Windows.Storage.ApplicationData.Current.RoamingSettings;
    }

    public String UserName {
        get { return (String)roamingSettings.Values["userName"]; }
        set { roamingSettings.Values["userName"] = value ;} 
    }

    // etc..
}

The value is only in one place, so you can always check what you have already declared. It enables intellisense and it makes it type safe. You could also make the whole thing static or singleton.

1

It may depend on the speed and memory performance that you need.

I have been thinking a lot about radix sort recently. It has some great characteristics. It could be a great candidate for anything where you might want to implement your own auto complete. Speed wise it is O(kN) complexity where k is the length of the strings and N is the number of strings. Storage is also O(kN).

Unlike a STL vector, a linked list, or an array of strings, maintaining a sorted table does not involve a linear search for an insertion point because the data is in a tree with branches that are indexed by the letters in the string which is a constant time operation repeated once for each character in the string. Checking to see if a string is in the table takes the same or less effort, terminating when one of the letters indexes a NULL pointer. Finding the first M strings to display for an auto complete is similarly fast.

A hash table can also be a great tool for dictionary lookup.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật