Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call instance method inline after New statement

Tags:

vb.net

How can i convert this code to VB.net

public void SetBooks(IEnumerable<Book> books)
    {
        if (books == null)
            throw new ArgumentNullException("books");

        new System.Xml.Linq.XDocument(books).Save(_filename);
    }

in http://converter.telerik.com/ it says:

Public Sub SetBooks(books As IEnumerable(Of Book))
        If books Is Nothing Then
            Throw New ArgumentNullException("books")
        End If
        New System.Xml.Linq.XDocument(books).Save(_filename)
End Sub

But visual studio says "Syntax error." because of "New"

What is the keyword for this situation, i searched on Google but no result.

like image 749
ibrahim Avatar asked Dec 16 '22 02:12

ibrahim


2 Answers

Actually, you can do it in one line with the Call keyword

Call (New System.Xml.Linq.XDocument(books)).Save(_filename)
like image 146
Tik Avatar answered Jan 17 '23 13:01

Tik


You cannot initialize an object and use it in one statement in VB.NET (as opposed to C#). You need two:

Dim doc = New System.Xml.Linq.XDocument(books)
doc.Save(_filename)

In C# the constructor returns the instance of the created object, in VB.NET not.

like image 40
Tim Schmelter Avatar answered Jan 17 '23 11:01

Tim Schmelter