Click here to Skip to main content
15,846,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Updated 11-Jan-13 13:31pm
Akbar Ali Hussain 11-Jan-13 14:11pm    
what is there in ReadAddress() method?
[no name] 11-Jan-13 18:28pm    
Well I could post it but the rules said to keep it as simple as possible...

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.
Share this answer
[no name] 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?
[no name] 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)
[no name] 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;
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.
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900