当使用在.net中的共享方法 [英] When to use Shared methods in .net

查看:170
本文介绍了当使用在.net中的共享方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点越来越复杂的信息关于这个,所以我希望有人能清楚这件事对我来说。

我应该使用共享的方法/在下列情况下功能:

我有一个名为Person的一个泛型类。此类重新presents一个人在数据库中。

我有一个名为的PersonManager经理级。这个类包含的方法而添加,更新,删除单个Person对象。一种方法,也存在查找从数据库中的人。

如果在管理器类这些方法被声明为共享的方法?或者是创建的PersonManager类的新实例每次并调用相应的方法就可以了比较合适的。

所以,如果共享的:

  PersonManager.AddPerson(NewPerson)
 

与非共享的:

 暗淡MyPersonManager作为的PersonManager
MyPersonManager.AddPerson(NewPerson)
 

当仰视的人,共享的版本是:

 暗淡DT作为新的DataTable
DT = PersonManager.GetPersons
 

与非共享的版本:

 暗淡DT作为新的DataTable
昏暗MyPersonManager作为新的PersonManager
DT = MyPersonManager.GetPersons
 

解决方案

由于共享的方法和成员称为静态在C#中已经有东西在计算器...

<一个href="http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c">http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c

I'm kinda getting mixed messages about this so I'm hoping someone can clear this up for me.

Should I be using Shared methods/functions in the following situation:

I have a generic class named "Person". This class represents a person in the database.

I have a manager class named "PersonManager". This class contains methods which adds, updates, deletes individual Person objects. A method also exists to lookup Persons from the database.

Should these methods in the manager class be declared as shared methods? Or is it more appropriate to create a new instance of the PersonManager class each time and call the appropriate method on it.

So, if shared:

PersonManager.AddPerson(NewPerson) 

versus non-shared:

Dim MyPersonManager as PersonManager
MyPersonManager.AddPerson(NewPerson)

When looking up Persons, the shared version would be:

Dim dt as New DataTable
dt = PersonManager.GetPersons

versus the non-shared version:

Dim dt as New DataTable
Dim MyPersonManager as New PersonManager
dt = MyPersonManager.GetPersons

解决方案

Since shared methods and members are called static in c# there is already stuff on stackoverflow...

http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c

这篇关于当使用在.net中的共享方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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