2013-12-17 73 views
0

如何在我的主模塊中創建一個類的實例?我一直沒有使用VB.NET(實際上大約兩天)我想要的是爲測試創建一個控制檯應用程序並創建不在主代碼文件中的類。如果它在同一個主模塊中,我可以創建一個類的實例,但是我不知道如何做的是創建一個實例,如果該類不在主模塊中。Visual Basic .NET想在我的主模塊中創建一個類文件實例?

類文件:

Public Class Class1 
    Dim cText As String 
End Class 

主要模塊:

Module Module1 

    Sub Main() 
     Dim oLine As New Line("a", "b", "c") 

     oLine.setYourName = "testName-testName" 

     Class1 h As new Class1() <--Error at this line 

     Console.WriteLine(oLine.setYourName) 
     Console.ReadLine() 
    End Sub 


End Module 

Public Class Line 

    Private mstrLine As String 
    Private mstrTest As String 
    Friend Text As String 

    Public Sub New() 
     Console.WriteLine("Zero-Arguement Construtor") 
    End Sub 

    Public Sub New(ByVal Value As String) 
     Console.WriteLine("One-Arguement Construtor") 
    End Sub 

    Public Sub New(ByVal Value As String, ByVal v As String, ByVal a As String) 
     Console.WriteLine("Three-Arguement Construtor") 
    End Sub 

    Public Sub TextFileExample(ByVal filePath As String) 
     ' Verify that the file exists. 
     If System.IO.File.Exists(filePath) = False Then 
      Console.Write("File Not Found: " & filePath) 
     Else 
      ' Open the text file and display its contents. 
      Dim sr As System.IO.StreamReader = System.IO.File.OpenText(filePath) 
      Console.Write(sr.ReadToEnd) 
      sr.Close() 
     End If 
    End Sub 

    Public Function GetWord() As String 
     Dim astrWords() As String 
     astrWords = Split(mstrLine, " ") 
     Return astrWords(0) 
    End Function 

    Property setYourName() As String 
     Get 
      Return Text 
     End Get 
     Set(value As String) 
      Text = value 
     End Set 
    End Property 

    Property Line() As String 
     Get 
      Return mstrLine 
     End Get 
     Set(ByVal Value As String) 
      mstrLine = Value 
     End Set 
    End Property 

    ReadOnly Property Length() As Integer 
     Get 
      Return mstrLine.Length 
     End Get 
    End Property 
End Class 
+0

什麼是錯誤 – Jade

回答

1

使用此行

Dim h As New Class1() 

代替

Class1 h As new Class1() 
1

改變這一行:

Class1 h As new Class1() <--Error at this line 

Dim h As New Class1() <--No more errors 
0

你似乎是混合的C#和VB.NET一起一點點的語法。

在C#中,變量的聲明類型的標識符的名稱之前出現時,這樣的:

Class1 h = new Class1(); 

在VB.NET,您維度變量,然後標識符,像這樣:

Dim h As New Class() 

也可以聲明/維變量,然後在一個單獨的線分配它們,這樣的:

C#:

Class1 h; 
h = new Class1(); 

VB.NET:

Dim h As Class1 
h = new Class1() 
相關問題