类模块比普通模块有什么好处? [英] What are the benefits of class modules over plain modules?

查看:68
本文介绍了类模块比普通模块有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究VBA for Access,最新一章是关于类模块的。到目前为止,我认为我对如何创建一个(至少对于初学者)有很好的把握。但是,我坚持认为,与普通模块相比,类模块有什么好处?我想我需要了解这一点,知道何时使用每一个。我得到的类模块是基于每个实例运行的,允许相同的代码用于不同的实例。我能想到它将被使用的唯一时间是Recordset类。既然已经创建了这个,那么还能使用这种能力吗?我在网上搜索过,发现那些学过课程模块的人一直都在使用它们。有人说程序越复杂,就越有可能在课堂模块中做得更好。给出了一个例子,比较了编写一行代码而不是50代。在他谈论的上下文中,50行代码进入了类模块,一行是调用它时(或实例化,无论哪个是正确的)术语)。但是,常规模块中的公共函数/ subs具有相同的能力。在我的书中找到的例子被简化到了我不明白为什么经历麻烦学习课程模块,因为函数或子函数可以完美地工作。


我知道课堂模块必须有充分的理由,但到目前为止我还没有弄清楚。我不需要知道如何使用它们,只是为什么。

I have been studying VBA for Access, and the latest chapter was on class modules. So far, I think that I have a pretty good grasp on how to create one (at least for a beginner). However, the point that I''m stuck on, is what benefits are there to class modules over plain modules? I think that I need to understand this to know when to use each one. I get that class modules are run on a per instance basis, allowing the same code to be used for different instances. The only time that I can think of where this would be used would be the Recordset class. Since this one is already created, where else would this ability be used? I have searched online and found that the people who have learned class modules, use them all the time. One person said that the more complex the program, the more likely it would be better done in a class module. An example was given that compared writing one line of code instead of 50. In the context that he was talking, the 50 lines of code went into the class module and the one line was when it was called (or instantiated, whichever is the proper term). However, public functions/subs in regular modules have this same ability. The examples found in my book were simplified to the point that I don''t understand why go through the hassle to learn class modules since a function or a sub would work perfectly.

I know that there has to be a good reason for class modules, but so far I haven''t been able to figure it out. I''m not needing to know how to use them, just why.

推荐答案

我肯定在输入这个时,我会用有人;因此,如果我报道一些相同的材料,请原谅我。希望我与以下内容的距离不会太远。


警告Emptor

请记住,我首先是贸易的化学家和大量的数学和工程教育;计算机科学是一种爱好,所以我可能会误解CS-Theory的这种应用。然而,正如任何真正的爱好者一样,我努力理解并使用最好的方法和技巧。


-

我知道你已经阅读了很多已经;但是,您可以阅读此链接课程简介

了解课程( VB2005)我知道,它是Visual Basic vs. VBA;但是,我认为在这种情况下,一般理论是相关的。


我希望以下内容将打开 VBA开发者手册作者:Ken Getz,Mike Gilbert - 为什么使用班级模块 - 第273页我在家里有这本书;然而,发布整个部分并不完全合法 - 但作为Google的摘录。



MS Access中的一个实际例子是表格。 />
它们是一个类模块。他们坐在数据库中;但是,在加载之前不占用任何内存。它们有一组易于程序员处理的控件,你可以打开相同的表单6次,信息略有不同......彼此独立,但是,如果你允许的话,信息可以提供彼此和其他形式。如果你真的很敏锐,那么每个表格都可以添加到用户定义的集合中;然而,我没有必要这样做......但玩起来很有趣。


我会把课程用作临时表作为手段通过将每个记录添加为类集合的成员来解决我应该作为记录事务处理的问题。也许不是最好的答案,但它有效,如果表被锁定,我可以保存记录,直到可以输入整个事件。


我用它们作为用户配置文件...基本上,启动时,启动屏幕将设置用户对象,请求密码,检查数据库,验证用户等。

