Visual Basic、导入和模块中的安全字符串
Secure String in Visual Basic, Imports & Module
我正在尝试输入密码并将其存储为安全字符串。我在 MSDN 上找到了以下代码。
Imports System.Security
Module Example
Public Sub Main()
' Define the string value to be assigned to the secure string.
Dim initString As String = "TestString"
' Instantiate the secure string.
Dim testString As SecureString = New SecureString()
' Use the AppendChar method to add each char value to the secure string.
For Each ch As Char In initString
testString.AppendChar(ch)
Next
' Display secure string length.
Console.WriteLine("The length of the string is {0} characters.", _
testString.Length)
testString.Dispose()
End Sub
End Module
' The example displays the following output:
' The length of the string is 10 characters.
但是我遗漏了一些东西,因为当我将它粘贴到 Public Class 元素时出现错误。错误如下:
'Imports' statements must precede an declarations
'Module' statements can occur only at file or namespace level
我试过将 import 语句放在其他代码中的任何 dim
语句之前,这没有帮助,而且我不确定我应该把模块放在哪里(即文件或命名空间级别在哪里?)
非常感谢任何帮助
VB.NET
中文件的结构
您的问题来自于您可能不了解什么是 class、模块等等...所以让我们回顾一下...
VB.NET 的完整回顾:https://msdn.microsoft.com/en-us/library/aa712050%28v=vs.71%29.aspx
但是有点长...
进口声明
这里声明的是:"Hey Compiler, I am about to use some functions that you will find in this file, please link it to my file"
so Imports System.Security
表示您导入所有 class 名称空间 System.Security.
中包含的所有方法和方法
放在哪里?
此声明放在您的 .vb 文件的开头。它不能在其他任何东西里面。之前唯一可以做的是选项(比如“Option Strict On”,但我们不会在这里讨论......)。
Class声明
放置 class 语句意味着您正在为一个对象创建一个新的 "blueprint"。您即将定义其方法、属性 和功能。
放在哪里?
class 语句在 Imports
声明之后,可以放在名称空间、模块或另一个 class
中
模块语句
VB.NET 中的一个模块,除了共享 Class。如果您注意到,您不能声明共享 Class,而必须声明模块。
这是什么意思?
这意味着无需实例化任何对象即可访问模块中的任何内容。如果您的模块提供测试方法:
Public Module MyModule
Public Function Test() As String
Return "Test"
End Function
End Module
然后您可以通过以下方式调用该方法:
'... Anywhere in your code
MyModule.Test()
放在哪里?
一个模块就像一个 class,但是它 不能 放在另一个 class 里面。它必须放在名称空间内或您的文件中。
希望这对您有所帮助...
所以基本上
Option Strict On
Imports System.Security
Namespace MyNamespace
Public Module MyModule
'My code here
Public Class MyClassInsideAModule
'My code here
End Class
End Module
Public Class MyClass
'My code here
Public Class MyClassInsideAClass
'My code here
End Class
End Class
End Namespace
我正在尝试输入密码并将其存储为安全字符串。我在 MSDN 上找到了以下代码。
Imports System.Security
Module Example
Public Sub Main()
' Define the string value to be assigned to the secure string.
Dim initString As String = "TestString"
' Instantiate the secure string.
Dim testString As SecureString = New SecureString()
' Use the AppendChar method to add each char value to the secure string.
For Each ch As Char In initString
testString.AppendChar(ch)
Next
' Display secure string length.
Console.WriteLine("The length of the string is {0} characters.", _
testString.Length)
testString.Dispose()
End Sub
End Module
' The example displays the following output:
' The length of the string is 10 characters.
但是我遗漏了一些东西,因为当我将它粘贴到 Public Class 元素时出现错误。错误如下:
'Imports' statements must precede an declarations
'Module' statements can occur only at file or namespace level
我试过将 import 语句放在其他代码中的任何 dim
语句之前,这没有帮助,而且我不确定我应该把模块放在哪里(即文件或命名空间级别在哪里?)
非常感谢任何帮助
VB.NET
中文件的结构您的问题来自于您可能不了解什么是 class、模块等等...所以让我们回顾一下...
VB.NET 的完整回顾:https://msdn.microsoft.com/en-us/library/aa712050%28v=vs.71%29.aspx
但是有点长...
进口声明
这里声明的是:"Hey Compiler, I am about to use some functions that you will find in this file, please link it to my file"
so Imports System.Security
表示您导入所有 class 名称空间 System.Security.
放在哪里?
此声明放在您的 .vb 文件的开头。它不能在其他任何东西里面。之前唯一可以做的是选项(比如“Option Strict On”,但我们不会在这里讨论......)。
Class声明
放置 class 语句意味着您正在为一个对象创建一个新的 "blueprint"。您即将定义其方法、属性 和功能。
放在哪里?
class 语句在 Imports
声明之后,可以放在名称空间、模块或另一个 class
模块语句
VB.NET 中的一个模块,除了共享 Class。如果您注意到,您不能声明共享 Class,而必须声明模块。
这是什么意思?
这意味着无需实例化任何对象即可访问模块中的任何内容。如果您的模块提供测试方法:
Public Module MyModule
Public Function Test() As String
Return "Test"
End Function
End Module
然后您可以通过以下方式调用该方法:
'... Anywhere in your code
MyModule.Test()
放在哪里?
一个模块就像一个 class,但是它 不能 放在另一个 class 里面。它必须放在名称空间内或您的文件中。
希望这对您有所帮助...
所以基本上
Option Strict On
Imports System.Security
Namespace MyNamespace
Public Module MyModule
'My code here
Public Class MyClassInsideAModule
'My code here
End Class
End Module
Public Class MyClass
'My code here
Public Class MyClassInsideAClass
'My code here
End Class
End Class
End Namespace