how to call non static method into a static? [duplicate] - c#

This question already has answers here:
How do I call a non-static method from a static method in C#?
(11 answers)
Closed 8 years ago.
I have the following code, I want to call data1() from data2()
private void data1()
{
}
private static void data2()
{
data1(); //generates error
}

In oder to invoke an non static method you need to create an object.
Static methods are methods on class level.
"normal" methods are on object leven.
so what you need to do in order to executer the non static method is the following:
class ClassName {
private static void data2() {
var data1Obj = new ClassName();
data1Obj.data1();
}
private void data1() {
//execute code here
}
}
but if you only use data1 in this way you could make that static to

Related

C#, editing this object (ref-type) in an extension method [duplicate]

This question already has answers here:
Passing Objects By Reference or Value in C#
(9 answers)
Impossible to use ref and out for first ("this") parameter in Extension methods?
(6 answers)
Closed 15 days ago.
namespace TEstOfEverythingInCSharp
{
public static class RangeExtension
{
public static void ClearList(this System.Collections.Generic.List<int> thisList)
{
thisList = new System.Collections.Generic.List<int>();
}
}
public class Program
{
public static void Main(string[] args)
{
System.Collections.Generic.List<int> l = new();
l.Add(6);
l.Add(7);
l.Add(8);
l.ClearList();
System.Console.WriteLine(l.Count);
}
}
}
For example, I would like to implement an extension method which clears a list with ints (I know about the method Clear(), this is just an example of editing). This code should outout "0", however it is "3".
It is written in MSDN that for editing objects of value-types we can use "ref this" instead of "this", but the onbjects of ref-types can be edited with a code like this.
Tell me, please, what am I doing incorrectly?

Calling a method based on value of variable [duplicate]

This question already has answers here:
How to call an appropriate method by string value from collection?
(3 answers)
Closed 3 years ago.
I have 1000 methods called method0001, method0002 , ... ,method1000.
I have a variable that takes values between 1 and 1000.
If the value of the variable is x, I'd like to call methodx. For instance, if the value of the variable is 34, I'd like to call method0034. How can I code this in C# please?
Many people are asking what is need for Methodwxyz. Every method is a different type of math question.
i've done this, following the helpful comments but am getting errors (edited the question from earlier)
using System.Collections.Generic;
using UnityEngine;
public class TextControl : MonoBehaviour
{
public static TextControl instance;
void Start()
{
instance = this;
}
// Update is called once per frame
void Update()
{
this.GetType().GetMethod("Template00" + "1").Invoke(this, null);
}
public static string Templates001()
{
// doing something here
}
}
thanks
You could do this through reflection. Edit for a quick sample (forgot invoke parameters). Some tips about reflection:
If you get a nullexception that means it can't find the method
The method you are invoking needs to be public
If you use obfuscation you may not have the same names for the method
Code
public class Program
{
public static void Main(string[] args)
{
Check method1 = new Check(1);
Check method2 = new Check(2);
}
}
public class Check
{
public Check(int x)
{
this.GetType().GetMethod("Method" + x).Invoke(this, null);
}
public void Method1()
{
Console.WriteLine("Method 1");
}
public void Method2()
{
Console.WriteLine("Method 2");
}
}

Writing a void as a parameter [duplicate]

This question already has answers here:
Pass Method as Parameter using C#
(13 answers)
Closed 3 years ago.
(I'm a bit new to programming so if this doesn't make sense just say so)
Let's say that a method takes in a void as parameter. Ex:
method(anotherMethod);
and I want to write the void inside of the brackets rather than writing the void and putting the name inside so rather than
void theVoid() {
doSomethingHere;
}
and then calling it like
method(theVoid());
I wanted to do
method({ doSomethingHere; })
directly, is it possible to do so?
The thing you are trying to do is called "lambda" it's anonymous functions
Here is the documentation for that.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions
Your method will need to be passed a Delegate (if you do not return anything, an Action should be fine)
https://learn.microsoft.com/en-us/dotnet/api/system.action-1?view=netcore-2.2
Method(TheVoid()); // does not compile
Method(TheVoid); // compiles
using System;
public class Example
{
public void Method(Action func)
{
func();
}
public void TheVoid()
{
Console.WriteLine("In example.TheVoid");
}
}
public class Program
{
public static void TheVoid()
{
Console.WriteLine("In TheVoid");
}
public static void Main()
{
var example = new Example();
example.Method(example.TheVoid);
example.Method(() => {
Console.WriteLine("In lambda");
});
example.Method(TheVoid);
}
}
Example of what you are trying to do

What is the reason behind this? (Not to be able to access class field from static method) [duplicate]

This question already has an answer here:
Static methods can't access instance fields?
(1 answer)
Closed 5 years ago.
that it is not possible to access class field from a static method inside the same class? I do not understand why designed like that.
using System;
namespace StaticMethodAccessInstanceField
{
class Program
{
int m = 2;
static void Main(string[] args)
{
Display();
}
static void Display()
{
Console.WriteLine(m);
}
}
}
Note: Above code does not work. When I make field static or remove static from method header, it works.
Because you don't have an instance when you are inside the static method. So there is no way you could identify (let alone access) the variable you want.
Because instance members are only accessible from an instance. Imagine if you created two instances of the Program class, which m should it chose?
You should specify the instance, so for example:
Program p = new Program();
Console.WriteLine(p.m);

How to get the class name which is calling another class static funcaion [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Retrieving the calling method name from within a method (C#)
I have a class say A in which there is a Method called Func1; this function is static.
Now there are some other classes say B, C that use A.Func1
How can I get the class name which contains the function that is calling ?
ie
public class A
{
public static void Func1()
{
// who called me?
}
}
public class B
{
public void CallFunc()
{
A.Func1();
}
}
public class C
{
public void AlsoCallFunc()
{
A.Func1();
}
}
Can use StackTrace class in order to acees that kind of information.
To get calling method name, I sometimes use this function. But you need to check if it works in your specific case:
private static string GetCallingMethodName()
{
const int iCallDeepness = 2;
System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(false);
System.Diagnostics.StackFrame sframe = stack.GetFrame(iCallDeepness);
return sframe.GetMethod().Name;
}
if you need to work with form and want to know which form is calling then use event and sender
maybe you work with the output of
Environment.StackTrace

Categories

Resources