Home > Blockchain >  syntax error in New Form() With {} vb.net
syntax error in New Form() With {} vb.net

Time:04-04

Dim name As String = "hello"
If CType(My.Application.OpenForms(name), Faker) Is Nothing Then
    New Faker() With {.Name = name, .Title = String.Format("{0} - ID:{1}", "hello", Me.ClassClient.ClientAddressID)}.Show()
End If

Syntax error in New , if i remove all code and write Dim F As New Faker() With {} and F.show() no error but not work and give me error while running the program object reference not set to an instance of an object

can any one here help me pls

CodePudding user response:

There are two answers here Constructing an object and calling a method without assignment in VB.Net that should help.

  1. Use With ... End With
  2. Use Call

I think you're having an issue because you're trying to use the C# style of creating an instance of an object without assigning it before chaining a method call.

Your code becomes:

Dim name As String = "hello"

If CType(My.Application.OpenForms(name), Faker) Is Nothing Then
    With New Faker() With {.Name = name, .Title = String.Format("{0} - ID:{1}", "hello", Me.ClassClient.ClientAddressID)}

    .Show()
End If

Or

Dim name As String = "hello"

If CType(My.Application.OpenForms(name), Faker) Is Nothing Then
    Call New Faker() With {.Name = name, .Title = String.Format("{0} - ID:{1}", "hello", Me.ClassClient.ClientAddressID)}.Show()

End If
  • Related