Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the variable name passed to a function

Tags:

c#

.net

asp.net

Let me use the following example to explain my question:

public string ExampleFunction(string Variable) {     return something; }  string WhatIsMyName = "Hello World"; string Hello = ExampleFunction(WhatIsMyName); 

When I pass the variable WhatIsMyName to the ExampleFunction, I want to be able to get a string of the original variable's name. Perhaps something like:

Variable.OriginalName.ToString() 

Is there any way to do this?

like image 259
GateKiller Avatar asked Sep 16 '08 13:09

GateKiller


People also ask

What is the variable name that is used by a function to receive passed value?

The data passed to the function are referred to as the "actual" parameters. The variables within the function which receive the passed data are referred to as the "formal" parameters.

What is a variable function?

Introduction. If name of a variable has parentheses (with or without parameters in it) in front of it, PHP parser tries to find a function whose name corresponds to value of the variable and executes it. Such a function is called variable function. This feature is useful in implementing callbacks, function tables etc.

How do you pass a variable name as an argument in Python?

Use a dict. If you really really want, use your original code and do locals()[x][0] = 10 but that's not really recommended because you could cause unwanted issues if the argument is the name of some other variable you don't want changed. Show activity on this post. Show activity on this post.

Can the variable be the name of the function?

Variables and functions have separate name spaces, which means a variable and a function can have the same name, such as value and value(), and Mata will not confuse them.


2 Answers

What you want isn't possible directly but you can use Expressions in C# 3.0:

public void ExampleFunction(Expression<Func<string, string>> f) {     Console.WriteLine((f.Body as MemberExpression).Member.Name); }  ExampleFunction(x => WhatIsMyName); 

Note that this relies on unspecified behaviour and while it does work in Microsoft’s current C# and VB compilers, and in Mono’s C# compiler, there’s no guarantee that this won’t stop working in future versions.

like image 145
Konrad Rudolph Avatar answered Sep 20 '22 17:09

Konrad Rudolph


This isn't exactly possible, the way you would want. C# 6.0 they Introduce the nameof Operator which should help improve and simplify the code. The name of operator resolves the name of the variable passed into it.

Usage for your case would look like this:

public string ExampleFunction(string variableName) {     //Construct your log statement using c# 6.0 string interpolation     return $"Error occurred in {variableName}"; }  string WhatIsMyName = "Hello World"; string Hello = ExampleFunction(nameof(WhatIsMyName)); 

A major benefit is that it is done at compile time,

The nameof expression is a constant. In all cases, nameof(...) is evaluated at compile-time to produce a string. Its argument is not evaluated at runtime, and is considered unreachable code (however it does not emit an "unreachable code" warning).

More information can be found here

Older Version Of C 3.0 and above
To Build on Nawfals answer

GetParameterName2(new { variable });  //Hack to assure compiler warning is generated specifying this method calling conventions [Obsolete("Note you must use a single parametered AnonymousType When Calling this method")] public static string GetParameterName<T>(T item) where T : class {     if (item == null)         return string.Empty;      return typeof(T).GetProperties()[0].Name; } 
like image 32
johnny 5 Avatar answered Sep 16 '22 17:09

johnny 5