This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get name of a variable or parameter
I have this function.
public void AddVariable( String str)
{
Response.Write(str); // will write the value of str
}
But I need to write the name of the string variable that is passed into the function.
For example:
String temp = "test Variable";
AddVariable(temp);
Here I need to get the name of the variable inside my function, Instead of value.
ie, I need to get 'temp' inside my function, Instead of 'test Variable'.
Is it possbile?
You could get the name of the variable inside the function ("str"), but you cannot get the name of the variable that was passed into the function unless you pass it in as a second parameter.
It doesn't make sense to get the name of the variable passed to the function because there may not have even been a variable if a literal was passed such as AddVariable("test variable").
You don't need parameter name (possible from reflection) but rather a variable name that has been passed as a parameter. AFAIK, for all practical purpose, this is not possible for the code within the function.
On the other hand, it's as such possible to do the code analysis of all assemblies loaded in the app-domain and find all invocations to your method and then do the stack-walk to determine possible invocation so that you may able to nail the variable name in the calling method (again if there can be many invocations in calling method, making it difficult to guess the variable name and you have to then rely on IL offset etc) but its just too convoluted.
Perhaps, you can state the reason for such requirements, there can be some alternative. For example, you can get and log stack trace within your method code that can be used for say trouble-shooting.
Related
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
Closed 9 years ago.
In the following constructor, is the 'this' keyword required? I know I can remove it, it complies and everything is okay. If I omit 'this' will that cause problems for me down the road? Is ommission of 'this' considered bad practice?
// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}
No, this is purely optional in almost all cases. The only reason it is required in your example is to disambiguate between local variables and parameters and member variables that happened to have the same identifier name.
You could avoid the use of this by renaming either the parameter or the field to something unique.
Yes, you need it in your code example. C# will always assume you mean local variable, so if a member variable and a local variable exist with the same name, you must use this, otherwise it will be assumed that you are referring to the local variable.
In your code example, if you neglect the this, you are effectively assigning name (or alias)'s value to itself (thus accomplishing nothing).
in the case below this is absolutely required. But you can get around it by choosing variable names carefully. In my humble opinion, there is nothing wrong with the example below and I do not mind having to use this. In fact, I often use this even when it is not required just to prompt intellisense and auto-complete in my IDE.
internal class Something
{
private string name;
public Something(string name)
{
this.name = name;
}
}
It's optional, unless you have a member variable named 'name', in that case, your line there will be assigning to itself.
I always like to access all member variables with "this", just to be explicit. Some people like to use "_" as a prefix, or even "m".
In C# it is most common to either name the private variable _Name or m_Name. This also makes it easy to visually match up Properties because if you have a property Name, which uses a local private variable _Name, it is easy to see quickly what it is for.
Without the this keyword in your example, all you are doing is assigning the local variable to the local variable (and not to the private variable). Which is why it compiles, but won't do what you think it is doing.
in your example, if you don't use "this" something worse will happen: "Variable Hiding"
This question already has answers here:
Impossible to use ref and out for first ("this") parameter in Extension methods?
(6 answers)
Closed 7 years ago.
I have created a void extension method which can be used with decimal data types. I wanted to be able to modify the this parameter variable inside the scope of the method. This is the code for my extension method:
public static void SetAndConvertIfHasValue(this decimal assignTo, double? valueToAssign)
{
if (valueToAssign.HasValue)
assignTo = (decimal)valueToAssign.Value;
else
assignTo = 0m;
}
However, when I call it:
data.MyDecimalToSet.SetAndConvertIfHasValue(nullableDouble);
data.MyDecimalToSet is not set to the value in nullableDouble if it has one.
In debug if I step into the extension method, assignTo is changed to the correct value, this change just doesn't bubble up to data.MyDecimalToSet.
At this point I have decided to use a standard method rather than an extension method as a solution to this problem, however I was curious as to why this doesn't work? And whether there is a way around it, or if it simply is impossible?
This doesn't work because when you pass a variable to a method, a copy of that variable is passed. When you operate on that copy, you will only be changing the copy.
Note that this will happen regardless of whether the variable is a reference or a value type. However, if you pass a reference type, you can change the contents of the reference type, since the reference passed to the method will be copied, but it will still reference the original object.
(decimal is a value type, so that last point does not apply to it.)
You are doing it the right way if you use a standard method instead.
I'm using Lua interface on c# to pass an object I created to lua's function.
It successfully calls the function, but lua is keep throwing an error:
LuaInterface.LuaException: /hook.lua:32: attempt to index local 'objj' (a nil value)
This is the c# code:
public class PerObj
{
public string name;
public PerObj()
{
}
}
PerObj obj = new PerObj();
LuaFunction lf = lua.GetFunction ("item.HookMe");
lf.Call(obj);
And here's the lua code:
function item:HookMe(objj)
objj.name= "lalala"
end
The function is actually being called, but I'm not sure it's not working...
Change the function definition to:
function item.HookMe(objj)
objj.name= "lalala"
end
The colon in the original definition means that the function has also the self parameter. Those function are called like this: object:HookMe(). But you want to call it directly, so the colon is not applicable.
Edit:
If you want to keep the function defininition and retain self, call it like this:
lf.Call(null, obj);
To call it passing also the self object:
lf.Call(lua["item"], obj);
It seems like the problem is the design of the Lua method (but it really depends on the intent):
Instead of
function item:HookMe(objj)
-- self not used
objj.name= "lalala"
end
This would work better in the given example:
function item:HookMe()
self.name= "lalala"
end
The reason (as well discussed in other answers) is that declaring a function with the method syntax (:) adds an implied first formal parameter self. The caller can pass anything as the first actual argument but the contract is usually to pass the parent table of the function so it can access its sibling fields.
In this case, name seems to be a sibling of HookMe so the method shouldn't be operating on an arbitrary table passed as objj but instead on self.
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
Closed 9 years ago.
In the following constructor, is the 'this' keyword required? I know I can remove it, it complies and everything is okay. If I omit 'this' will that cause problems for me down the road? Is ommission of 'this' considered bad practice?
// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}
No, this is purely optional in almost all cases. The only reason it is required in your example is to disambiguate between local variables and parameters and member variables that happened to have the same identifier name.
You could avoid the use of this by renaming either the parameter or the field to something unique.
Yes, you need it in your code example. C# will always assume you mean local variable, so if a member variable and a local variable exist with the same name, you must use this, otherwise it will be assumed that you are referring to the local variable.
In your code example, if you neglect the this, you are effectively assigning name (or alias)'s value to itself (thus accomplishing nothing).
in the case below this is absolutely required. But you can get around it by choosing variable names carefully. In my humble opinion, there is nothing wrong with the example below and I do not mind having to use this. In fact, I often use this even when it is not required just to prompt intellisense and auto-complete in my IDE.
internal class Something
{
private string name;
public Something(string name)
{
this.name = name;
}
}
It's optional, unless you have a member variable named 'name', in that case, your line there will be assigning to itself.
I always like to access all member variables with "this", just to be explicit. Some people like to use "_" as a prefix, or even "m".
In C# it is most common to either name the private variable _Name or m_Name. This also makes it easy to visually match up Properties because if you have a property Name, which uses a local private variable _Name, it is easy to see quickly what it is for.
Without the this keyword in your example, all you are doing is assigning the local variable to the local variable (and not to the private variable). Which is why it compiles, but won't do what you think it is doing.
in your example, if you don't use "this" something worse will happen: "Variable Hiding"
This question already has answers here:
How can I get the name of a variable passed into a function?
(23 answers)
Closed 9 years ago.
Duplicate:
Determine the name of the variable used as a parameter to a method
Is there any way to retrieve the name of a parameter that was passed into a method e.g.
int someParameter = 1;
Method(someParameter);
public void Method(int parameter)
{
// I want the name of 'parameter' which will be 'someParameter'.
}
No. It's only the value which is passed in as the argument. All the method gets is the integer. The fact that the expression happened to be just evaluating a variable is unknown as far as your method is concerned.
No, there is no way.
This will be followed by a bunch of people showing weird lambda expression ways to change the call site and kinda get the name, but the short answer is no.
The only way perhaps will be with annotations with run-time retention. But then, it will be the name of the annotation, not of the parameter itself. A parameter name is just a syntactic artifact of the language. It does not get carried over to the compiled result.