Reputation: 3213
How can I create an instance of a class in my main module? I have not been using VB.NET for all that long (Actually about two days) What I want is to create a console application for a test and create classes that are not in the main code file. I can create an instance of the class if it is in the same main module but what I don't know how to do is create a instance if the class is not in the main module.
CLass File:
Public Class Class1
Dim cText As String
End Class
Main Module:
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
Upvotes: 0
Views: 1098
Reputation: 34846
You seem to be mixing the syntax of C# and VB.NET together a little bit.
In C#, the declaration type of the variable appears before the name of the identifier, like this:
Class1 h = new Class1();
In VB.NET, you dimension the variable and then the identifier, like this:
Dim h As New Class()
It is also possible to declare/dimension variables and then assign them in a separate line, like this:
C#:
Class1 h;
h = new Class1();
VB.NET:
Dim h As Class1
h = new Class1()
Upvotes: 0
Reputation: 2992
Use this line
Dim h As New Class1()
instead of
Class1 h As new Class1()
Upvotes: 1
Reputation: 11
Change this line:
Class1 h As new Class1() <--Error at this line
to
Dim h As New Class1() <--No more errors
Upvotes: 1