How to access class member from lambda in C#? [duplicate] - c#

This question already has answers here:
A field initializer cannot reference the nonstatic field, method, or property - while creating a list
(2 answers)
Error 1 A field initializer cannot reference the non-static field, method, or property
(2 answers)
Closed 4 years ago.
In java I can do this
public class HelloWorld
{
public static void main(String[] args)
{
OtherClass oc = new OtherClass();
oc.a.run();
}
}
public class OtherClass
{
public int s = 3;
public Runnable a = () -> System.out.println("s is " + s);
}
The output will be s is 3. When I try this in C# with this code
using System;
namespace SomeNamespace
{
public class Program
{
public static void Main(string[] args)
{
MyClass m = new MyClass();
m.a.Invoke();
}
}
public class MyClass
{
public int s = 3;
public Action a = () => Console.WriteLine(s);
}
}
Then I get (23:51) A field initializer cannot reference the non-static field, method, or property 'SomeNamespace.MyClass.s'

Related

c# How to get properties of a static class instance [duplicate]

This question already has answers here:
Get value of a public static field via reflection
(4 answers)
Closed 1 year ago.
How can i get the public properties from a static instance of a class?
Example
public record MyClass{
public static MyClass Instance1 = new(true);
public static MyClass Instance2 = new(false);
public bool MyParam {get;init;}
private MyClass(bool myParam){
MyParam = myParam;
}
}
And I would like to access the value of MyParam from the static instances Instance1, Instance2.
Edit: sorry for the bad details. I want to do this all via reflection. Get all the static instances of the class, and for each instance the value of the param.
Based on how you wrote your code, you can accessit as MyClass.Instance1.MyParam
For example (Try it out here: https://dotnetfiddle.net/nwBbkH#)
using System;
public class Program
{
public static void Main()
{
// Here we are accessing MyParam
Console.WriteLine(MyClass.Instance1.MyParam);
Console.WriteLine(MyClass.Instance2.MyParam);
}
}
public record MyClass
{
public static MyClass Instance1 = new(true);
public static MyClass Instance2 = new(false);
public bool MyParam
{
get;
init;
}
private MyClass(bool myParam)
{
MyParam = myParam;
}
}

Static field problem (error CS0120) in c# [duplicate]

This question already has answers here:
CS0120: An object reference is required for the nonstatic field, method, or property 'foo'
(9 answers)
Closed 2 years ago.
I have to vrite an override function for my function DajGlos(), but I get back error CS0120 (An object reference is required for the non-static field, method, or property). How can I fix this?
My code:
static void Main(string[] args)
{
Pies pies = new Pies("Reksio", "ssaki", "lądowe", 50);
Pies.Przedstaw("Reksio", "ssaki", "lądowe");
Pies.DajGlos();
}
abstract class Zwierze
{
private static string Rodzina { get; set; }
private static string Grupa { get; set; }
private static string Imie { get; set; }
public static void Przedstaw(string Imie, string Rodzina, string Grupa)
{
Console.WriteLine("Jestem " + Imie + ", rodzina: " + Rodzina + ", grupa: " + Grupa);
}
public abstract void DajGlos();
}
class Pies : Zwierze
{
public Pies(string Imie, string Rodzina, string Grupa, int dlugoscOgona)
{
}
int dlugoscOgona;
public override void DajGlos()
{
Console.WriteLine("Bark!");
}
}```
ClassName.StaticMethodName(...) is for accessing static methods.
ObjectName.NonStaticMethodName(...) is for accessing non-static methods.
The line Pies.DajGlos(); is ClassName.NonStaticMethodName(...) which is not allowed.
I guess what you were trying to do is: pies.DajGlos();.
DajGlos is an instance method, so as the error message says, you need to call it on a specific instance (in your case - pies), not on the class itself. I.e.:
Pies pies = new Pies("Reksio", "ssaki", "lądowe", 50);
Pies.Przedstaw("Reksio", "ssaki", "lądowe");
pies.DajGlos(); // Here!

How to Set Object to null [duplicate]

This question already has answers here:
Can I modify a passed method parameter
(3 answers)
Closed 4 years ago.
Why c = null does not work in the following code?
public class Sample{
public int c;
}
public class Program
{
public static void Main(string[] args)
{
Sample c = new Sample();
Console.WriteLine(c.c);
f(c);
Console.WriteLine(c.c);
g(c);
Console.WriteLine(c.c);
}
static void f(Sample c){
c.c = 2;
}
static void g(Sample c){
c = null;
}
}
The output is 0, 2 and 2, while I expect the null for the last output.
Its because reference to Sample is copied so null is assigned to the copied reference and original reference does not changed.
if you want to change original reference you should pass it by reference:
static void g(ref Sample c)
{
c = null;
}
....
g(ref c);

c# Methods error CS0236 [duplicate]

This question already has answers here:
A field initializer cannot reference the nonstatic field, method, or property
(6 answers)
Closed 5 years ago.
Guys i need some help here,
i have error in
Line 50: CS0236 C# A field initializer cannot reference the non-static field, method, or property
Line 51: CS0236 C# A field initializer cannot reference the non-static field, method, or property
namespace ConsoleApplication5
{
public delegate string Metodo(string t1, string t2);
class Program
{
static void Main(string[] args)
{
}
}
public class ExemploDel
{
public static string NomeMetodo(string t1, string t2)
{
return t1 + t2;
}
Metodo meuMetodo = NomeMetodo;
}
public class Metodos
{
public string Method1(string tx1, string tx2)
{
return tx1 + tx2;
}
public string Method2(string tx1, string tx2)
{
return tx2 + tx1;
}
}
public class DelegatesEx
{
public static string NomeMetodo(string t1, string t2)
{
return t1 + t2;
}
Metodos obj = new Metodos();
Metodo m1 = obj.Method1;
Metodo m2 = obj.Method2;
Metodo m3 = NomeMetodo;
}
}
First of all, you have placed the calling code at wrong place, the following 4 lines should go in the Main method of your program:
Solution1:
class Program
{
static void Main(string[] args)
{
Metodos obj = new Metodos();
Metodo m1 = obj.Method1;
Metodo m2 = obj.Method2;
Metodo m3 = NomeMetodo;
}
}
Secondly, you have marked your delegate as static while you are trying to assign it instance method, so either make the delegate non-static in declaration and make the methods static.
public delegate string Metodo(string t1, string t2);
Solution2:
if you want to keep the Metodo delegate static, then your methods should also be satic like:
public class Metodos
{
public static string Method1(string tx1, string tx2)
{
return tx1 + tx2;
}
public static string Method2(string tx1, string tx2)
{
return tx2 + tx1;
}
}
and now your main method code will also be changed, as static methods are access using the class not instance of it:
class Program
{
static void Main(string[] args)
{
Metodo m1 = Metodos .Method1;
Metodo m2 = Metodos .Method2;
Metodo m3 = NomeMetodo;
}
}

while overidding base methods, why default arguments are not working well? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# optional parameters on overridden methods
using System;
namespace Apple
{
class A
{
public virtual void Func(int a=4){
Console.WriteLine(" A Class: "+a);
}
}
class B : A
{
public override void Func(int a = 12)
{
Console.WriteLine(" B Class: "+ a);
}
}
public class Program
{
static void Main(string[] args)
{
A ob = new B();
ob.Func();
Console.ReadLine();
}
}
}
// Output: B Class: 4
Default parameters are filled at compile time, and the code references a variable ob through the base class. The virtual method override works at run time, as expected. You could achieve the desired effect by using method overload:
class A
{
public void Func(int value)
{
}
public virtual void Func()
{
Func(4);
}
}
class B: A
{
public override void Func()
{
Func(12);
}
}
The compiler places the the default parameter value based on the type of the object and is done during the compile time.
Hence the compiled code would look like:
using System;
namespace Apple
{
public class Program
{
private static void Main(string[] args)
{
A ob = new B();
ob.Func(4);
Console.ReadLine();
}
}
}
You could get the desired result by doing this:
public class Program
{
static void Main(string[] args)
{
A ob = new B();
((B)ob).Func();
Console.ReadLine();
}
}
Because you creating instance of Class A which is referring to the address of class B.
A ob = new B();
Since the instance is of class A, the method you calling is pointing to method in class A.
You can check this by putting debug and then execute the code.
instead if you create instance of class B ie
B ob = new B();
it will call the method Fun() from class B and will display output as
" B Class: 12"
the default parameter value is Static binding.

Categories