相结合的方法 [英] combining methods

查看:106
本文介绍了相结合的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者所以请原谅我的无知。

I am a beginner so please forgive my ignorance.

在我的应用我有很多的复制和粘贴code那是完全一样的,并执行完全一样的功能(按钮单击事件等)。这些冗余code住在我的许多网页的code-后面。所以我决定减少code重复,这些方法移动到一个类文件,并只对它们作出调用code隐藏页。

In my app I have a lot of copy and paste code that is exactly the same and performs exactly the same function (button click events and the like). These redundant code live in the code-behind of many of my pages. So I decided to reduce the code duplication and to move these methods into a class file and only make a call to them from the code-behind pages.

下面是我的code按钮单击事件背后调用类文件中的方法的例子:

Here is an example of a button click event in my code behind calling the methods from a class file:

#region DELETE selected users - button

protected void btnDeleteSelected_Click(object sender, EventArgs e)
{
    try
    {
        UserGvUtil.DeleteSelectedUsersAndProfiles(GridView1, Msg);
    }
    catch (Exception ex)
    {
        UserGvUtil.ExceptionErrorMessage(Msg, ex);
    }
    finally
    {
        UserGvUtil.RefreshGridView(GridView1);
    }
}

#endregion

我的问题是,我可以将这个try / catch块到另一种方法,并将其移动到同一个类文件?所以,我在点击事件的唯一的事情是code一行。

My question is, can I combine this try/catch block into yet another method and move it to the same class file? So, the only thing I have in the click event is a single line of code.

是否有意义做到这一点?不知道为什么,但我想有文件作为清洁,简单越好,所以我可以让所有的编辑在一个地方我的背后code。

Does it make sense to do this? Not sure why, but I would like to have my code behind files as clean and simple as possible so I can make all the edits in a single place.

很抱歉,如果我没有任何意义。我只是学习有关的类和方法,它淹没我的头,有很多的想法。

Sorry if I make no sense. I'm just learning about classes and methods and it floods my head with lots of ideas.

推荐答案

您可以将里面的东西了尝试块到您传递给一个共享的方法,匿名委托有一个try / catch。你真的不需要把刷新到最后,虽然。其实,我倒觉得你只需要运行它,如果尝试块成功。

You can move the stuff inside the try block into an anonymous delegate that you pass to a shared method that has a try/catch. You really don't need to put the refresh into the finally, though. In fact, I would think you would only want to run it if the try block succeeds.

这篇关于相结合的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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