Link to home
Start Free TrialLog in
Avatar of chenegar
chenegar

asked on

Public Classes in an API

I'm a complete newbie when it comes to C#, ASP.NET, and Visual Studio 2012. I'm not even sure I know how to ask this question, but I'm going to forge ahead. I'm not a programmer, either, but I know the basics of quite a few languages, although C# is the first OOP language I've worked with and I'm still wrestling with the basic precepts of it.

We have an application written in C# that takes data from a text file and imports it to a SQL database. The application uses an API provided by the vendor. If understand it correctly, an API is a set of definitions for classes, methods, properties, etc. that lets you use those definitions without having to start from scratch. The API is accessed by adding it in the using statement at the beginning of the program.

I have the documentation for the API, so I know how the various classes are defined.

I was doing OK until I hit a problem I can't find a way around and I'm sure it must be because my knowledge of this is so spotty.

One of the things the import program does is add an address to a person's record. A feature of the SQL database is a city table that includes the city name, state, county, and zip code, along with an ID field. In the first version of the import, the program added a new record to the city table every time an address was added to a person's record. The code that did this is:
LFCity city = new LFCity(user, userhomecity, userstate, Country.UNITEDSTATES);
city.County = userhomecounty;
city.Zip = userzipcode;

LFAddress useraddr = nc.Addresses.AddUSAddress(userhomeaddress1, city);

Open in new window


I hate having the city table get cluttered up with duplicate records, so I changed the program to use the existing city record in the address rather than create a new one. Here's how I did it:
LFIdentifier cityId = new LFIdentifier(usercityid);
LFAddress useraddr = nc.Addresses.AddUSAddress(userhomeaddress1, LFCity.GetCityById(entuser, cityId));

Open in new window


Works great. LFCity.GetCityById is a method within the LFCity class.

Then I moved on to the next type of person in the import. The first type of person is identified in the code above as "user" and there is only ever one user in the file.

The second type of person that gets imported is an "op," of which there can be more than one. Thinking I knew what I was doing, I created this code:
LFIdentifier cityId = new LFIdentifier(opcityid);
LFAddress opaddress = nc.Addresses.AddUSAddress(opaddress1, LFCity.GetCityById(entuser, cityId));

Open in new window


I get this error: "a local variable named 'cityId' cannot be declared in this scope because it would give a different meaning to 'cityId', which is already used in a 'parent or current' scope to denote something else."

It makes sense to me that cityId can't be used again. So I changed the code to:
LFIdentifier opcityId = new LFIdentifier(opcityid);
LFAddress opaddress = nc.Addresses.AddUSAddress(opaddress1, LFCity.GetCityById(entuser, opcityId));[

Open in new window

This code doesn't cause an error, but it doesn't work, either. The address isn't created on the op record, period.

My next thought was to create a private method that would get the cityId then pass it back to create the address. Here's how the method is defined:
public static LFCity GetCityById(
LFUser user, 
LFIdentifier cityId)

Open in new window


I've thoroughly confused myself at this point because I need the GetCityById method to create the address without adding a new record to the city table.

Can someone help me understand where I need to go from here?
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America 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
Avatar of chenegar
chenegar

ASKER

Arthur, I was really hoping your suggestion would work. It's simple and makes sense. But no, it didn't work. I didn't get an error but I didn't get the address, either.
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
glad to be of assistance.  Without seeing all your code, I could only take a wild guess.  Good that it got you looking more closely at your code.  

AW
chenegar>> you can award points yourself, and not have tio wait for a Moderator to award the points according you your spec.
I thought I did that.
It appears that you requested that the question be closed, which is different from awarding points.

AW
I found the fix to my problem, but Arthur put be on the right track. There's often something to be learned even in a solution that doesn't work.