1

asp.net C#4 I have a simple class to working with query strings.

A new instance is created like this:

        public QueryString(string querystring)
    {
        try
        {
            _table = new Hashtable();
            if (querystring.Length > 0)
            {
                foreach (string pair in querystring.Split('&'))
                {
                    string[] item = pair.Split('=');
                    _table.Add(item[0].ToLower(), item[1]);
                }
            }
        }
        catch (Exception)
        {
        }
    }

I want to add a method to this that will remove a key value pair. I don't want it to return a new querystring, I just want it to remove the pair from the current instance. Not sure how to do that since it says I can't assign a value to 'this'

        public void Remove(string key)
    {

        String querystring = this.ToString();
        try
        {
            _table = new Hashtable();
            if (key.Length > 0)
            {
                foreach (string pair in querystring.Split('&'))
                {
                    string[] item = pair.Split('=');
                    if (item[0] != key)
                    {
                        _table.Add(item[0].ToLower(), item[1]);
                    }

                }
                this = _table;
            }
        }
        catch (Exception)
        {
        }
    }
merk
  • 1,721
  • 5
  • 23
  • 39
  • Can't you just get the key from the parameter and call `_table.Remove()`? Also, you might consider moving to `Dictionary` for this, rather than `Hashtable`. – Jim Mischel May 15 '13 at 20:10
  • 3
    Slightly offtopic, but instead of rolling your own you could also use [`System.Web.HttpUtility.ParseQueryString(queryString)`](http://stackoverflow.com/a/1877016/266143), which returns a [`NameValueCollection`](http://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx) on which you can call methods like `Add()` and `Remove()`. The `ToString()` implementation of the class returns an encoded query string. – CodeCaster May 15 '13 at 20:41

3 Answers3

2

You're overcomplicating things. Since your class's state is made up of the _table field, all you need to do is remove the item with the given key from that field.

The following example replaces your untyped Hashtable wit a strongly-typed Dictionary. I also chose to initialize the dictionary with a LINQ statement, but you could keep your old code there if you prefer.

public class QueryString
{
    private readonly Dictionary<string, string> _table;

    public QueryString(string querystring)
    {
        if (querystring.Length > 0)
        {
            var pairs = 
                from pair in querystring.Split('&')
                let item = pair.Split('=')
                select new {key = item[0], value = item[1]};
            _table = pairs.ToDictionary(p => p.key, p => p.value);
        }
    }

    public void Remove(string key)
    {
        _table.Remove(key);
    }
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
0

You cannot assign a value to this since it is a reference to the object itself.

However, if you remove the line this = _table; , isn't things working as they should then? I guess your ToString() is somewhat using the hashtable to generate a "printer friendly" QueryString, and if that is the case, the way I see it, your Remove() method should be working (since you are replacing the _table variable with a new HashTable not including the key-value pair you want to exclude).

Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
0

you are passing a querystring into the class so the original querystring IS intact.

However you then break down the querystring into a a Hashtable of key/value pairs. If you want to keep THAT intact you need to clone the HashTable and perform the remove on the clone.

In any case it's probably a good idea to keep the querystring you are passing in as a constructor parameter in a member variable for safe keeping.

fnostro
  • 4,531
  • 1
  • 15
  • 23