Vb.net Visual Basic、Imports&;中的安全字符串;模块

Vb.net Visual Basic、Imports&;中的安全字符串;模块,vb.net,syntax,syntax-error,Vb.net,Syntax,Syntax Error,我正在尝试输入密码并将其存储为安全字符串。我在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 testStr

我正在尝试输入密码并将其存储为安全字符串。我在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中的文件结构

您的问题来自这样一个事实:您可能不理解什么是类、模块等等。。。让我们回顾一下

要完整查看VB.NET,请执行以下操作:

但是有点长

进口声明

这句话的意思是:“嘿,编译器,我将要使用一些你将在这个文件中找到的函数,请将它链接到我的文件”

因此,
导入System.Security
意味着导入命名空间System.Security中包含的所有类和方法

放在哪里

此语句放在.vb文件的最下面。它不可能在其他任何东西里面。之前唯一可以做的事情就是选项(比如“Option Strict On”,但我们这里不讨论…)

类声明

放置class语句意味着您正在为对象创建新的“蓝图”。您将要定义它的方法、属性和功能

放在哪里

class语句位于
导入
声明之后,可以放在名称空间、模块或其他类中

模块语句

VB.NET中的一个模块,它是一个共享类。如果注意到不能声明共享类,则必须声明模块

这是什么意思

这意味着模块中的任何内容都可以访问,而无需实例化任何对象。如果您的模块提供了测试方法:

Public Module MyModule
  Public Function Test() As String
    Return "Test"
  End Function
End Module
然后,您可以通过执行以下操作调用该方法:

'... Anywhere in your code
MyModule.Test()
放在哪里

模块就像一个类,但是它不能被放置在另一个类中。它必须放在名称空间或文件中

希望这有助于

所以基本上

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

VB.NET中的文件结构

您的问题来自这样一个事实:您可能不理解什么是类、模块等等。。。让我们回顾一下

要完整查看VB.NET,请执行以下操作:

但是有点长

进口声明

这句话的意思是:“嘿,编译器,我将要使用一些你将在这个文件中找到的函数,请将它链接到我的文件”

因此,
导入System.Security
意味着导入命名空间System.Security中包含的所有类和方法

放在哪里

此语句放在.vb文件的最下面。它不可能在其他任何东西里面。之前唯一可以做的事情就是选项(比如“Option Strict On”,但我们这里不讨论…)

类声明

放置class语句意味着您正在为对象创建新的“蓝图”。您将要定义它的方法、属性和功能

放在哪里

class语句位于
导入
声明之后,可以放在名称空间、模块或其他类中

模块语句

VB.NET中的一个模块,它是一个共享类。如果注意到不能声明共享类,则必须声明模块

这是什么意思

这意味着模块中的任何内容都可以访问,而无需实例化任何对象。如果您的模块提供了测试方法:

Public Module MyModule
  Public Function Test() As String
    Return "Test"
  End Function
End Module
然后,您可以通过执行以下操作调用该方法:

'... Anywhere in your code
MyModule.Test()
放在哪里

模块就像一个类,但是它不能被放置在另一个类中。它必须放在名称空间或文件中

希望这有助于

所以基本上

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

您的导入语句不能在类内,它必须位于文件的第一行。与您的模块声明一样,它不能在类中。。。一个模块就像一个共享的类…很抱歉这样评论,但是如果你有时间,请看这个:谢谢Martin,我知道这是一个有点新手的问题,但是谢谢,像这样的少量但重要的信息似乎是通过google查找的噩梦。@repairtech当您开始使用一种新语言时,首先要做的是了解它是如何工作的:您的import语句不能在类中,它必须在文件的第一行。与您的模块声明一样,它不能在类中。。。一个模块就像一个共享的类…很抱歉这样评论,但是如果你有时间,请看这个:谢谢Martin,我知道这是一个有点新手的问题,但是谢谢,像这样的少量但重要的信息似乎是通过谷歌找到的噩梦。@repairtech好吧,当你开始研究一门新语言时,首先要做的是了解它是如何工作的: