none
Converting Code Snip from C# to VB.NET? RRS feed

  • Question

  • Hello,

    I am trying to use some C# code in my VB.NET project, and used a converter to port over a class, and the conversion went well. But in one part I am getting a syntax error on the keyword 'New' hoping someone can assist in getting the code converted correctly.

    Here's the C# code:

    private static void DemandWebPermission(Uri uri)
    {
    string uriString = UriToString(uri);
    
    if (uri.IsFile)
    {
    string localPath = uri.LocalPath;
    new FileIOPermission(FileIOPermissionAccess.Read, localPath).Demand();
    }
    else
    {
    new WebPermission(NetworkAccess.Connect, uriString).Demand();
    }
    }

    And here's the conversion where the keywords 'New' are marked as having BC0035 Syntax errors:

        Private Shared Sub DemandWebPermission(ByVal uri As Uri)
            Dim uriString As String = UriToString(uri)
    
            If uri.IsFile Then
                Dim localPath As String = uri.LocalPath
                New FileIOPermission(FileIOPermissionAccess.Read, localPath).Demand()
            Else
                New WebPermission(NetworkAccess.Connect, uriString).Demand()
            End If
        End Sub


    Tuesday, June 18, 2019 7:03 PM

Answers

  • VB does not allow calling a member on an instance created in the same line unless you use the 'Call' syntax:

    Private Shared Sub DemandWebPermission(ByVal uri As Uri)
    	Dim uriString As String = UriToString(uri)
    
    	If uri.IsFile Then
    		Dim localPath As String = uri.LocalPath
    		Call (New FileIOPermission(FileIOPermissionAccess.Read, localPath)).Demand()
    	Else
    		Call (New WebPermission(NetworkAccess.Connect, uriString)).Demand()
    	End If
    End Sub
    


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Tuesday, June 18, 2019 7:21 PM
  • Call New FileIOPermission(FileIOPermissionAccess.Read, localPath).Demand()

     Call New WebPermission(NetworkAccess.Connect, uriString).Demand()

    the converter should have done the above. 

    • Marked as answer by David VB2005 Wednesday, June 19, 2019 1:19 AM
    Tuesday, June 18, 2019 7:38 PM

All replies

  • VB does not allow calling a member on an instance created in the same line unless you use the 'Call' syntax:

    Private Shared Sub DemandWebPermission(ByVal uri As Uri)
    	Dim uriString As String = UriToString(uri)
    
    	If uri.IsFile Then
    		Dim localPath As String = uri.LocalPath
    		Call (New FileIOPermission(FileIOPermissionAccess.Read, localPath)).Demand()
    	Else
    		Call (New WebPermission(NetworkAccess.Connect, uriString)).Demand()
    	End If
    End Sub
    


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Tuesday, June 18, 2019 7:21 PM
  • Call New FileIOPermission(FileIOPermissionAccess.Read, localPath).Demand()

     Call New WebPermission(NetworkAccess.Connect, uriString).Demand()

    the converter should have done the above. 

    • Marked as answer by David VB2005 Wednesday, June 19, 2019 1:19 AM
    Tuesday, June 18, 2019 7:38 PM
  • Thanks, Dave and DA! That helped a lot!
    Wednesday, June 19, 2019 1:20 AM