Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7816061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:43:52+00:00 2026-06-02T05:43:52+00:00

I have a web service which receive a string. This string contains multiple keys

  • 0

I have a web service which receive a string.

This string contains multiple keys => values which are concatenated with the character ‘+’.

I must validate each value (“required”, “not empty”), and assign each to a variable with the same name.

Here is how I build the Dictionary from the string :

string firstname;
string lastname;
string amount;

string request = "firstname=foo+lastname=bar+amout=100.58";

Dictionary<string, string> arguments = new Dictionary<string, string>();

request.Split('+').ToList<string>().ForEach(p =>
{
    string[] tmp = p.Split('=');

    if (tmp.Length == 2)
        arguments.Add(tmp[0], tmp[1]);
});

// Validate and assign : How I do with one value : (I must find a better way)
bool isValid = true;

// check "firstname"
if(arguments.ContainsKey("firstname") && string.IsNullOrWhiteSpace(arguments["firstname"]) == false)
{
    firstname = arguments["firstname"];
}
else
{
    isValid = false;
    Logger.Write("Invalid argument : firstname");
}

// Do this for about 20 arguments, it becomes huge...

if(isValid)
{
    Console.WriteLine(firstname); // Displays foo
    Console.WriteLine(lastname); // Displays bar
    Console.WriteLine(amout); // Displays 100.58
}

Thanks, and sorry for spelling mistakes, I’m French.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:43:54+00:00Added an answer on June 2, 2026 at 5:43 am

    I think you want something like this, but since you didn’t acutally ask a question I’m just guessing:

    request.Split('+').ToList<string>().ForEach(p =>
    {
        string[] tmp = p.Split('=');
    
        if (tmp.Length == 2 && !string.IsNullOrWhiteSpace(tmp[1]))
        {
            // edit - if your string can have duplicates, use
            // Dictionary<U,K>.ContainsKey(U) to check before adding
            var key = tmp[0];
            var value = tmp[1];
    
            if(!arguments.ContainsKey(key))
            {
                arguments.Add(key, value);
            }
            else
            {
                //overwrite with new value
                //could also maybe throw on duplicate or some other behavior.
                arguents[key]=value; 
            }
        }
        else
            throw InvalidOperationException("Bad dictionary string value");
    });
    

    Also, I would question the use of ToList->ForEach if this was in front of me in a code review. You want to avoid side effects in Linq, I would write it with a traditional foreach like:

    var itemValues = request.Split('+');
    foreach(var item in itemValues)
    {
        string[] tmp = item.Split('=');
    
        if (tmp.Length == 2 && !string.IsNullOrWhiteSpace(tmp[1]))
            arguments.Add(tmp[0], tmp[1]);
        else
            throw InvalidOperationException("Bad dictionary string value");
    });
    
    
    
    // Validate and assign
    //read values from the dictionary
    //use ContainsKey to check for exist first if needed
    
    Console.WriteLine(arguments["firstname"]); // Displays foo
    Console.WriteLine(arguments["lastname"]); // Displays foo
    Console.WriteLine(arguments["amout"]); // Displays 100.58
    

    Edit 2 – You should encapsulate the logic in a method:

    private string TryGetValue(IDictionary<string,string> dict,string key)
    {
        string value = null;
        if(dict.ContainsKey(key) && !string.IsNullOrWhiteSpace(dict[key]))
        {
            value = dict[key];
        }
        else
        {
            Logger.Write("Invalid argument : " + key);
        }
        return value;
    }
    

    Now you can say:

    string firstName = TryGetValue(arguments,"firstname");
    string lastName= TryGetValue(arguments,"lastName");
    string amount = TryGetValue(arguments,"amount");
    
    bool isValid = firstName!=null && lastName != null && amount != null;
    
    if(isValid)
    {
        Console.WriteLine(firstName ); // Displays foo
        Console.WriteLine(lastName); // Displays bar
        Console.WriteLine(amout); // Displays 100.58
    }
    

    TryGetValue would make an excellent extension method:

    public static class Extensions
    {
        public static string TryGetValue(this IDictionary<string,string> dict, string key)
        {
            string value = null;
            if(dict.ContainsKey(key) && !string.IsNullOrWhiteSpace(dict[key]))
            {
                value = dict[key];
            }
            else
            {
                Logger.Write("Invalid argument : " + key);
            }
            return value;
        }
    
    }
    

    Now the calling code would look like:

    string firstName = arguments.TryGetValue("firstname");
    string lastName= arguments.TryGetValue("lastname");
    string amount = arguments.TryGetValue("amount");
    

    Last edit –
    A note on extention methods – Yes they are neat, but it’s also easy to accidently get in a bad situation overusing them. Read up on msdn and blogs about them, follow the guidelines. Avoid extensions on generic types like object, string ect.

    In my projects I always lay out extension methods in distinct namespaces depending on the type they interact with, which forces classes that wish to use them to be very explicate about it like:

    namespace Extensions.IDictionary { ... }
    namespace Extensions.string { ... }
    namespace Extensions.SomeType { ... }
    namespace Extensions.IList { ... }
    

    and consuming code would have using clauses to match:

    using Extensions.IDictionary;
    

    to pull in just the extensions your interested in, no more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a restful web service which can deal with DTOs in json format
I have a web service in which I am manipulating POST and GET methods
I have a web service to which users upload python scripts that are run
I have a ReSTful web service which needs to parse locale-sensitive data from the
I have a xml web service which I would like to track using Google
I have created a web service which is saving some data into to db.
I have created a web service which takes a username and password as parameters
I have a Java web-service which currently runs on a local tomcat server. I
I have a web service in which I created an enum.. i have a
I need to call a web service to receive a JSON object which I'll

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.