Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

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

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
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
  • 3
  • 2
2 Solutions
 
Gustav BrockCIOCommented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
Gustav BrockCIOCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Miguel OzSoftware EngineerCommented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
Miguel OzSoftware EngineerCommented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
Miguel OzSoftware EngineerCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now