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?
C#ASP.NET

Avatar of undefined
Last Comment
chenegar

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Arthur_Wood

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
chenegar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Arthur_Wood

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
Arthur_Wood

chenegar>> you can award points yourself, and not have tio wait for a Moderator to award the points according you your spec.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
chenegar

ASKER
I thought I did that.
Arthur_Wood

It appears that you requested that the question be closed, which is different from awarding points.

AW
chenegar

ASKER
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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.