A.Pissicat
A.Pissicat

Reputation: 3295

Search if class contains a specific property

I have a setting file in my project, that contains some StringCollection (one by user). Each StringCollection is named with a user name and contains one or more values for this user. As an example, if an user is name "User1", the settings will contain this :

<setting name="User1" serializeAs="Xml">
    <value>
        <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <string>[email protected]</string>
            <string>[email protected]</string>
        </ArrayOfString>
    </value>
</setting>

I want to test is my settings contains a certain user, i.e. I want to know if class Properties.Settings.Default has a property named like this user. How can I do?

If my question in unclear, I want to have something like this:

private bool UserExists(String user)
{
    // Test if a property in Properties.Settings.Default is named "user"
}

In my case, I have a field Properties.Settings.Default.User1. So UserExists("User1") must be True and UserExists("User2") must be False

Upvotes: 2

Views: 91

Answers (3)

Rush
Rush

Reputation: 76

You can load settings as StringCollection and call Contains() on the collection to determine if your user exist or not

Example

private bool UserExists(String user)
{
    // Test if a StringCollection is named "user"
    return Properties.Settings.Default.User1.Contains(user);
}

EDIT

What you are trying to do is find if the settings exist or not, for that you can try something like this

private bool UserExist(string user)
{
   return Properties.Settings.Default.Properties.Cast<SettingsProperty>().Any(prop => prop.Name == user);
}

Upvotes: 2

Lord Drake
Lord Drake

Reputation: 182

Your question is unclear, and your clarification is confusing the issue. Your title is "Search if a string file contains a field", then you state that you want to see if your settings contain a certain user, while your clarification seems to indicate that you want the name of a collection? Since the first and last items in that list don't make much sense, I'll answer the second.

According to the MSDN documentation for StringCollection the extension method .Contains() does exactly what you seem to be looking for.

Here is a modified version of the documented example that lines up with your question a little more.

using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection
{
    public static void Main()
    {
        // Creates and initializes a new StringCollection.
        StringCollection myCol = new StringCollection
        {
            "[email protected]",
            "[email protected]",
            "[email protected]",
            "[email protected]"
        };

        if (UserExists(myCol, "[email protected]"))
        {
            Console.WriteLine("User 2 exists!");
        }
    }

    private static bool UserExists(IList myCol, string user)
    {
        return myCol.Contains(user);
    }
}

Upvotes: 0

JayHandle
JayHandle

Reputation: 186

Or you looking for something like this?

private bool UserExists(String user)
{
   // Test if a StringCollection is named "user"
   return user == Properties.Settings.Default.name ;
}

Upvotes: 0

Related Questions