是否可以在单独的文件中创建功能? [英] Is it possible to create function in separate file?

查看:82
本文介绍了是否可以在单独的文件中创建功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好CodeProject,



我正在使用VB.net 2010 express创建一个应用程序。我只使用1个表单作为主要表单,我发现代码行很长,对我来说非常小。所以我的问题是我可以在一个单独的文件中创建一个函数并在主窗体上调用它吗? (就像在java文件中创建方法并使用另一个java文件调用该方法一样)

Hi Codeproject,

Im creating an application using VB.net 2010 express. I only use 1 form as the main form and i found out that the line of codes is pretty long and its pretty missy for me. so my question is can i create a function into a separate file and call it on the main form? (just like creating a method in a java file and calling that method using the another java file)

推荐答案

是的,但更多信息会有所帮助。



0)您可以使用 partial 将现有类拆分为单独的文件。

1)你可以创建一个单独的类或模块。

2)但是这些都不会减少大型函数的代码味道 - 你能将它分成逻辑部分吗?
Yes, but more information would be helpful.

0) You could split the existing class into separate files by using partial.
1) You could make a separate class or module.
2) But neither of those will reduce the code smell of a large function -- can you split it into logical pieces?


让我们澄清:在vb.net中没有函数,只有类的方法。您不能将方法与类分开。但是您可以创建部分类,并将同一类的部分定义放在不同的文件中 - VisualStudio在创建表单时使用此功能来将代码与生成的部分分开。请参阅: http://msdn.microsoft.com/en-us/library/yfzd5350.aspx [ ^ ]

但请注意那里的评论:

Let''s clarify: in vb.net there are no functions, only methods of class. You can not separate a a method from the class. But you can make partial classes, and put the partial definitions of the same class in different files - VisualStudio is using this feature when creating forms to separate your code from the generated part. See: http://msdn.microsoft.com/en-us/library/yfzd5350.aspx[^]
But note the remark there:
引用:

在正常情况下,你不应该拆分开发跨越两个或多个声明的单个类或结构。因此,在大多数情况下,您不需要Partial关键字。

Under normal circumstances, you should not split the development of a single class or structure across two or more declarations. Therefore, in most cases you do not need the Partial keyword.

但是您最好重新考虑代码并将不同的功能提取到单独的类和/或单独的类库中。

But you would better re-factor your code and extract the different functionality into separate classes and/or separate class libraries.


这篇关于是否可以在单独的文件中创建功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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