question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VB -> C#: Field initializers referencing non-static fields

See original GitHub issue

Input code

Public Class A
    Private x As Integer = 2
    Private y(x) As Integer
End Class

Erroneous output

public class A
{
    private int x = 2;
    private int[] y = new int[x + 1];
}

Expected output

public class A
{
    private int x = 2;
    private int[] y;
    public A()
    {
        y = new int[x + 1];
    }
}

Details

Product in use: VS extension

Version in use: 6.6.0

Accessing a non-static field in C# is an error, where it allowed in VB (CS0236).

It’s a bit more complicated than this, since you need to account for multiple constructors.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tverweijcommented, May 2, 2020

@GrahamTheCoder " I don’t know what the VB order is"

The VB Initializers run within the constructor before any other constructor code in the sequence top to bottom as they are defined in the class.

So the VB code:

Public Class x
    public property a As String =b
    private property b As String = "ABC"

    Public Sub New()
        b = "123"
    End Sub
End Class

translate to c# as:

public class x
{
    public string a { get; set; };
    private string b { get; set; };
    public x()
    {
        //field initializers
        a = b  //so result of a is null and NOT ABC or 123
        b = "ABC"
        //end field initializers
        //rest of the constructor code
        b= "123";
    }
}

0reactions
GrahamTheCodercommented, Mar 19, 2020

I’m consolidating the remainder of #418 here: The reference to a non-static might be inside a lambda, but the initialization it still needs to be moved to the constructor:

Imports System.IO

Public Class Test
    Private lambda As System.Delegate = New ErrorEventHandler(Sub(a, b) Len(0))
    Private nonShared As System.Delegate = New ErrorEventHandler(AddressOf OnError)

    Sub OnError(s As Object, e As ErrorEventArgs)
    End Sub
End Class
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Conceptual reason of the 'A field initializer cannot ...
I'm not sure about a field, but it seems rational to deny field initializers access to properties or methods. For example:
Read more >
Compiler Error CS0236
A field initializer cannot reference the non-static field, method, or property 'name'. Instance fields cannot be used to initialize other ...
Read more >
[Solved] A field initializer cannot reference the non-static ...
A variable initializer for an instance field cannot reference the instance being created. It means you can't initialize one instance member* ...
Read more >
Re: A field initializer cannot reference the non-static ...
Re: A field initializer cannot reference the non-static field, method, or property. textBox1 and textBox2 are fields, i.e. member variables, and ...
Read more >
C# 6.0 - A field initializer cannot reference the non-static field ...
The reason for the error is that the method GetCurrentDate is NOT marked as static . Setting the method static as shown below...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found