一个被多次调用的函数应该放在哪里?
这是这个问题的后续如何避免重复的代码?
我正在使用使用 C# 的 ASP.NET 4.0,我有一个名为 FillDropDownList(DropDownList ddl, DataTable dt, string dataValueField, string dataTextField, string defValue)
的函数,该函数在我的 ASP.NET 页面之一上被多次调用填充一些下拉列表。我现在发现我有几个页面需要以完全相同的方式填充几个下拉列表。
我应该创建一个新类并将该方法放入该新类中,而不是将相同的代码复制并粘贴到不同的页面中吗?我该怎么称呼它?我应该给班级起什么名字?里面应该还有其他功能吗?我在想也许我应该将其命名为Util
?你会怎么办?
This is a follow-up to this question How to avoid repeated code?
I am using ASP.NET 4.0 with C# and I have a function called FillDropDownList(DropDownList ddl, DataTable dt, string dataValueField, string dataTextField, string defValue)
which is called multiple times on one of my ASP.NET pages to populate some dropdown lists. I am now finding that I have several pages where I need to populate several dropdownlists in exactly the same way.
Rather than copying and pasting the same code in different pages, should I create a new class and put the method in that new class? How would I call it? What should I call the class? Should it have other functions in it? I was thinking maybe I should call it Util
? What would you do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建静态类并在其中放置您的函数
或者您可以创建 DropDownList 的扩展(它也是一个静态类)
用法(如 DropDownList 的方法)
You can create static class and place there your function
Or you can create an extension to DropDownList (it is also a static class)
Usage (like method of DropDownList)