7

With this Blazor component:

@page "/counter"

<h1>Counter</h1>

<p>Current count: @_a.CurrentCount</p>

<button class="btn btn-primary" onclick="@_b.IncrementCount">Click me</button>

@functions {
    private readonly ClassA _a = new ClassA();
    private readonly ClassB _b = new ClassB(_a);

    class ClassA
    {
        public int CurrentCount { get; set; }
    }

    class ClassB
    {
        private readonly ClassA _classA;

        public ClassB(ClassA classA)
        {
            _classA = classA;
        }

        public void IncrementCount() => _classA.CurrentCount++;
    }
}

I get this error:

Error CS0236 A field initializer cannot reference the non-static field, method, or property '__Counter._a'

This thread explain how resolve this error in standard class:

Why can't you use 'this' in member initializers?

But for this, it needs a constructor.

Is it possible to add constructor in Blazor component?

How resolve this error?

3
  • 2
    You proably have to use the OnInit method. Commented Feb 25, 2019 at 23:06
  • This done the job, but I need remove the readonly attribut to initialize _a and _b in OnInit. Commented Feb 25, 2019 at 23:30
  • @HenkHolterman, init also was my first idea and, after read OP comment, I guess "code-behind" is the only way to match readonly properties requirement. I elaborate an answer with this. Commented Feb 26, 2019 at 11:28

1 Answer 1

8

To keep classes readonly you should to move to "code-behind". Then you can instantiate classes on constructor:

@page "/counter"
@inherits CounterBase
<h1>Counter</h1>

<p>Current count: @_a.CurrentCount ...

CounterBase.cs

using Microsoft.AspNetCore.Blazor.Components;

namespace YourApp.Pages
{

    public class ClassA
    {
        public int CurrentCount { get; set; }
    }

    public class ClassB
    {
        private readonly ClassA _classA;

        public ClassB(ClassA classA)
        {
            _classA = classA;
        }

        public void IncrementCount() => _classA.CurrentCount++;
    }

    public class CounterBase : BlazorComponent
    {
        protected readonly ClassA _a;
        protected readonly ClassB _b;

        //constructor
        public CounterBase()
        {
            _a = new ClassA();
            _b = new ClassB(_a);
        }
        ...
Sign up to request clarification or add additional context in comments.

7 Comments

The constructor is invalid. Can you remove the void? And I valid you answer.
lel, of course. Sorry about typo. Any other comment is welcome.
Now, I have this error : Error CS0122 'CounterBase._a' is inaccessible due to its protection level. If I replace by protected, I can't use readonly. Other idea?
fixed now with public class ? ( or move classA and classB as nested classes ) Maybe nested classes are closer to your initial question.
It's not possible to access private field in subclass. You need add a getter and use this getter in component. In the constructor, use field and don't declare variables. Can you improve your answer?
|

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.