6

I have a legacy VB.Net class library that includes some extension methods, one of which is roughly this:

Namespace Extensions
    Public Module OrmExtensions

        <Extension()>
        Public Function ToDomainObjectCollection(ByRef objects As OrmCollection(Of OrmObject)) As DomainObjectCollection
            Return objects.AsQueryable().ToDomainObjectCollection()
        End Function

        <Extension()>
        Public Function ToDomainObjectCollection(ByRef objects As IQueryable(Of OrmObject)) As DomainObjectCollection
            Dim doc As New DomainObjectCollection()
            For Each o In objects
                doc.Add(o.ToDomainObject())
            Next
            Return doc
        End Function
    End Module
End Namespace

To use these extensions in VB I just have to import Extensions.OrmExtensions. I have another project, which is in C# that depends on the VB one with the extensions and I can't get them to work. OrmExtensions isn't available and simply using the namespace VBProject.Extensions doesn't make the extensions available.

There are multiple projects that depend on the VB project and ideally the extensions should be available to all of them.

I've done a fair bit of googling but haven't been able to turn up anything about actually using VB extension methods in C#. I assume the issue is that they're required to be in a Module, but I haven't been able to confirm that.

I'd rather not duplicate the extension methods everywhere they're required(especially in our unit test project, which is in C#).

7
  • You should extend IEnumerable<T>, not IQueryable<T>. Commented May 8, 2013 at 17:22
  • What happens if, in C# with the correct using, you type OrmExtensions. and look for the methods? How do they appear? Do they look like valid extension methods? Commented May 8, 2013 at 17:23
  • compile those extension method as an external library and then add a reference to that library from your c# projet Commented May 8, 2013 at 17:23
  • @TimS. the method is simply not available. Commented May 8, 2013 at 17:27
  • @SLaks i would disagree. If you need to extend IQueryable then that would be acceptable. Perhaps you need to explain further? Commented May 8, 2013 at 17:28

1 Answer 1

5

You should not be using ByRef.

VB.Net (apparently) supports extension methods on ref parameters; C# (rightfully) does not.

A ByRef (ref in C#) parameter allows the method to assign a new instance to the variable or field passed by the caller.
They should rarely be used.

Sign up to request clarification or add additional context in comments.

5 Comments

(And the fact that they're being used here possibly indicates a lack of understanding of the meaning of ByRef, which should be addressed.)
@EricJ.: Well for one thing the value of objects isn't changed in either of these methods. Why would you want it to be ByRef? And what would it mean for it to be ByRef with a changing value? What would happen if you called it on the result of a method call, for example?
@EricJ.: The callsite would either implicitly pass ref (allowing the method to unexpectedly change the caller) or make a hidden temporary variable (possibly breaking the method)
@JonSkeet ByRef was used (perhaps naively) to attempt to avoid the potentially expensive creation of a copy of a large collection.
@JustinEdwards: Yup, that suggests you don't understand what ByRef means (or rather, what ByVal means for reference types). It would be worth reading my C#-based article on the topic - the same principles are the same with VB: pobox.com/~skeet/csharp/parameters.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.