1

I am coding to the Podio .Net API using VB, but having difficulty porting the example C# code for a date range dictionary item to the VB.Net equivalent. Here is a snippet from their .NET API client documentation:

var filter = new Dictionary<string, object>
{
    {"somekey", from = new DateTime(2013, 9, 1), to = new DateTime(2013, 9, 30) }
};

I am not experienced in C#, so would appreciate any help in the equivalent VB syntax.

Steve_Ros
  • 23
  • 2
  • 1
    [c# to VB convertor](http://converter.telerik.com/) – Turnip Mar 22 '16 at 19:07
  • @DaveDoknjas I've "tried" that website many times – Turnip Mar 22 '16 at 19:47
  • @Turnip: I've deleted my comment - I didn't realize that the C# code wasn't valid in the first place when I tried that converter. – Dave Doknjas Mar 22 '16 at 20:02
  • This C# code is invalid - converters have no hope of producing valid VB code from invalid C# code. – Dave Doknjas Mar 22 '16 at 20:12
  • @DaveDoknjas: Technically, it could be valid given an extension method & a pair of local variables. – SLaks Mar 22 '16 at 20:23
  • Thanks @DaveDoknjas. I did copy the code example in the documentation correctly, but their C# syntax appears to be in error. The VB syntax you gave is exactly what I came up with, and does work in the application. However, I'm puzzled by your remark that the original C# doesn't use an anonymous type. I think what we have here is just an anonymous object initialization, both in the C# and VB versions. Regardless, It's working and I've accepted your solution. – Steve_Ros Mar 24 '16 at 03:21
  • @Steve_Ros: In your original C#, there is no "new {...}" syntax - that's the syntax for a C# anonymous type. – Dave Doknjas Mar 24 '16 at 03:33

1 Answers1

1

The online converters handle this very poorly. SLaks mentioned that we could be calling an extension method here which combines the 2 dates into the Dictionary value, but I could not get this to work in C#, so I don't think that is the case.

The only way to make sense of your original C# code is if we assume that you either did not copy it correctly or the API documentation was wrong, and that the code was intended to be:

var filter = new Dictionary<string, object>()
{
    {"somekey", new { from = new DateTime(2013, 9, 1), to = new DateTime(2013, 9, 30)} }
};

In this case, the conversion is straightforward and it seems to correspond to what worked for you as mentioned in your comment:

Dim filter = New Dictionary(Of String, Object)() From {
    {
        "somekey", New With {
            Key .from = New Date(2013, 9, 1),
            Key .to = New Date(2013, 9, 30)
        }
    }
}

And to be fair, the online converter mentioned previously does convert this adjusted C# code fine.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • Actually, if this is an extension method, this is impossible to convert to VB. That's assignment, not equality. – SLaks Mar 22 '16 at 20:49
  • Thank you Dave for responding. In the meanwhile I was able to get it working using VB.Net syntax for the dictionary entry {"somekey", New With {Key .from = fromarg, Key .to = toarg}} – Steve_Ros Mar 22 '16 at 20:50
  • @SLaks: It's assignment, but that's fine within this VB construct. – Dave Doknjas Mar 22 '16 at 20:58
  • @Steve_Ros: However, you are now using an anonymous type, but the original C# code does not. – Dave Doknjas Mar 22 '16 at 21:13