Visual Basic 中的共享变量和 DLL 使用问题。范围混乱
我正在尝试将一些先前编写的代码拆分到 DLL 中。这是一个简单的记录器系统。
项目中有一些东西需要与主窗体共享,所以我将它们设置为共享变量,但我不经常使用共享的东西,我担心这会导致关于范围的变量冲突。我想我应该在这里发表一篇关于它的文章,看看是否有人可以解释我不完全理解的内容。
由于这是一个记录器,它将在几个地方使用。其他需要日志记录的 DLL 可以通过实例化对象和项目引用来引用它。我的主窗体还将有一个实例化对象和对记录器库的引用。
由于我的属性之一是连接字符串并且是共享的,这是否意味着 DLL 内的记录器类实例将具有与主 UI 表单上的实例相同的共享值?或者实例位于 DLL 内部这一事实是否会提供我需要的范围边界?我希望它确实如此..
我主要担心我可能想在未来使用两个不同的连接字符串进行日志记录。
(我希望我的问题有意义。如果没有,请发表评论,我会尽力澄清。)
I'm trying to split some prior crafted code into a DLL. It's a simple logger system.
There are a few things that need to be shared with the main form in the project, so I set them up as a shared variable, but I don't use shared stuff often, and I worry it will cause variable conflicts regarding scope. I figured I would make a post here about it and see if someone can explain what I don't fully understand.
Since this is a logger it will be used a couple of places. Other DLLs that need logging may reference it through a instanced object and project reference. My main form will also have an instanced object and a reference to the logger libary.
Since one of my properties is a connection string and it's shared, does this mean that a instance of my logger class inside a DLL will have the same shared values as a instance on my main UI form? Or will the fact that the instance is inside of a DLL provide the scope boundary I need? I'm hoping it does..
I mainly worry that I might want to log using two different connection strings down the road.
(I hope my question makes sense. If it doesn't, post comments and I'll try to clarify.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,实例位于 DLL 中这一事实并不能提供您需要的范围边界。如果 DLL 中的类或成员被声明为静态,它们将被共享,并且您可能会遇到问题。因此,只要不要将它们声明为
静态
,并确保在使用它们时创建对象的新
实例,就应该没问题。No, the fact that the instance is in a DLL does not provide the scope boundary you need. If the class or members in the DLL were declared
static
they would be shared and you could run into problems. So, just don't declare themstatic
and be sure to createnew
instances of the object(s) when you consume them and you should be ok.