Solved

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

Posted on 2014-11-21
8
144 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 50

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 50

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
Technology Partners: 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!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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