因为我打开此表单但是隐藏了,用户类保持不变(我还在常规模块中放置了一个全局来保持用户对象的活着;但是,我已经读过这种做法并不是真正的正确方法)无论如何当我正在打开一个表单,我可以使用构造zobjusr.userstat。并且该信息就在那里,而不必再次解密信息。 (我加密用户名,使用密码的摘要等等。因此,通过在zobjusr对象中保存该信息,代码不必为每个表单或每次我想要重新运行对用户进行身份验证...(我还有一个程序,当代码命中时调用(zobjuser.askpinagain),调用正确的表单,剔除信息,传递消化并与当前进行比较 - 现在可能有在标准模块中编写很多代码并调用每个代码,存储在全局变量等中 - 最好的是,如果没有大量的工作,任何人都无法直接获得明文。


或与客户我可以做类似的实验室结果,po数字,你有什么。


有一件事要问:你应该什么时候创建自定义对象?当然这是一个MSOfficeXP参考...但问题似乎仍然有效。


我确定我错过了Neopa,Rabbit或其中一个人会帮忙回答的问题。
I''m sure that while typing this, I''ll cross post with someone; thus, please forgive me if I cover some of the same material. Hopefully, I will not be too far off the mark with the following.

Caveat Emptor
Keep in mind, I am first and foremost a Chemist by trade and education with a heavy dose of Mathematics and Engineering; Computer Science is a hobby so I may stray a bit with this application of CS-Theory. However, as with any true hobbyist, I strive to understand and use the best methods and techniques.

-
I know you''ve read a lot already; however, you might read this link Introduction To Classes

Understanding Classes (VB2005) I know, it''s a Visual Basic vs. VBA; however, I think in this case the general theory is relevant.

I hope the following will open VBA Developer''s Handbook By Ken Getz, Mike Gilbert - Why Use Class Modules - Page 273 I have this book at home; however, not exactly legal to publish an entire section - but as an excerpt via Google.



One practical example in MS Access is the forms.
They are a class module. They sit in the database; however, take up no memory until loaded. They have a collection of controls that are easy for the programmer to handle, you can open the same form 6 times, with slightly different information... all independent of each other, and yet, if you allow it, the information can be available to each other and to other forms. If you''re really cleaver, each form instituted can be added to a user defined collection; however, I''ve not had the need to do that... but it was fun to play with.

I''ll use classes sometimes as a temporary table as a means to get around issues I''ve had with what should be handled as a record transaction by adding each record as a member of the class collection. Maybe not the best answer however, it works, and if the tables are locked, I can hold the records until the entire event can be entered.

I''ve used them as a user profile... basically, upon start, the splash-screen will institute the user object, ask for the password, check the database, validate the user and the like.
Because I leave this form open but hidden, the user class stays (I''ve also placed a global in a regular module to keep the user object alive; however, I''ve read that this practice isn''t really the correct way) In any case when I''m opening a form I can use the construct "zobjusr.userstat" and the that information is right there without having to decrypt the information again. (I encrypt the user names, use a digest for the password, and so forth. Thus, by having that information held in the zobjusr object the code doesn''t have to be re-ran for every form or every time I want to authenticate a user for... (I also have a procedure that when called (zobjuser.askpinagain) when the code hits this, the correct form is called, the information culled, handed of for digest and compared against the current - now could have code a lot of that in standard modules and called each one, stored in global variables and the like) - best, is that the clear text is not directly available to anyone at any time without some extensive work.)

or with a customer I can do similar things for Lab results, po numbers, what have you.

One thing to ask: When Should You Create Custom Objects? Ofcourse this a MSOfficeXP reference... but the question still seems valid.

I?m sure I?m missing something here that Neopa, Rabbit, or one of the others will help answer.

这真的很难回答。


像我一样来自程序编程背景(当我接受早期教育时,他们没有OO。)我发现我可以按照我的需要完成我想要的大部分工作。我怀疑你已经做得很好,意识到在课堂上完成的许多事情也可以通过程序来完成。当事情变得复杂时,我怀疑课程会更多。


也就是说,任何具有相关数据结构的东西都可以使用类更容易地处理。结构也是可能的,但是完整的类包括结构化数据的概念,并且比这更加灵活和强大。


我曾经构建一个类来处理调用表单这样,无论哪种形式称为另一种形式都可以隐藏在调用点,然后在关闭类中嵌入的表单时再次显示。它工作得很好,但除了那个以及数据库本身的对象结​​构之外,我很少需要考虑我工作中的类。


否则,我只会指引你回Z的答案(我认为这非常有帮助),特别是最后一个链接,并说我怀疑熟悉课程将是一个糟糕的举动。虽然使用程序可以做很多事情,但事实恰恰相反。可能更是如此。如果可以的话,和他们一起玩。您可能会及时发现他们为开发人员提供的更容易的事情。此外,熟悉这些概念将使我更容易过渡到其他现代计算机语言。
This is really hard to answer.

Coming, as I do, from a procedural programming background (They didn''t have OO when I got my early education.) I find I can do most of what I want that way as well as I need. I suspect you have done well to realise that many things done in classes could also be done with procedures. I suspect that classes come into their own more when things get complicated.

That said, anything with an associated data structure could probably be handled more easily using a class. Structures are also a possibility, but a full class includes the concept of structured data and is a lot more flexible and powerful than just that.

I did once build a class to handle calling forms such that whichever form called another form could be hidden at the point of calling and then made visible again when the form embedded within the class was closed. It works perfectly, but other than that and the object structure of the database itself, I rarely need to think about classes in my work.

Otherwise, I would just direct you back to Z''s answer (which is pretty helpful I thought), and particularly the last link, and say that I doubt getting acquainted with classes would be a bad move. While it''s true that much can be done using procedures - the reverse is also true. Possibly more so. Play with them if you can. You will probably discover in time the sort of things that they make easier for the developer. Also, a familiarity with the concepts will make transition to other modern computer languages that much easier i suspect.


我觉得Class有一个很大的优点就是编码的复杂性实际上是封装在Class本身内,并与最终用户屏蔽。从本质上讲,他们没有必要知道他们如何工作。可以很容易地利用类的属性和方法,而不必知道它们的复杂性。所有需要知道的是属性/方法的名称以及需要为这些方法传递的任何特定参数。
One big advantage that I feel a Class has is that the complexities of the Coding are actually encapsulated within the Class itself and is shielded from the End User. In essence, they have no need to know ''How They Work''. Properties and Methods of the Class can be easily utilized without having any idea of their complexity. All that needs to be known are the Names of the Properties/Methods and any specific Arguments needed to be passed for those Methods.


这篇关于类模块比普通模块有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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