Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Parsing API Call data

Hello Experts,
I have an exiting program in C# which call an API.  The API returns more than 100 rows.  The C# program looks as below.

var memberFound = MemberSearch(Date);
if (memberFound != null)
	foreach (var member in memberFound)
	{
	    var list = new List<string>();
	    list.Add(member.memberNumber);
	}

Environment.Exit(0);

Open in new window


I am trying to parse the data in stored temporary in the member variable (or memberFound) which contains the following information.  The data stored looks like an Array, but not works like that.  I am trying to pass (member or memberFound) data to another method where I can parse the data and Insert to a table.

Here I have hard time understand var data type.  Please try to help.

memberCode = 1001                        
age = 32
name = Mark John
memberType = "xxx"
balanceAmount = 2450.00
.
.
.

Open in new window


Thank you very much in advance.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

var is used to implicitly declare a variable type, so the compiler effectively tries to guess the type of your variable. When you use var, you don't need to know the return type of a method. This can sometimes be a good thing and other times a bad thing! Looking at your code, we have no idea what datatype memberFound or member actually is.

A quick example: In your code, you are implicitly declaring the variable type:

var list = new List<string>();

This is the same as explicitly declaring the variable:

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

Without us knowing what member is, we can't really give you a solution. Judging by the fact that you call member.memberNumber, I'm guessing it's an object of some type.

Your code does look a little odd as you're declaring a new list in each iteration of your loop. It makes more sense to declare the list outside the loop and then add to it inside the loop:

var memberFound = MemberSearch(Date);
if (memberFound != null) {
    var list = new List<string>();

    foreach (var member in memberFound)
    {
         list.Add(member.memberNumber);
    }
}

Open in new window

Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Chris Stanyon, Thank you!

I agree that the code looks odd.  In fact this is not the whole code.  After this, another call is made to get the memberDetails.  This code is from a third party.  Please advice how to make it better.

The Data is like this
--------------------------------------
memberCode = 1001        - Integer                
age = 32                            - Integer
name = Mark John            - String
memberType = "xxx"         - String
balanceAmount = 2450.00 - Money

Now the question is, what is the best way to write this raw data into a table.  Please advice.

Thank you again for your help.
How you write it to a table will depend largely on how you've structured your app, particularly the data layer, so I can't give you a specific example. If you're wanting to insert all the member info into your table, then why not just pass in the results of the MemberSearch() method:

var membersFound = MemberSearch(Date);
InsertIntoYourDB(membersFound);

You'd need to write your InsertIntoYourDB() method to accept the results of the MemberSearch, and use that to insert into your DB however you've set up your data layer.
How my method should look like?  I am trying as below.  But getting errors.

private void InsertIntoYourDB(var membersFound)
{
}

Error      CS1503      Argument 1: cannot convert from 'System.Collections.Generic.IList<MemberAPIApp.Program.Memo>' to 'var'      MemberManager      
Error      CS0825      The contextual keyword 'var' may only appear within a local variable declaration or in script code      MemberManager
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
IMO, the reason why var is confusing is that var was originally created to support LINQ queries, where in your query you can declare a new, anonymous type:

e.g.

List<int> someInts = new List<int>() { 1, 1, 2, 3, 5, 8, 13 };
var fibonaccis = from value in someInts
                 select new
                 {
                     Value = value,
                     IsEven = (value % 2 == 0),
                     IsPowerOfThree = (value % 3 == 0),
                 };

foreach (var fibonacci in fibonaccis)
{
    Console.WriteLine($"Value {fibonacci.Value} is {(fibonacci.IsEven ? "even" : "odd")} and {(fibonacci.IsPowerOfThree ? "is" : "is not")} a power of three.");
}

Open in new window


In the above, the select creates a new, anonymous type which has three properties:  Value, IsEven, and IsPowerOfThree. I haven't declared a class or struct which defines this type...the compiler will do that. Since the compiler creates the type, I don't know what the name of the type is. However, if I use var, then I can let the compiler fill in the type for me (at compile time). And my code will just work.

Now, the reason people use var, in my experience, is because:

  • It's just three letter to type
  • You can change the type of the variable by simply adjusting the right side of the equals
  • They come from a Javascript environment...which just LOVES var to pieces
  • They claim that it forces you to name your variables better

The first and second points above are unarguable--they're just facts. The third point is me taking a jab at Javascipt developers, but I think it's true to some degree. The fourth point I take issue with:  If you learn to name your variables well as a matter of practice, then it doesn't matter if you use var of the actual type.

The biggest problem I have with people who (ab)use var is that it's easy to get carried away. As Chris Stanyon mentioned, you can easily use var to capture the result of a function call. But as a maintainer, I now have to hover over the variable or the function call in order to determine the type of the variable. This is not good for maintenance. I should be able to glance at a piece of code and, in short order, determine what that code does by the way it is written.
Chris Stanyon,
Now how to retrieve the rows from membersFound in the method InsertIntoYourDB?

Ex. memberCode, age, name, memberType, balanceAmount etc.

Thank you!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris Stanyon, Thank  for your excellent help.  It worked like a charm.  Thank you again!

Hello kaufmed, Your post was useful, but for my current issue Chris Stanyon's resolution worked the best for me.  Thank you again.