Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my code compile in VB.NET but the equivalent in C# fails

The following VB.NET code works:

Dim request As Model.LearnerLogbookReportRequest = New Model.LearnerLogbookReportRequest
request.LearnerIdentityID = Convert.ToInt32(Session("identityID"))
request.EntryVersion = LearnerLogbookEntryVersion.Full

Dim reportRequestService As IReportRequestService = ServiceFactory.GetReportRequestService(ServiceInvoker.LearnerLogbook)
        reportRequestservice.SaveRequest(request)

The following C# code fails to compile:

LearnerLogbookReportRequest request = new LearnerLogbookReportRequest();
request.LearnerIdentityID = theLearner.ID;
request.EntryVersion = LearnerLogbookEntryVersion.Full;

IReportRequestService reportRequestService = ServiceFactory.GetReportRequestService(ServiceInvoker.LearnerLogbook);

reportRequestService.SaveRequest(ref request);

The LearnerLogbookReportRequest is declared as:

Public Class LearnerLogbookReportRequest
    Inherits AbstractReportRequest

Error:

Error   11  Argument 1: cannot convert from 'ref RACQ.ReportService.Common.Model.LearnerLogbookReportRequest' to 'ref RACQ.ReportService.Common.Model.AbstractReportRequest'    C:\p4projects\WEB_DEVELOPMENT\SECURE_ASPX\main-dev-codelines\LogbookSolution-DR6535\RACQ.Logbook.Web\Restful\SendLogbook.cs 64  50  RACQ.Logbook.Web

Why is the C# version failing to compile?

like image 264
Anthony Avatar asked Jun 12 '13 05:06

Anthony


People also ask

Which is better Visual Basic or C#?

When you will go for a job in Programming then C# will be the better choice. If you first want to learn then VB could be the better choice - it depends on what is better readable for you. In both cases : what you have to learn is not the programming-language - what you have to learn is the use of the .

Is C# faster than VB net?

It is that VB computes the loop bound once while C# computes the loop condition on each iteration. It is just a fundamental difference in the way the languages were intended to be used. ...is slightly faster than the equivalent in VB.

Does VB net support pointer?

NET really doesn't have pointers per-say, and certainly not like C pointers. This is primarily because the . NET Framework is a "managed" platform and memory is managed, assigned, allocated and deallocated by the CLR without you, the developer, having to worry about it (i.e. no malloc commands!)

Does VB 10.0 support keyword dynamic?

The dynamic keyword in C# gave it that same capability. It did get changed in VB.NET version 10 however, it is now using the DLR as well. Which adds support for dynamic binding to language implementations like Python and Ruby. The syntax is exactly the same, use the Dim keyword without As.


1 Answers

VB is rather looser with ByRef parameters than C# is. For example, it allows you to pass properties by reference. C# doesn't allow this.

In a similar way, with Option Strict off, VB allows you to use an argument which is a subtype of the declared parameter. As a short but complete program, consider this:

Imports System

Public Class Test
    Public Shared Sub Main(args As String())
        Dim p As String = "Original"
        Foo(p)
        Console.WriteLine(p)
    End Sub

    Public Shared Sub Foo(ByRef p As Object)
        p = "Changed"
    End Sub
End Class

That works in VB, but the equivalent in C# wouldn't... and for good reason. It's dangerous. In this case, we're using a string variable and we happen to change p to refer to another string, but if we change the body of Foo to:

p = new Object()

Then we get an exception at execution time:

Unhandled Exception: System.InvalidCastException: Conversion from type 'Object' to type 'String' is not valid.

Basically ref is compile-time type-safe in C#, but ByRef isn't type-safe in VB with Option Strict off.

If you add:

Option Strict On

to the program in VB, however (or just change the defaults for your project) you should see the same problem in VB:

error BC32029: Option Strict On disallows narrowing from type 'Object' to type
'String' in copying the value of 'ByRef' parameter 'p' back to the matching
argument.

        Foo(p)
            ~

This suggests that you're currently coding without Option Strict... I'd recommend using it ASAP.

like image 72
Jon Skeet Avatar answered Nov 14 '22 22:11

Jon Skeet