1

I have taken this struct definition from the Windows API CodePack:

public struct IconReference
{
    //...

    public IconReference(string moduleName, int resourceId)
        : this()
    {
        //...
    }

    public IconReference(string refPath)
        : this()
    {
        //...
    }

    //...
}

The problem is I don't understand how to translate those kind of constructors to Vb.Net.

What is exactlly the meaning of that : this()?

When I use an online code translator, it translates it as Me.New(), however, this fails at compilation because that struct does not have a parameterless ctor.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

2 Answers2

3

The this() in C# calls the parameterless constructor. Since you don't have a parameterless constructor in C# (and structs can't even contain "explicit parameterless constructors"), you can omit the this().

And so for the VB.NET code. You can omit the Me.New() code.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Yes a default constructor can be specified in Vb.Net but the MSDN guidelines says it is incorrect to do it, I'm not sure why the WindowsAPICodePack guys did that, but if I can omit it then problem solved, thanks. – ElektroStudios Feb 12 '16 at 08:56
  • 1
    You are welcome. VB.NET gave me an error in VS2015, but not in Ideone, so I think they use an outdated compiler. Probably this has been 'fixed' in the lastest version of the VB.NET compiler. – Patrick Hofman Feb 12 '16 at 08:57
  • If you omit the call to the default constructor in C#, you'll hit a [compiler error](https://msdn.microsoft.com/en-us/library/bb513821.aspx), since the class has a automatically implemented property. – sloth Feb 12 '16 at 09:54
  • Indeed, not the code OP gave, but as Hans pointed out, that code is incomplete... @sloth – Patrick Hofman Feb 12 '16 at 09:55
2

This syntax is needed because IconReference has an "automatically implemented property":

  public int ResourceId { get; set; }

see also https://stackoverflow.com/a/7670854/121309

Community
  • 1
  • 1
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111