0

I am working on converting an extension method I have in C# into VB.Net, but can't figure out the correct way to set a constraint on the output.

My current C# code is working on a Dictionary and looks as follows:

public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)
    where TValue : new()
{   }

I've seen questions explaining how to set the constraint on the inputs such as here, but I can't seem to find one explaining how to convert the output constraint of where TValue : new()

I'm sure it's easy, but I've grown a bit rusty with VB and hoping someone here knows it off the top of their head.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • [VB.Net Generic Procedures](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/generic-procedures) - the constraints go directly inside the `(Of ...)` between procedure name and argument list. See the example `Public Function findElement(Of T As IComparable)` – Damien_The_Unbeliever Feb 11 '20 at 16:56
  • The syntax is actually the same for inputs and outputs, see https://stackoverflow.com/a/25256115/1968 – Konrad Rudolph Feb 11 '20 at 16:57
  • 1
    Does this answer your question? [Any VBNET equivalence of C# where generic constraint keyword?](https://stackoverflow.com/questions/2803933/any-vbnet-equivalence-of-c-sharp-where-generic-constraint-keyword) – madreflection Feb 11 '20 at 16:57

1 Answers1

5

Here's the conversion, as obtained from Telerik's Code Converter:

Module Extensions
  <Extension()>
  Public Function GetOrCreate(Of TKey, TValue As New)(ByVal dict As IDictionary(Of TKey, TValue), ByVal key As TKey) As TValue

  End Function
End Module
djv
  • 15,168
  • 7
  • 48
  • 72
InteXX
  • 6,135
  • 6
  • 43
  • 80
  • 2
    Beat me to it by about 30 seconds. – Joel Coehoorn Feb 11 '20 at 16:59
  • WOOHOO!!! - The trick was the `GetOrCreate(Of TKey, TValue As New)` - Thank you!!! – John Bustos Feb 11 '20 at 17:01
  • I saw people's comments, but none of them made sense / worked - THIS gave me the answer. Thank you!! – John Bustos Feb 11 '20 at 17:02
  • Also: there was an edit to remove the `Shared` modifier, which I rolled back. `Shared` should be removed if the method is part of a Module, but is still needed if the method is part of Class. Since this code is being converted from C#, I believe the latter is more likely. – Joel Coehoorn Feb 11 '20 at 17:02
  • @AhmedAbdelhameed Was already typing a response separately, just took a minute to have it ready. – Joel Coehoorn Feb 11 '20 at 17:03
  • @JoelCoehoorn Correct me if I'm wrong but a VB extension method _**Must**_ be defined in a module as I mentioned in the edit message. – 41686d6564 stands w. Palestine Feb 11 '20 at 17:03
  • @AhmedAbdelhameed Generally that is true, but IIRC there are ways around it. But maybe those required reflection games or similar. It's been a while. – Joel Coehoorn Feb 11 '20 at 17:04
  • @JoelCoehoorn ~ To clarify the `Shared` situation, I edited the code a bit. – InteXX Feb 11 '20 at 17:21
  • @JohnBustos ~ YW :-) – InteXX Feb 11 '20 at 17:22
  • @AhmedAbdelhameed ~ `... a VB extension method Must be defined in a module ...` Yes, that's correct. In my experience, at least. I've not experimented with putting one in a class as Joel suggests might be possible. In any event, I haven't run across a situation that would make it worth the extra trouble. Modules are easy. So easy, in fact, that I created three of them yesterday ;-) – InteXX Feb 11 '20 at 18:40