试图链接两个对象 [英] trying to link two objects

查看:46
本文介绍了试图链接两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个存储客户(名称,地址等)的表单。但是我在打印地址信息时遇到问题(姓名和姓名弹出就好了)。



这只是readinput方法,不确定这是问题所在。

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();
            adr.city = textCity.Text;
            adr.street = textStreet.Text;
            adr.ZipCode = textZip.Text;
            adr.country = (Countries)comboCountries.SelectedIndex;
            return adr;
        }



有很多代码,但我显然不能发布所有内容..如果有人对我可能做的事情有任何暗示我非常感谢。



编辑:我正在通过列表框打印答案。我也使用List来存储值。添加了ReadAddress()方法。


there is so much code but I obviously can''t post it all..so 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.

推荐答案

我们无法断言 - 假设ReadAddress有效,您提供的代码应该没问题。就个人而言,我不会使用out参数和bool,但会改为返回联系人或null:

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

(这些方法只是为了给你一般的想法)



从断点开始在ReadInput方法的第一行 - 然后逐步查看你的变量。如果你不能从那里解决,那么我们需要看相关的代码。

(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.


你没有提到你如何打印对象,但根据摘录,最有可能的原因是,当您打印对象时,您将打印所有属性。当属性使用本机数据类型但Address属性使用另一个对象(Address类的实例)时,这通常可以正常工作。



所以当你打印时,属性应该是循环的,如果属性的类型是类而不是本机数据类型,那么循环它''属性等等。



列出对象的属性,例如反射;请参阅 Type.GetProperties [ ^ ]方法。
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.


这篇关于试图链接两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