Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading