How to create a "reference" to an object?
-
Hi, Suppose I have a function which takes in 4 objects as parameters like: methodA(classA obj1, classA obj2, classA obj3, classA obj4) and inside the method, a check will be performed and depending on the result, either the first three objects will be used, or the last three. The way I did this in C++ is basically by defining three pointers, and have those pointers point to either the first three objects or the last three depending on the check. And later those three pointers are used to access the data. But how can I achieve this in C#? Thanks a lot.
-
Hi, Suppose I have a function which takes in 4 objects as parameters like: methodA(classA obj1, classA obj2, classA obj3, classA obj4) and inside the method, a check will be performed and depending on the result, either the first three objects will be used, or the last three. The way I did this in C++ is basically by defining three pointers, and have those pointers point to either the first three objects or the last three depending on the check. And later those three pointers are used to access the data. But how can I achieve this in C#? Thanks a lot.
When you are using objects, you are actually using references to the objects. The four method parameters are references. To get another three references, you just have to declare them in the method:
// Declare the references
classA use1, use2, use3;// Then set the references
if (some condition) {
use1 = obj1;
use2 = obj2;
use3 = obj3;
} else {
use1 = obj2;
use2 = obj3;
use3 = obj4;
} -
When you are using objects, you are actually using references to the objects. The four method parameters are references. To get another three references, you just have to declare them in the method:
// Declare the references
classA use1, use2, use3;// Then set the references
if (some condition) {
use1 = obj1;
use2 = obj2;
use3 = obj3;
} else {
use1 = obj2;
use2 = obj3;
use3 = obj4;
}