简体   繁体   中英

Convert C# (with typed events) to VB.NET

I have an ASPX page (with VB Codebehind). I would like to extend the GridView class to show the header / footer when no rows are returned.

I found a C# example online ( link ) ( source ). However, I cannot convert it to VB because it uses typed events (which are not legal in VB).

I have tried several free C# to VB.NET converters online, but none have worked.

Please convert the example to VB.NET or provide an alternate method of extending the GridView class.

Notes / Difficulties:

  1. If you get an error with DataView objects, specify the type as System.Data.DataView and the type comparison could be the following: If data.[GetType]() Is GetType(System.Data.DataView) Then

  2. Since the event MustAddARow cannot have a type in VB (and RaiseEvent event doesn't have a return value), how can I compare it to Nothing in the function OnMustAddARow() ?

EDIT:

The following is a sample with (hopefully) relevant code to help answer the question.

namespace AlwaysShowHeaderFooter {
    public delegate IEnumerable MustAddARowHandler(IEnumerable data);

    public class GridViewAlwaysShow : GridView {
           // Various member functions omitted //
           protected IEnumerable OnMustAddARow(IEnumerable data) {
            if (MustAddARow == null) {
                throw new NullReferenceException("The datasource has no rows. You must handle the \"MustAddARow\" Event.");
            }
            return MustAddARow(data);
        }

        public event MustAddARowHandler MustAddARow;
    }
}

Please see here .

There is no exact equivalent to this for VB.

Use a 'Sub' delegate with a 'ByRef' parameter instead.

A follow-up answer gives an example that works, but explains why it's a bad idea.

关于1,最干净的VB方式是

If TypeOf data Is System.Data.DataView Then

As I said in my comment the code will not convert to VB.Net "magically". It will require working thru it to get it to compile properly.

The easiest would be to compile the the C# code as a library.

  1. Create a new project (C# Class Library) and name it "AlwaysShowHeaderFooter"
  2. Move the files from App_Code over to the new project
  3. Add a reference to System.Web and System.Configuration
  4. Add a reference in your web project to either the compiled dll's of "AlwaysShowHeaderFooter", or add the project itself as a reference if you have it in the same solution.
  5. Switch out <%@ Register TagPrefix="Custom" Namespace="AlwaysShowHeaderFooter" %> with <%@ Register Assembly="AlwaysShowHeaderFooter" Namespace="AlwaysShowHeaderFooter" TagPrefix="Custom" %>

Now you have split the control out to it's own project which can be referenced in any .Net project.

关于2.我认为VB.Net中的事件可以传递ByRef参数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM