Solved

What variable type or object will be... .net

Posted on 2014-11-21
8
143 Views
Last Modified: 2014-11-28
Please suggest a variable for my use to maintain:

1. key value like "FirstName", "lastName", etc.
2. yes/no for the matching key (boolean or string).
3. index (int is preferred but string is acceptable).
4. Sortable using index data as 1, 2, 3 or "01", "02", "03"

c#

dictionary, arraylist, etc.
Please comment on ease of use and performance.
0
Comment
Question by:Mike Eghtebas
  • 3
  • 3
  • 2
8 Comments
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 100 total points
ID: 40459305
To me it sounds like a class with your values and Ids as properties.

I guess it should be persistent too, as you mention "maintain". You would probably use a database, but you can easily make the class persistent and store it in an XML file.

/gustav
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40459559
Thanks for the help. Would the following format work?

<?xml version="1.0" encoding="utf-8" ?>
<Users>
     <User Name="Default">
         <Settings>
              <Setting SettingName = "Default">
                     <Field Name="First_Name" Text ="All" Include="Yes" Index="2"></Field>
                     <Field Name="Last_Name" Text ="All" Include="Yes" Index="1"></Field>
             </Setting>
        </Settings>
    </User>
    <User Name="John">
         <Settings>
              <Setting SettingName = "Setting-1">
                    <Field Name="First_Name" Text ="John" Include="Yes" Index="1"></Field>
                     <Field Name="Last_Name" Text ="Doe" Include="Yes" Index="2"></Field>
             </Setting>
        </Settings>
    </User>
    <User Name="Robert">
         <Settings>
              <Setting SettingName = "My 1st Setting">
                    <Field Name="First_Name" Text ="Angela" Include="No" Index="2"></Field>
                     <Field Name="Last_Name" Text ="Hansen" Include="Yes" Index="1"></Field>
             </Setting>
        </Settings>
         <Settings>
              <Setting SettingName = "Choice two">
                    <Field Name="First_Name" Text ="Jane" Include="Yes" Index="1"></Field>
                     <Field Name="Last_Name" Text ="Hansen" Include="Yes" Index="2"></Field>
             </Setting>
        </Settings>
    </User>
</Users>

Open in new window

When a new user, say Alex opens the page, the entire block '<User Name="Default"> . . . </User>' will be copied to this xml file as:  <User Name="Alex"> . . . </User>.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40460071
I can't tell. I've only done it once and that was the other way round - first creating the class, then make it persistent, and from that created the XML file.

/gustav
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40460236
1& 2: First name and last name are not good candidate keys as they are not unique. You are better off using unique integer/string Id depending on list size.
3& 4: Please clarify if we need to sort by First name or last name the whole list. From the xml it seems that sorting will depend on setting???.

if you do not have a unique key you could use a generic list:
LIst<User> users = new LIst<User>();

Open in new window

where User is the user class. My suggestion is to map all information as elements (not attributes) as it is easier to map to a C# class and persist (serialize) later on to xml.
Note: It is possible to serialize C# properties to attributes, but it needs more work as shown here
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40460248
1& 2: First name and last name are not good candidate keys
First_Name, Last_Name, etc. are unique per user. And User_First_Name, First_Name, Last_Name_Last_Name, etc. are unique in the entire document.

3& 4: Please clarify if we need to sort by
Only index to be sorted withing User (user will decide how her/his controls has to be displayed, like tab index).
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40460983
In other words, sorting is only needed for how to display first and last name. Correct?

Q1. Any particular reason why you need the settings element?
Xml  (and class) could be simpler if you added directly to User node. For new user you can add a SetupNewUser method to assign the default values.
Q2. Could you explain what include does?
For xml purposes if element is not needed, we do not saved it to xml file.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40461039
Hi Miguel,

In other words, sorting is only needed for how to display first and last name. Correct?
Yes. Actually, there will be more than 2, maybe 10 controls.

Q1. Any particular reason why you need the settings element?
settings with username "Default" will be copied to settings with username "User1", "User2", etc. This will allow User1, for example, to modify her/his tab-index to display the controls they way s/he wishes; as you have described in your post above (by using SetupNewUser method).

Xml  (and class) could be simpler if you added directly to User node.
Yes, easy is better. This is good choice.

Q2. Could you explain what include does?
If it is false, that control will be excluded (will not be displayed).  The settings with username "Default" will show all controls meaning all includes will be true by default unless a user decides not to see a control by changing its include to false. We need to saved it to xml file for each user and each control under that user..

Mike
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 400 total points
ID: 40461900
Ok, your user class could be implemented as follows:
    public class User
    {
        public string Name { get; set; }
        public List<Setting> Settings { get; set; }

        public User()
        {
            this.Settings = new List<Setting>();
        }

        public static User GetDefaultUser(string name = "Default")
        {
            User user = new User();
            user.Name = name;
            user.Settings.Add(Setting.GetDefaultSetting());
            return user;
        }
    }

Open in new window

where
    public class FieldInfo
    {
        [XmlAttribute]
        public string Name { get; set; }
        [XmlAttribute]
        public string Text { get; set; }
        [XmlAttribute]
        public bool Include { get; set; }
        [XmlAttribute]
        public int Index { get; set; }

        public FieldInfo()
        {
        }
    }

    public class Setting
    {
        [XmlAttribute]
        public string Name { get; set; }
        public List<FieldInfo> Fields { get; set; }

        public Setting()
        {
            this.Fields = new List<FieldInfo>();
        }
        
        public static Setting GetDefaultSetting()
        {
            Setting defaultSetting = new Setting();
            defaultSetting.Name = "Default";
            defaultSetting.Fields.Add(new FieldInfo() { Name = "First_Name", Text = "All", Include = true, Index = 2 });
            defaultSetting.Fields.Add(new FieldInfo() { Name = "Last_Name", Text = "All", Include = true, Index = 1 });
            return defaultSetting;
        }
    }

Open in new window

Note: I used generic list in the implementation but feel free to change It to generic dictionaries if one of the properties can be used as unique key and you have to do search/sort by that key. For 10 items I would not worry about Settings property.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with opening combobox using VB.NET. 3 26
Open a link in vb.net 2 16
Visual Studio npm 1 12
itextsharp with c# 3 10
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question