面向对象问题C#
我有两个对象。其中一个拥有另一个对象。
User
-----
ID
Name
Surname
UserAddress
------------
ID
UserID
Address
当我更改第一个(用户)时,第二个(用户地址)中的该对象不会更改。我该如何修复它?
我正在将对象添加到哈希表中。当我需要列出我的数据时,我使用这个哈希表。当我更新对象时,我在哈希表中找到这个对象的索引。之后我更新。但是隐藏在第二个对象中的这个对象没有更新。
我正在从数据库中获取 userAddress-object 用户地址信息。
_userAddress= new userAddress();
_userAddress.ID = Convert.ToInt32(d["ID"]);
_userAddress.Address= d["Address"].ToString();
////I am taking user object here. _userAddress.UserID= (Address)_userDa.Detail(Convert.ToInt32(d["UserID"]), conStr);
当我更改用户对象时,用户地址对象中的同一用户对象不会更改。
I have two objects. One of them has the other object.
User
-----
ID
Name
Surname
UserAddress
------------
ID
UserID
Address
When I change the first one(User),This object in the second one(UserAddress), does not change. How can I fix it?
I am adding objects to hashtable. When I need to list my data, I use this hashtable.When I update the object, I find the index of this object in the hashtable.After that I update. But this object which is hided in the second object is not updated.
I am taking into userAddress-object user address information from database.
_userAddress= new userAddress();
_userAddress.ID = Convert.ToInt32(d["ID"]);
_userAddress.Address= d["Address"].ToString();
////I am taking user object here. _userAddress.UserID= (Address)_userDa.Detail(Convert.ToInt32(d["UserID"]), conStr);
When I change the User object, same user object in the userAddress object does not change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您正在尝试将 RDBMS 行为应用于对象。如果 UserID 字段在 C# 代码之外没有提供任何有用的用途,则应将其删除。
无论如何,将 User 作为属性添加到 UserAddress 类中。从 UserAddress 中删除 UserId。
It looks like you're trying to apply a RDBMS behavior to objects. If the UserID field offers no useful purpose outside the C# code, you should delete it.
In any event, add User as a property to the UserAddress class. Remove UserId from UserAddress.
听起来您正在以关系数据库的方式思考。
您的 User 对象应该有一个 UserAddress 对象作为属性(或者它们的集合,假设允许多个地址 - 因此需要这种关系)。
然后,您的数据访问实现应该负责对基础表(或您正在使用的任何存储机制)的更新
Sounds like you are thinking in a relational DB way.
Your User object should either have a UserAddress object as a property (or a collection of them, assuming multiple addresses are allowed - hence the need for the relationship).
Your data access implementation should then take care of the updates to the underlying tables (or whatever storage mechanism you are using)
ID是什么类型?
如果是值类型就不行。
如果它是引用类型,请确保您具有对同一 ID 对象的引用。
更好的方法是有参考文献 -
用户->用户地址
UserAddress->User
那么你可以这样做 - userAddress.User.ID
或用户 ID
获得相同的ID。
What type is ID?
If it's value type, it can't work.
If it's reference type, make sure you have a reference to the same ID object.
Better approach is to have references-
User->UserAddress
UserAddress->User
Then you can just do - userAddress.User.ID
or user.ID
to get the same ID.
除非这两个类相互引用,否则您不太可能看到更改。你可以有类似的东西 -
同样,UserAddress 应该引用 User 对象,而不仅仅是 ID。
Unless the 2 classes have a reference to each other, you are not likely to see the changes. You can have something like -
Likewise, UserAddress should have reference to the User object and not the ID alone.