如何将对象从一个类传递到另一个类 [英] How do I pass an object from one class to another

查看:146
本文介绍了如何将对象从一个类传递到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我是C#的新手。我有一个应用程序调用DataBaseIO类方法来读取SQL数据库表。此方法创建User对象的新实例,并使用从输入表中读取的数据填充实例。在调试中我可以看到实例使用正确的数据更新。



但是当控件从此DataBaseIO方法返回到调用方法时,该对象为null。调用方法在源Transaction.cs中,对象在DataBaseIO.cs中创建。如何将对象传递回Transaction.cs中的调用方法?



问候

Pat

Hello,
I am new to C#. I have an application which calls a DataBaseIO class method to read through an SQL database table. This method creates a new instance of a User object and populates the instance with data read from the input table. In debug I can see that the instance is updated with the correct data.

However when control returns from this DataBaseIO method to the calling method ,the object is null. The calling method is in source Transaction.cs and the object is created in DataBaseIO.cs. How can I pass the object back to the calling method in Transaction.cs ?

regards
Pat

推荐答案

使DataBaseIO类中方法的返回值将它创建的用户对象返回到Transaction类中方法的调用站点。



如果你可以在DataBaseIO中调用方法,就像你指出的那样,对我来说,你的Transaction类有一个有效的引用。



或者,如果您不希望公开整个用户类型对象,则可以在DataBaseIO类中创建访问器方法,以便有选择地在User Type对象中公开您希望公开的内容。
Make the return value of the method in the DataBaseIO class return the User Object it creates to the call-site of the method in the Transaction class.

If you can call method in DataBaseIO, as you indicate, that means, to me, that your Transaction class has a valid reference to it.

Or, if you don't wish to expose the "whole" User Type object, you could create accessor methods in the DataBaseIO class that selectively exposed what you wished to expose in the User Type object.


这篇关于如何将对象从一个类传递到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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