pass ref parameter by value and set it to null - reference

Consider the following code Snippet
Form form2 = new Form();
test(form2);
form2.Show();
public void test(Form f)
{
f = null;
}
Since f also holds "another" reference to Form2, setting f to null should set Form2 to null as well, which it doesn't. Need a good explanation to understand this.

The reference set to null is the local copy of the form2 reference. As the reference is passed by value, meaning making an exact copy and passing the copy, the original remains untouched.
The value passed here can be seen as a memory address (which is not exactly the case vith VMs but it is a helpful and adequate metaphor).
In the test method, you set a variable holding a copy of this address to null. This has no further consequences whatsoever.
The case is very different if you use the address stored in the variable to access and change the actual Object the address refers to. You are changing the real thing here, so all changes remain after your local variable runs out of scope.
To take one more step back :
You can see the variable as a slip of paper with an address of a friend (your object).
If you burn the paper (setting the variable to null), your friend is not affected.
If you use the paper to visit the address and give your friend a present or slap him in the face (calling a method on the object behind the variable), he is definitely affected and you have to live with the consequences

(I'm assuming this is Java.)
Method parameters are always passed by value. That means their contents are always copied to a new variable. In this case the contents of the variable f, which contains a reference to an object, are copied to a new variable. When that new variable's contents are replaced with null, the contents of the original variable are unaffected -- they still point to the original object.

Related

Setting a Constant independent of executed Module

I try to Set a constant which should contain the Connection String for my Database. But the Setup for my macro is so that on the first Sheet in my Excel there are a lot of Buttons. Depending on which Button the user clicks another Module gets executed, but all of them connect to the same DB at some point.
Since I don't want to change the Connection String at many different points it should be done with one Constant.
But I can never tell which Module gets executed an which doesn't.
Therefore I created a new Module which only contains one Line of Code:
Public Const ConnectionString As String = "Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=X;User=root;Password=X;"
Unfortunately when a Button is clicked and some Module/Sub gets executed, the Variable "ConnectionString" Contains no Value.
How can I make sure that independent of the called Sub/Module the Constant is set?
A Public constant must be used in all contexts (modules, classes, controls, forms etc.), except two cases:
a. Option Private Module is in effect;
b. The constant has also been declared in modules/procedures (Dim ConnectionString As String) but no value has been allocated. If, after declaration, a value would be allocated (ConnectionString = "xxx"), then VBA will return "xxx". So looking for ConnectionString As String in all project, the place of the real problem will be descovered, I think.
But at least part of the credit must go to #BrakNicku, who firstly put in discussion the second possibility...

If instances are really just pointers to objects in Python, what happens when I change the value of a data attribute in a particular instance?

Suppose I define "Class original:" and create a class attribute "one = 4." Then I create an instance of the class "First = original()." My understanding is that First now contains a pointer to original and "First.one" will return "4." However, suppose I create "Second = original()" and then set "Second.one = 5." What exactly happens in memory? Does a new copy of Class original get created with a class attribute of 5?
I've created a Class original with class attribute one. I then created two instances of this class (First and Second) and verified that id(First.one) and id(Second.one) are pointing to the same place. They both return the same address. However, when I created Third=original() and set Third.one = 5 and then check id(Thrid.one) it appears to be pointing somewhere else. Where is it pointing and what happened? When I check original.one it still returns "4" so obviously the original object is not being modified. Thanks.
It appears you are asking about a piece of code similar to this:
class Original:
def __init__(self, n):
self.one = n
first = Original(4)
second = Original(4)
third = Original(5)
print(id(first.one))
# 140570468047360
print(id(second.one))
# 140570468047360
print(id(third.one))
# 140570468047336
Suppose I define "Class original:" and create a class attribute "one = 4." Then I create an instance of the class "First = original()." My understanding is that First now contains a pointer to original
No. The variable references the instance you created, not the class. If it referenced the class, there would be no way for you to get at the instance you just created.
The instance will, somewhere in its object header, of course contain a pointer to its class. Without that pointer, method lookup wouldn't be possible, since you wouldn't be able to find the class from the instance.
and "First.one" will return "4."
Yes. The attribute one of first contains a pointer to the object 4 (which is an instance of the class int).
[Note that technically, some Python implementations will perform an optimization and actually store the object 4 directly in the attribute instead of a pointer to the object. But that is an internal implementation detail.]
However, suppose I create "Second = original()" and then set "Second.one = 5." What exactly happens in memory? Does a new copy of Class original get created with a class attribute of 5?
No. Why would you need a separate copy of the class? The methods are still the same for both instances. In fact, that is precisely the reason why methods in Python take the instance as their first argument! That way, there need only be one method. (This is actually the same in every OO language, except that in most other languages, this argument is "invisible" and only accessible using a special keyword like self in Ruby, Smalltalk, Self, and Newspeak or this in Java, C#, and Scala.)
I've created a Class original with class attribute one. I then created two instances of this class (First and Second) and verified that id(First.one) and id(Second.one) are pointing to the same place. They both return the same address. However, when I created Third=original() and set Third.one = 5 and then check id(Thrid.one) it appears to be pointing somewhere else.
It is not quite clear to me what your question is here. first.one and second.one both point to 4, so they both point to the same ID since they both point to the same object. third.one points to 5, which is obviously a different object from 4, so naturally, it has a different ID.
It is, in fact, one of the requirement of IDs that different objects that exist at the same time must have different IDs.
Where is it pointing and what happened?
Again, it is not quite clear what you are asking.
It is pointing at 5, and nothing happened.
When I check original.one it still returns "4" so obviously the original object is not being modified.
Indeed, it isn't. Why would it be?

Object reference exactly

Demo demo=new Demo();
Here demo is a reference variable. Are reference variables equal to object references... ?
If no then please explain the concept of object reference.
Demo represents the type of the object.
demo represents the object reference, you will be able to refer to new Demo() object and calling its methods (for example).
new Demo() represents the object itself which is situated in heap memory.
In case of java language,
When you write the statement
Demo obj = new Demo();
It means that You are declaring a variable named obj and it is of type Demo.
by writing
obj = new Demo();
You are creating a new object in the heap memory and the reference variable "obj" will refer to it so that when you want to access the object created just now, you can access it through reference variable "obj".
so when you want to call some method on Demo object, you can call it using
obj.someMethod();
A reference to an object is a way to denote that object. The address of an object, for example, is one kind of reference (probably the simplest kind). Other kinds of references can exist, too, and they are written and read using some more complicated logic. It could theoretically be a double pointer, a pseudo-address, or something else - as long as it contains enough information that (if interpreted in a specified way) can be used to denote a specific object, .
A reference variable is a variable whose value is a reference to an object. So, for example, a reference variable could be a variable whose value is the address of an object, or (as I described above) something different, but equivalent.
For comparison, the other common type of variable (called primitive type in Java, value type in C# and other names in other contexts) is the kind variable whose value is an actual object (instead of a reference).

Passing a String by Reference in Metro/C++?

I'm working on a C++ Metro style app and have to pass a string by reference (somehow). At first, I passed the String^ which doesn't work because strings are immutable how I have found out.
What would be a proper way to pass a string by reference?
Edit: OK, it seems that it's not that easy since the answers and comments suggest to use return values. But as far as I think this is not applicable in my situation: In this Metro app I have two pages and a string should be "shared" across those two pages.
So in the main page I do this in a click event:
this->Frame->Navigate(newPage, this->TestString);
In the OnNavigatedTo event of the second page I convert the second parameter to a String^ and change it. Then I use this->Frame->GoBack() to navigate back to the first page. There I'd like to have access to the changed string. Unfortunately, GoBack() doesn't allow to pass any parameters as far as I know.
You can use a tracking reference:
void ModifyTheParameter(String^% value) {
value = gcnew String("Blah");
}
That would modify the original variable you passed in as parameter (see MSDN for more info and examples). It would then be used just as any other method taking a String^ parameter.
But if possible, avoid using tracking references as parameters. I'd recommend just returning a String^ and assigning that to the original variable.
Yet another possibility: You could just create some kind of View-agnostic DataModel that contains your String (and possibly other data that you work with). You could then pass that DataModel to your method. Since the DataModel variable isn't changed (just a property of it), you wouldn't need to pass a reference to it.
See below an example of a function f which takes as a parameter a reference to a std::string.
std::string someString;
void f(std::string& s);
f(someString);

Is it possible to take the name of a variable and turn it into a string in ActionScript 3.0?

I am making a simple debugger window in ActionScript for myself where I can add and remove variables I want to track. I was to be able to add variables to the list by just doing something like
DebuggerMonitor.trackVar(variable).
My question is, is there any way I can turn "variable" itself (the name, not the value) into a String to be added into a text field?
Depending on how "intelligent" your debugger should be, you could just pass the name along:
DebuggerMonitor.trackVar( variable, "variable" );
since obviously, when used in a context like this, the name should be known at the time you are writing the program.
You can also do some reflection magic to get instance variable names, but it won't work for temp variables (their names are dropped at compilation time):
public function getVariableName( instance:*, match:* ):String {
var typeDescription:XML = describeType( instance );
var variables:XMLList = typeDescription..variable;
var accessors:XMLList = typeDescription..accessor;
for each(var variable:XML in variables)
if(matchesXMLName( instance, variable, match ))
return variable.#name;
for each(var accessor:XML in accessors)
if(matchesXMLName( instance, accessor, match ))
return accessor.#name;
return "No name found.";
}
private function matchesXMLName( instance:*, xml:XML, match:* ):Boolean {
return match == instance[xml.#name.toString()];
}
var varName:String = getVariableName ( myObject, variable );
Using reflections like this will also be quite costly, if used often - you will have to think of a way to cache the type descriptions.
I recommend you check out the as3commons reflections package - there is a lot of useful functionality in there...
Short answer - No :(
You can access the type name but not individual instance names, as these are lost at run-time.
There is a confusion caused by the keyword 'var' because it is used to create several types of bindings.
Lexical bindings (the keyword 'var' was used inside a function).
Dynamic bindings (the keyword 'var' was used to declare a class' field).
Lexical bindings are interpreted by the compiler at compile time as addresses of the registers of the registers space occupied by the function. The names given to lexical bindings perish at this time and it is not possible to restore them at runtime - therefore you can't get the "name" of the variable.
Dynamic bindings are a kind of "public API" of the objects that declare them, they may be accessed from the code that was not compiled together with the code that created them, this is why, for the purpose of reflection the names of these bindings are stored in compiled code. However, ActionScript has no way of referencing LHS values, so you cannot, even if you know the name of the variable and the object declaring it, pass it to another function. But you can look it up in the debugger or by calling describeType on the object declaring the variable. Note that describeType will not show information on private variables even if you are calling it from the scope of the object in question.

Resources