0

I'm developing a class which contains some const strings

public static class Constants
{
    public const string CarID= "car_id";
    //public const string NumberID= "number_id"; // this is the second const string might be added, so 
    //the new created function can return the two

}

public class CarENParameters
{

    public string Params { get; set; }

    public CarENParameters(string carId)
    {
        Params = carId;
    }

}

public static class CarPropertyProcess
{
    //test params

    public static CarENProps Parse(Uri uri,string content)
    {

        string carID= Regex.Matches(content, @"\$\('#CarXL'\)\.val\((\d+)\)", RegexOptions.None)[0].Groups[1].Value;

        var parameters = new Dictionary<string, string>
        {
            {Constants.CarID, carID},        
        };

        return new CarENProps(uri.AbsoluteUri, parameters);
    }

    public static CarENParameters GetParameters()
    {
        return new CarENParameters(Constants.CarID);
    }
}

In the class Constants, I have one carID, now the case is it might have more than one const string like : public const string NumberID= "number_id";

So I want to create one function to return a list of those const strings, which are car_id and number_id with a class name CarENParameters but I havent figured out how to return a list by a get/set in a class, should I use dictionary or keyvaluespair to achieve that ? I'm quite new to C# so hope that I can have a better point of view from the helps of you guys. Thanks

2 Answers 2

1

Are you looking for something like this:

public static List<CarENParameters> GetParameters()
{
    return new List<CarENParameters>()
           {
                new CarENParameters(Constants.CarID1),
                new CarENParameters(Constants.CarID2),
                new CarENParameters(Constants.CarID3)
           }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use reflection for this

don't forget to put using System.Reflection;

        // get class type
        Type type = typeof(Constants);

        // get a list of fields
        FieldInfo[] fields = type.GetFields();

        List<CarENParameters> list = new List<CarENParameters>();

        // loop on field list
        foreach (FieldInfo field in fields)
        {
            // if field is a string add it to our return list
            if (field.FieldType == typeof(String))
                list.Add(new CarENParameters((String) field.GetValue(null)));
        }

6 Comments

thanks a lot for your example, I have done that and now I use another classs to print out the elements inside the list, however, as the List has an object of CarENParameters, and on the ApiController, I used IEnumerable<object> do you know how to extract all the elements with yield return new in this case ? this is my code : notepad.cc/share/vlyX7OyI90
"Unable to cast object of type 'System.Collections.Generic.List1[System.Object]' to type 'System.Collections.Generic.List1[COIUS.Overlook.BookingEngine.Common.Vincci.CarENParameters]'.",
You are trying to calst a list of Objects to a list of CarENParameters where this error occurs ? But you can just loop on the list of Objects an create the list of Ca‌​rENParameters
Yes I'm trying to loop all the elements inside the Objects to display them in JSON format on HTTP with yeild return new, and I had an error when I tried to cast it.
when I tried to extract all the IEnumerable objects and cast it with CarENParameters, I had an error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.