Click here to Skip to main content
13,352,770 members (40,580 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm currently trying to create a form which stores a customer (name, adress etc..) but the thing is I'm having trouble printing the address info(name and last name pops up just fine).

this is just the readinput method from, not sure if this is where the problem lies.
private bool ReadInput(out Contact contact)
           string fnameOK = " ";
           string lnameOK = " ";

           contact = new Contact();

           Address adress;
           //calls the readAdress method which sets the variables in
           //my address class
           adress = ReadAddress();

           contact.AddressData = adress;

           bool inOK = ReadName(out fnameOK, out lnameOK);

               contact.FirstName = fnameOK;
               contact.LastName = lnameOK;

               return inOK;

private Address ReadAddress(){
            Address adr = new Address();
   = textCity.Text;
            adr.street = textStreet.Text;
            adr.ZipCode = textZip.Text;
   = (Countries)comboCountries.SelectedIndex;
            return adr;

there is so much code but I obviously can't post it if anyone has the slightest hint at what I might be doing wrong I'd very thankful.

edit:I'm printing the answer through the listbox..I'm also using a List for storing values.Added the ReadAddress() method.
Posted 11-Jan-13 8:56am
Updated 11-Jan-13 13:31pm
Akbar Ali Hussain 11-Jan-13 14:11pm
what is there in ReadAddress() method?
Member 9749451 11-Jan-13 18:28pm
Well I could post it but the rules said to keep it as simple as possible...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

We can't tell from that - assuming that ReadAddress works, the code you give should be ok. Personally, I wouldn't use an out parameter and a bool, but would look at returning either a contact or a null instead:
private Contact ReadInput()
    Contact contact = new Contact();
    contact.AddressData = ReadAddress();
    return ReadName(contact) ? contact : null;

public Address ReadAddress() { return new Address(); }
public bool ReadName(Contact c)
    c.FirstName = "John";
    c.LastName = "Smith";
    return true;
(The methods are just stubs to give you the general idea)

Start by putting a breakpoint on teh first line of you ReadInput method - then step through and look at your variables as you go. If you can't work it out from there, then we need to see the relevant code.
nandushjkr 12-Jan-13 7:22am
still not working :( I'm starting to wonder if there's something wrong with my address class, these are my properties in my address class:

public string street { get; set; }
public string city { get; set; }
public string ZipCode { get; set; }
public Countries country { get; set; }
OriginalGriff 12-Jan-13 7:26am
What about the code to do the printing? What does that look like?
nandushjkr 12-Jan-13 7:52am
Well I use a listbox...listBox1.Items.AddRange(m_contacts.GetContactsInfo());
which calls this method
public string[] GetContactsInfo(){
string[] strInfoStrings = new string[m_contactRegistry.Count];
int i = 0;
foreach (Contact contactobj in m_contactRegistry){
strInfoStrings[i++] = contactobj.ToString();
return strInfoStrings;
OriginalGriff 12-Jan-13 8:27am
So what does the Contact.ToString override look like?
(You are overriding ToString, aren't you - because if you aren't it'll use the default which returns just the class name)
nandushjkr 12-Jan-13 8:35am
this is the ToString method in the contact class:
public override string ToString()
string strOut = string.Format("{0, -20} {1}", FullName, m_adress.ToString());
return strOut;
ToString method in Address class:
public override string ToString(){
string strOut = string.Format("{0, -25} {1,-8} {2, -10} {3}", m_street, m_zipCode, m_city, GetCountryString());
return strOut;
OriginalGriff 12-Jan-13 8:47am
All looks good - what is it printing?
nandushjkr 12-Jan-13 8:53am
Well It's supposed to print firstname, lastname street zipcode country.

but right now it's only printing firstname and lastname and the default value i've set for country. I've tried everything but I cant figure out what's wrong....
OriginalGriff 12-Jan-13 9:02am
Ok, so from your code either m_street, m_zipCode, m_city are all blank (possible, but you would have noticed, I suspect), or the whole Address has been replaced with a new, un-set one.
Try this: put a breakpoint on the line where you call ReadAddress. Use the "Step Over" button to let it do it's thing, and then look at where you stored the return value with the debugger. Is all the data there?

If it is, when you have stored it in the Contact add a line of code:
(obviously you will want your instance name in there)
That will call the ToString override for your Contact and you should see the data you wanted, including the Address data. What do you see?

(This may take a while, but debugging-by-email is never going to be quick :laugh:)
OriginalGriff 12-Jan-13 9:05am
Bah! Ignore the last message - I think I spotted it...
You do realise that you can't access the backing fields for automatic properties directly, don't you?
public string street { get; set; }
public string city { get; set; }
public string ZipCode { get; set; }
public Countries country { get; set; }
string strOut = string.Format("{0, -25} {1,-8} {2, -10} {3}", m_street, m_zipCode, m_city, GetCountryString());
are not compatible...use the property names instead!
string strOut = string.Format("{0, -25} {1,-8} {2, -10} {3}", street, ZipCode, city, GetCountryString());
And see what happens!
nandushjkr 12-Jan-13 9:31am
I just debugged...and the funny thing is the setter methods are getting the values I type in my fields but the instance varibles(m_street, m_zipcode etc..) are all empty....
OriginalGriff 12-Jan-13 9:34am
See my other comment half an hour ago!
nandushjkr 12-Jan-13 9:42am
aha so there's no need for fields if I'm using automatic properties?
OriginalGriff 12-Jan-13 10:04am
No! There is *DEFINITELY* no need - you can't access the backing fields of automatic properties directly so all you end up with is either two copies of the same data, or one set that is wrong. (As happened here)

BTW: "m_" is a C++ convention, that isn't used in C# as there is no concept of Global variables to get confused with (and hence no "g_" wither). Properties start with Upper case, backing fields are an underscore and the property name (but starting with lower case), and local variables start with lower case.
private int _myValue;
public int MyValue
get { return _myValue; }
set { _myValue = value; }
public void MyMethod(int myValue)
_myValue = myValue;
It may seem dull, but it saves a lot of confusion later!
nandushjkr 12-Jan-13 10:42am
Alright, Thanks alot for your help it's working now!
OriginalGriff 12-Jan-13 10:47am
You're welcome!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You didn't mention how you print the object, but based on the excerpt, the likeliest reason is that when you print the object, you print all the properties. This typically works fine when the property is using a native data type but the Address property uses another object, instance of the Address class.

So when you print, the properties should be looped and if the type of the property is a class instead of a native data type, then loop through it's properties and so on.

To list the properties of an object you can use for example reflection; See Type.GetProperties[^] method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180111.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100