0

From:

Visual Studio Not Displaying SVG image as background

I'm trying to convert:

public class SvgHandler : IHttpHandler
{

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/svg+xml";
        context.Response.BinaryWrite(File.ReadAllBytes(context.Request.PhysicalPath));
        context.Response.End();
    }
}

to VB.NET.

My automatic conversion from most of the online converters is:

Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports System.Security
Imports System.Text
'Imports System.Threading.Tasks
Imports Microsoft.VisualBasic

Public Class SvgHandler
    Implements IHttpHandler

    Public ReadOnly Property IsReusable() As Boolean
        Get
            Return False
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context As HttpContext)

        context.Response.ContentType = "image/svg+xml"
        context.Response.BinaryWrite(File.ReadAllBytes(context.Request.PhysicalPath))
        context.Response.[End]()

    End Sub

End Class

However the Implements IHttpHandler has a squiggly blue underline with the message:

Compiler Error Message: BC30154: Class 'SvgHandler' must implement 'ReadOnly Property IsReusable() As Boolean' for interface 'System.Web.IHttpHandler'. Implementing property must have matching 'ReadOnly' or 'WriteOnly' specifiers.

What should the correct VB.NET code be?

user1946932
  • 474
  • 1
  • 16
  • 41
  • 1
    One of these should help. You need to nominate the "Implements ...", I believe https://stackoverflow.com/questions/23379054/vb-net-must-implement-error https://stackoverflow.com/questions/31371200/implementing-interface-from-c-sharp-to-vb-net – Craig May 10 '21 at 03:17
  • Or is it as simple as just dropping the brackets "Public ReadOnly Property IsReusable As Boolean" – Hursey May 10 '21 at 03:18
  • @Hursey No they just automatic re-appear :) – user1946932 May 10 '21 at 03:21
  • 1
    If you create an empty class with the appropriate name and then add the `Inherits` line, VS will offer to implement the missing members for you. You should do that to get the correct structure, then add the translated property/method bodies. – jmcilhinney May 10 '21 at 03:25
  • 1
    The converters won't have enough information from your initial C# code - where exactly is IHttpHandler from? You'll need a "using System.Web" to let the converter know exactly which interface it's dealing with. If your converter allows you to specify assemblies then you'll also need to let the converter know to examine System.Web.dll. Converters can't assume too much - they can't assume that every interface named IHttpHandler is System.Web.IHttpHandler. – Dave Doknjas May 10 '21 at 03:44
  • 1
    In C#, a method which matches something in the interface is automatically considered to implement it. In VB, you must explicitly state that the property or routine implements the interface member (and which member it implements)---and while I wouldn't call it good practice, you can give it any name you'd like. – Craig May 10 '21 at 18:37

1 Answers1

1

This compiles (as suggested automatically by VS2019):

Imports System.IO

Public Class SvgHandler
    Implements IHttpHandler
    Private ReadOnly Property IHttpHandler_IsReusable As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

    Private Sub IHttpHandler_ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest

        context.Response.ContentType = "image/svg+xml"
        context.Response.BinaryWrite(File.ReadAllBytes(context.Request.PhysicalPath))
        context.Response.[End]()

    End Sub

End Class
user1946932
  • 474
  • 1
  • 16
  • 41