初始化全局变量.Net [英] Initialize Global Variable .Net

查看:183
本文介绍了初始化全局变量.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过初始化事件模块中的模块/变量的方式来寻求帮助。



我需要它加载,以便下面的WinForm使用它(显然)
让我解释一下...



我使用WinSCP库,并得到作者自己在WinSCP方面的​​大量帮助。
但为了解决当前的问题,我需要初始化一个全局变量。
所以变量在那里,它的顺序,但Form2拒绝使用它,因为它显然需要从Form1中的事件开始/初始化。
Module被称为Public Module Module 1。

 公共模块Module1 

Public mySession As Session

End Module

我需要在此事件中启动/启动它;

  Private Sub Loginbutton_Click(sender As Object,e As EventArgs)处理Loginbutton.Click 

WinSCP Author无法提供帮助,因为它仅在WinSCP之外,只是.Net / VB问题。我在Stackoverflow上得到了非常多的帮助,所以我知道这里有人可以帮忙。



这可能是我忽略和遗忘的一些容易的事情。
此时我的头脑受到了伤害,所以任何帮助,代码示例和其他形式的帮助都非常值得赞赏,因为我的脑袋略微伤害了所有这些Visual Studio的东西。

解决方案

您这样做

  Public Module Module1 

Public mySession As New Session

End Module



 公共模块模块1 

公开mySession作为新建会话

Sub New()
'// ===>这段代码会在应用程序启动时执行
mySession = New Session()
End Sub

End Module

或者在按钮点击事件中创建实例



pre $私人小组LoginButton_Click(sender As Object,e As EventArgs)Handle Loginbutton.Click
mySession = New Session()
End Sub

如果您不能使用这个选项,那么您需要阅读库的文档,因为有时需要一个参数来初始化对象,或者甚至不能创建它的一个实例,因为只能创建调用其他实例将在里面创建的函数(这种事情是出于安全原因)


I'm trying to find some help in the ways of "Initializing" A module/Variable inside the module on an event.

I need it to load, in order for the following WinForm to use it (apparently) Let me explain...

I am using the WinSCP library and gotten plenty of help in the ways of WinSCP by the author himself. But in order to fix my current problem, I need to Initialize a Global Variable. So the Variable is there, its in order, but "Form2" refuses to use it, as it apparently needs to be Started/Initialized from an event in Form1. Module is called Public Module Module 1.

Public Module Module1

Public mySession As Session

End Module

I need to Launch/Start it on this Event;

Private Sub Loginbutton_Click(sender As Object, e As EventArgs) Handles Loginbutton.Click

WinSCP Author was unable to help as it was outside WinSCP alone, and just .Net/VB issue. I've gotten so much excellent help here on Stackoverflow so I know someone here will be able to help.

It might be some easy thing I have overlooked and forgotten. My head is hurting at this time, so any help, code samples and other forms of help is greatly appreciated as my head is slightly hurting of all this Visual Studio stuff.

解决方案

You do this

Public Module Module1

Public mySession As New Session

End Module

Or

Public Module Module1

Public mySession As New Session

Sub New()
  '//===>This code will execute when the application starts
   mySession = New Session()
End Sub

End Module

Or create the instance on the button click event

Private Sub Loginbutton_Click(sender As Object, e As EventArgs) Handles Loginbutton.Click
 mySession = New Session()
End Sub 

if you can't none of this option then you need to read the documentation of the library, because sometimes require a parameter to initialize the object, or even you can not create a instance of it, because can only create the instance calling other function that will created inside (This kind of thing is for security reason)

这篇关于初始化全局变量.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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