Solved

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

Posted on 2014-11-21
8
145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 51

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 51

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 36

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 36

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 36

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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