C# get the name of a method that called another one [duplicate] - c#

This question already has answers here:
How can I find the method that called the current method?
(17 answers)
How do I get the calling method name and type using reflection? [duplicate]
(5 answers)
Get the calling function name from the called function [duplicate]
(2 answers)
How to find the FULL name of the calling method in C#
(5 answers)
Closed 1 year ago.
Here is my problem: I have two classes which each include a method. Method 1 of class 1 calls method 2 of class 2.
Now, I need to store in a local variable of method 2 (of type string probably), the name of the method that called method 2 (i.e. Method1).
For this I have set up a global variable named string str in class 2. However, as we create a new instance of class 2 when we call method 2, we have: str=null.
Here is the code so that you understand the problem:
public class Class1
{
private Class2 _class2 = new Class2();
public Class2 Class2 { get => _class2; set => _class2 = value; }
public void Method1()
{
Class2.data = "Changes I want to Make in Class2";
Class2.Method2();
}
}
public class Class2
{
public string data;
public void Method2()
{
string str = data; //str=null instead of str="Changes I want to Make in Class2"
//Rest of the code here
}
}
I know that str=null since I instantiate the object Class2 = new Class2().
So, I need to find another way around the problem, and I realised that, in my case, getting the name of Method 1 from a local variable in Method 2 would be a solution.
My question is therefore the following: Is there a way to answer this problem?
Note: I am a beginner in computer development, so I apologise in advance if the solution is easy and I have not seen it before.

Related

Can an optional parameter be an empty class object type and NOT null? [duplicate]

This question already has answers here:
Default parameter for value must be a compile time constant?
(7 answers)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm attempting to build an 'Action' abstract class that will be used in many different ways. An Action, in my case, is just a simple repeatable task that is called by various 'Triggers'. I've also created an 'ActionArgs' class to be used as a structure to hold arguments for the Action to be performed. The default 'ActionArgs' object doesn't contain any arguments, which is intended to act as the default. However, I want to be able to create 'Action's and 'ActionArgs' that inherit from their respective parents, but I always want the base empty ActionArgs object to act as the default value for an optional parameter, as some Actions will have parameters, others might not. I'm having problems achieving this. I've tried a few things, but this is what I'm kind of going for:
public abstract class Action
{
public string Name { get; private set; }
public Action(string name) { Name = name; }
public abstract bool PerformAction(ActionArgs args = ActionArgs.Empty);
}
public class ActionArgs
{
public static readonly ActionArgs Empty = new ActionArgs();
public ActionArgs() { }
}
In attempting this, I get an error on the PerformAction() definition, says that a default parameter value must be a compile-time constant. Why is this impossible to resolve this at compile time? I'm guessing it's because it is assuming the worst that you have some run-time variation in the constructor. In my case I clearly do not, but is there any way that I can approach this in a way that doesn't involve using null as the default parameter?
I would use a method overload:
public abstract class Action
{
public string Name { get; private set; }
public Action(string name) { Name = name; }
public bool PerformAction() => PerformAction(ActionArgs.Empty);
public abstract bool PerformAction(ActionArgs args);
}

Is it possible to simplify the following into one row? [duplicate]

This question already has answers here:
static property in c# 6
(3 answers)
Closed 4 years ago.
I apologize for a really bad title. I don't know the correct terminology, but will edit it if you can inform me what I am actually asking.
Is it possible to do the following in one row, like with an auto-property?:
public class MyClass
{
static OtherClass _otherClass;
static OtherClass otherClass => _otherClass ?? (_otherClass = new OtherClass());
}
You could use Lazy<T> for lazy initialization, although it does not simplify the code too much:
static Lazy<OtherClass> _otherClassLazy = new Lazy<OtherClass>(() => new OtherClass());
static OtherClass otherClass => _otherClassLazy.Value;
Lazy<T> initializes the value only once, and only when actually accessed.
If you really want a single line, you can use:
static Lazy<OtherClass> otherClass { get; } = new Lazy<OtherClass>(() => new OtherClass());
At the cost of having to use otherClass.Value when referring to this variable in code.
This solution is preferable to the { get; } = new OtherClass() in case you want to initialize the property only on-demand, not during the initialization of the class itself. This may be desirable in case the constructor is doing a lot of work and the property might not be used in some cases.

I get a null reference on a property in one of my classes [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
Am using winforms in c# with visual studio 2015
This is my class
public class advmessage
{
public string[] message;
}
so then in the load event of form1 I do this
advmessage newadvmessage = new advmessage();
newadvmessage[1]="Hello";
and for that assignment, it throws the exception for null reference and says object reference not set to an instance of an object.
So if I have a class that does not need to be an array, everything works find, but on the ones that use arrays, is where it has the exception.
Also, I do not have a get set or return for any of the classes, and that might b the problem but yet when I added the { get; set; } in there it still had a problem. I could understand a null reference, if I referenced a null string, but I am just trying to set the message property that will be shown in a text box to the user.
Any help would be greatly appreciated.
public class advmessage
{
public advmessage(size)
{
message = new string[size];//or whatever size you want.
}
public string[] message;
}
advmessage newadvmessage = new advmessage(5);
newadvmessage.message[1]= "Hello";

Instantiate a class to call a method or use static methods? [duplicate]

This question already has answers here:
C# static vs instance methods
(8 answers)
Closed 6 years ago.
Let's say I have a class
public class product
{
public string GetName()
{
return "product";
}
public static string GetStaticName()
{
return "product";
}
}
These methods do the same thing but one is static and one isn't.
When i call these method I do this:
product p = new product();
string _ProductName = p.GetName();
and
string _ProductName = product.GetStaticName();
Which method is the best to use for performance etc?
Which method is the best to use for performance etc?
You haven't to address any performance issue here. You just have to decide if this method should be the same for all the instances of the objects you create. If the answer is yes, then it should be a static method. Otherwise, it should be an instance method. It's a design decision, not a performance decision.
By the way, based on your code. I don't think that in this case you have to address even that. You just want to get the name of the product and probably set it. That being said the following is the only thing you need.
public class Product
{
public string Name { get; set; }
}

Getting class name inside the class itself [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# getting its own class name
For Visual Studio, I want to create a class template and use a string field that holds the name of a class itself. Is it possible?
For example:
public class MyClass
{
public string TAG = GetClassName(this);
}
When talking about non-static methods use Object.GetType Method which returns the exact runtime type of the instance (a reference to an instance of the Type Class):
this.GetType().Name
When talking about static methods, use MethodBase Class and its GetCurrentMethod Method :
Type t = MethodBase.GetCurrentMethod().DeclaringType;
//t.Name
However, see this post on SO for more info on this.
public string GetClassName(object item)
{
return typeof(item).Name;
}
Try following:
this.GetType().Name
Type.Name -> Gets the name of the current member.

Categories