troubleshooting Question

Public Classes in an API

Avatar of chenegar
chenegar asked on
C#ASP.NET
8 Comments2 Solutions226 ViewsLast Modified:
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);

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));

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));

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));[
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)

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
Arthur_Wood

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros