面向对象问题C#

发布于 2024-10-15 04:18:07 字数 637 浏览 0 评论 0原文

我有两个对象。其中一个拥有另一个对象。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陈年往事 2024-10-22 04:18:07

看起来您正在尝试将 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.

奶气 2024-10-22 04:18:07

听起来您正在以关系数据库的方式思考。

您的 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)

栀子花开つ 2024-10-22 04:18:07

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.

笙痞 2024-10-22 04:18:07

除非这两个类相互引用,否则您不太可能看到更改。你可以有类似的东西 -

class User
{
   UserAddress Address {get;set;}
   //Other properties
}

User u = new User();
u.Address = new UserAddress();

同样,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 -

class User
{
   UserAddress Address {get;set;}
   //Other properties
}

User u = new User();
u.Address = new UserAddress();

Likewise, UserAddress should have reference to the User object and not the ID alone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文