Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2009, 12:37 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Is there a better way to do this?

Hi

I am doing some paypal stuff. PayPal returns big long string with values called NVP values.

they look like this

FIRSTNAME=Robert&MIDDLENAME=Herbert&LASTNAME=Moore

In one of the responses theoretically 40 fields can be returned as an array.

So It would look like something like this(in paypals api they have an array looking thing that can call indexes up by number or name)

["Firstname"] = Robert;

So I want to place all these variables in some properties so it is easier to grab and use these returned response fields.

so I probably have:


public static string FirstName { get; set;}

But not the problem is I don't know how to put them into these properties.

The only way I can think of is go through this how array and using a switch statement and start putting them where they belong but of course that will be pretty long switch statement.

The only other way I can think of is a big if/else statements but that is even worse.

Is there a better way?
 
Old February 13th, 2009, 01:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there.. I will think in vb, but this obviously the same (with a little code change). You have a collection object that is can be accesed by key. What if you fill that collection using the parameter name as key and the value as value. This way, you avoid the if or select case.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old February 14th, 2009, 01:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Seems like the trick wold be figuring out how to get an enumerator for the "array looking thing". Then you could just use a foreach statement to enumerate it, and load the key/value pairs into a Dictionary<TKey, TValue> or similar class.

For example, if you had a text file formatted like:

FIRSTNAME=Robert
MIDDLENAME=Herbert
LASTNAME=Moore

you could load a Dictionary with:

Code:
var nameValueList = newDictionary<string, string>();
foreach (var line inFile.ReadAllLines("ArrayLookingThing.txt")) // reads file from bin/debug
   nameValueList.Add(line.Split('=')[0], line.Split('=')[1]);
Console.WriteLine(nameValueList["FIRSTNAME"]);
File.ReadAllLines returns a string array which implements IEnumerable.

Just don't know enough about the data structure you're accessing. Can you iterate over it some how? Or maybe just parse the NVP string to a text file or other format that you can invoke an enumerator on, as above.

Bob
 
Old February 14th, 2009, 01:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Code:
classProgram
{
 staticvoid Main(string[] args)
 {
    var nameValueList = ParseNVPString  ("FIRSTNAME=Robert&MIDDLENAME=Herbert&LASTNAME=Moore");
    Console.WriteLine("{0} {1} {2}", nameValueList["FIRSTNAME"],
       nameValueList["MIDDLENAME"], 
       nameValueList["LASTNAME"]);
    Console.Read();
}
publicstaticDictionary<string, string> ParseNVPString(string NVPString) 
{
  string[] kayValuePairs = NVPString.Split('&');
  var nameValueList = newDictionary<string, string>();
  foreach (var keyValuePair in kayValuePairs)
  {
     nameValueList.Add(keyValuePair.Split('=')[0], keyValuePair.Split('=')[1]);
  }
  return nameValueList;
  }
}
 
Old February 16th, 2009, 09:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Just playing around with a LINQ to Objects version to:

Code:
staticvoid Main(string[] args)
{
  Dictionary<string, string> dictionary = 
              LinqParseNVPString("FIRSTNAME=Robert&MIDDLENAME=Herbert&LASTNAME=Moore");
 
  Console.WriteLine("{0} {1} {2}",
                     dictionary["FIRSTNAME"],
                     dictionary["MIDDLENAME"],
                     dictionary["LASTNAME"]);
  Console.Read();
}
public static Dictionary<string, string> LinqParseNVPString(string NVPString) 
{
  string[] keyValuePairs = NVPString.Split('&');
  Dictionary<string, string> dictionary = 
                               (from kvp in keyValuePairs
                               select new
                               {
                                  Key = kvp.Split('=')[0],
                                  Value = kvp.Split('=')[1]
                               }).ToDictionary(a => a.Key, a => a.Value);
  return dictionary;
}
 
Old February 16th, 2009, 09:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Here's the chained method call syntax instead of the query expression syntax:

Code:
publicstaticDictionary<string, string> LinqParseNVPString(string NVPString)
{
return NVPString.Split('&')
               .Select(k => new { Key = k.Split('=')[0], Value = k.Split('=')[1] })
                .ToDictionary(k => k.Key, k => k.Value);
}   









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.