Passing object (pass by reference) to a function does not persist changes - c#

Consider following code, I am trying to implement custom link list.
Expected Output : Start-->123-->11-->NULL
Actual Output : Start-->11-->NULL
class MyLinkList
{
public object data { get; set; }
public MyLinkList Next { get; set; }
}
public static void PrintLinkList(MyLinkList start)
{
Console.Write("Start-->");
while (start != null)
{
Console.Write(start.data + "-->");
start = start.Next;
}
Console.Write("NULL");
}
public static void AddNodeStart( MyLinkList start, object data)
{
MyLinkList newNode = new MyLinkList();
newNode.data = data;
newNode.Next = start;
start = newNode;
}
public static void Main()
{
MyLinkList n = new MyLinkList() { data = 11, Next = null };
AddNodeStart( n,123);
PrintLinkList(n);
Console.ReadLine();
}
The problem is even though node is added to the list in the AddToStart function, its value is not persisted when control come back to main function.
Object is passed by reference then why its value is not getting persisted. if I use 'ref' word then I get the expected result.
Atul sureka

Add ref to your method declaration like so:
public static void AddNodeStart(ref MyLinkList start, object data)
then call it
AddNodeStart(ref n,123);
and it should work.
If you don't, then start is just a variable inside AddNodeStart method - assigning values to it will not change reference stored in n.
That said it is rather a bad taste to do this. Instead consider returning the new node from your add method.
public static MyLinkList AddNodeStart(MyLinkList start, object data)
{
MyLinkList newNode = new MyLinkList();
newNode.data = data;
newNode.Next = start;
return newNode;
}
then call it this way:
n = AddNodeStart(n,123);

Because the reference in C# (also in Java) is passed by value. The actually reference used in the method is the copy of the reference value passed in. (The object being referred is not copied)
Another example is you can't swap 2 objects by passing the references into a method. Since all references are passed by value, what you actually swap is the values of the copied references.

Go the answer from Why use the 'ref' keyword when passing an object?.
Value is not getting persisted because I am creating a new object in AddNodeStart. If I modify the existing object in the function then the changes would be persisted.

Related

Why is a list of transforms within a class deleted when the class instance gets copied to a list?

I have this problem in Unity (or maybe C#) that's very weird to me. Here is a virtual class:
public abstract class ActionTaken : MonoBehaviour {
protected char type;
protected Transform minionTakingAction;
public abstract void activate();
}
And this virtual class is a parent to the one that interests me:
public class AbilityTaken : ActionTaken
{
public int index;
List<Transform> selectedFriendlyMinions;
List<Transform> selectedEnemyMinions;
List<Transform> selectedTiles;
public override void activate()
{
//The value here is 0 !!! And it should be 1...
Debug.Log(selectedEnemyMinions.Count);
if (selectedFriendlyMinions.Count == 0 && selectedEnemyMinions.Count == 0 && selectedTiles.Count == 0 )
{
minionTakingAction.GetComponentInParent<AbilitiesActivation>().activateAbility(index);
}
else
{
minionTakingAction.GetComponentInParent<AbilitiesActivation>().activateAbility(index, selectedFriendlyMinions, selectedEnemyMinions, selectedTiles);
}
}
public AbilityTaken(Transform _minionTakingAction, int abilityIndex, List<Transform> _selectedFriendlyMinions, List<Transform> _selectedEnemyMinions, List<Transform> _selectedTiles)
{
type = 'S';
minionTakingAction = _minionTakingAction;
index = abilityIndex;
selectedEnemyMinions = _selectedEnemyMinions;
selectedFriendlyMinions = _selectedFriendlyMinions;
selectedTiles = _selectedTiles;
//The value here is 1 !!!
Debug.Log(selectedEnemyMinions.Count);
}
public AbilityTaken(Transform _minionTakingAction, int abilityIndex)
{
type = 'S';
minionTakingAction = _minionTakingAction;
index = abilityIndex;
selectedFriendlyMinions = new List<Transform>();
selectedEnemyMinions = new List<Transform>();
selectedTiles = new List<Transform>();
}
}
As you can see in the comments, the value of selectedEnemyMinions list of Transforms changes from the constructor (count value: 1) to the "activate()" function (count value: 0), without me making any changes to it. All I do is:
1. I create a new instance of AbilityTaken giving to the constructor an enemyMinionSelection list with 1 element
2. I add abilityTaken to a list
3. I call activate() from LateUpdate()
AbilityTaken abilityTaken = new AbilityTaken(minionTakingAction, gameMaster.abilitySelected,
// value of enemyMinionSelected.Count here is 1
new List<Transform>(), gameMaster.enemyMinionsSelected, new List<Transform>());
List<ActionsTaken> actionsTaken = new List<ActionTaken>();
actionsTaken.Add(abilityTaken);
private void LateUpdate()
{
if (!actionInProgress &&
actionsTaken.Count>0)
{
ActionTaken currentAction = actionsTaken[0];
currentAction.activate();
}
}
If you can tell me why adding the class instance to List and accessing this instance would cause a member List of Transforms to change their value, that would be great. On the other hand the value of member variable "index" doesn't change (it's always 1). I tried changing Lists to public but that doesn't help as expected.
If you can tell me why adding the class instance to List and accessing this instance would cause a member List of Transforms to change their value, that would be great.
It's not. There's nothing wrong with doing that, it won't change the current values of AbilityTaken.
There is something else going on, that is, the problem is somewhere else. For example, Debug.Log(selectedEnemyMinions); is not printing the Count.
I thoroughly debugged my code and found that the problem here is that I am giving the
constructor of ActionTaken parameters (Namely List) that get changed after assignment in ActionTaken constructor. Then I execute some code that changes the first list of transforms, but due to C# not copying the value, but referencing the assigned value the List of Transforms changes when the firstly assigned value changes.
void function()
{
//Some code here
AbilityTaken abilityTaken = new AbilityTaken(minionTakingAction, gameMaster.abilitySelected, new List<Transform>(), gameMaster.enemyMinionsSelected, new List<Transform>())
//Here we go in the constructor and everything seems fine
// ! Some code here that changes gameMaster.enemyMinionsSelected. The class object doesn't copy the List of Transforms, but references it's address and when it changes, the reference in ActionTaken also changes.
}

Why can't I instantiate an object in a function

So basically I have a class NodeTree:
public class NodeTree
{
public NodeTree(int value, NodeTree left, NodeTree right)
{
Value = value;
Right = right;
Left = left;
}
public int Value { get; set; }
public NodeTree Right { get; set; }
public NodeTree Left { get; set; }
}
And in my main I want to do something like
NodeTree root = null;
Algo.InsertValueIt(root, 8);
Where InsertValueIt() is a method from my static class Algo which does :
public static void InsertValueIt(NodeTree root, int val)
{
var newNode = new NodeTree(val, null, null);
if (root == null)
{
root = newNode;
}
}
Everything is working as expected in my method, but back to my main, my object root is still null.
The reason I am confused is that I give to my method a reference, so it should modify the value of the adress to the new space I am allocating.
I think I can solve my problem by just returning a NodeTree, but is there a way of doing it with a void return type?
You need to define your argument to be passed by reference in order to modify the original value (note the ref keyword):
public static void InsertValueIt(ref NodeTree root, int val) {
...
Also when calling the method, you need to mark the parameter with ref:
Algo.InsertValueIt(ref root, 8);
... otherwise you only modify the local copy in that function.
What happens when you pass a reference type to a method?
The variable declared to be of a reference type holds the memory address where the instance of the type has been allocated (the root in the calling code). The value of this address is copied to a new variable created on the stack for the called method (the root in the InsertValueIt). Using that address through the new variable you will be able to change every public property of the instance or call any methods (provided that the address passed is not null).
What happens now if you call new on this local variable?
A new block of memory is allocated on the heap for the type, the constructor is called to initialize everything, and the memory address of this block is stored in the LOCAL variable inside the InsertValueIt.
Your original one, (the root in the calling code), is unaffected by this change. (still holds null). Using the ref keyword makes the this 'problem' disappear, but I suggest to use a method that creates the Node and return it to the calling method instead.
If you want to understand in more depth this subject I recommend these two articles:
C#Concepts: Value Types vs Reference Types from Joseph Albahari
Parameter Passing in C# from Jon Skeet
Your root value assigned in InsertValueIt is not used in any execution path, so you should add the ref keyword in the parameter declaration

C# Private member shared by all class instances

I am currently working with C# using the Unity3D engine and have come upon the following problem:
I created a class that has two private references to instances of another class which it has to access. Once I create multiple instances of the class and set the references I found out that all instances were using the same variable. I realized this as I was destroying an instance and just before that set the two variables holding the references to null. Immediately after doing that all other instances were throwing NullReferenceExceptions because they were still trying to access the references. The referenced objects are fine, other scripts can still access them.
Here is some pseudo code illustrating the structure:
public class Character
{
// Character data
}
public class StatusEffect
{
private Character target;
private Character originator;
public void Init(Character _Target, Character _Originator)
{
target = _Target;
originator = _Originator;
}
public void Destroy()
{
target = null;
originator = null;
}
}
In the program it would be called like this:
StatusEffect effect = new StatusEffect();
effect.Init(player1, player2);
// Time goes by
effect.Destroy();
After calling Destroy() every StatusEffect's two references will be null.
This is not only an issue when destroying StatusEffects, but also when creating new ones. As soon as I touch the references from within a new instance all StatusEffects will reference the two Characters specified by the new StatusEffect.
I do not understand why or how I can fix this issue. Can someone enlighten me on this matter?
Cheers,
Valtaroth
EDIT:
Here is the real code as requested:
I have a container class holding several StatusEffects. As soon as it starts, it initializes all of them.
public class CElementTag
{
// ..Other data..
public float f_Duration; // Set in the editor
private CGladiator gl_target;
private CGladiator gl_originator;
private float f_currentDuration;
public CStatusEffect[] ar_statusEffects;
// Starts the effect of the element tag
public void StartEffect(CGladiator _Originator, CGladiator _Target)
{
gl_originator = _Originator;
gl_target = _Target;
f_currentDuration = f_Duration;
for(int i = 0; i < ar_statusEffects.Length; i++)
ar_statusEffects[i].Initialize(gl_originator, gl_target);
}
// Ends the effect of the element tag
public void EndEffect()
{
for(int i = 0; i < ar_statusEffects.Length; i++)
{
if(ar_statusEffects[i] != null)
ar_statusEffects[i].Destroy();
}
}
// Called every update, returns true if the tag can be destroyed
public bool ActivateEffect()
{
f_currentDuration -= Time.deltaTime;
if(f_currentDuration <= 0.0f)
{
EndEffect();
return true;
}
for(int i = 0; i < ar_statusEffects.Length; i++)
{
if(ar_statusEffects[i] != null && ar_statusEffects[i].Update())
RemoveStatusEffect(i);
}
return false;
}
// Removes expired status effects
private void RemoveStatusEffect(int _Index)
{
// Call destroy method
ar_statusEffects[_Index].Destroy();
// Remove effect from array
for(int i = _Index; i < ar_statusEffects.Length - 1; i++)
ar_statusEffects[i] = ar_statusEffects[i+1];
ar_statusEffects[ar_statusEffects.Length - 1] = null;
}
}
The actual StatusEffect class is holding the two references as well as some other data it needs to work. It has virtual methods because there are some classes inheriting from it.
public class CStatusEffect
{
// ..Necessary data..
// References
protected CGladiator gl_target;
protected CGladiator gl_originator;
virtual public void Initialize(CGladiator _Target, CGladiator _Originator)
{
gl_target = _Target;
gl_originator = _Originator;
// ..Initialize other necessary stuff..
}
virtual public void Destroy()
{
gl_target = null;
gl_originator = null;
// ..Tidy up other data..
}
virtual public bool Update()
{
// ..Modifying data of gl_target and gl_originator..
// Returns true as soon as the effect is supposed to end.
}
}
That should be all the relevant code concerning this problem.
EDIT2
#KeithPayne I have a static array of ElementTags defined in the editor and saved to xml. At the beginning of the program the static array is loading the xml and stores all element tags. When creating a new element tag to use I utilize this constructor:
// Receives a static tag as parameter
public CElementTag(CElementTag _Tag)
{
i_ID = _Tag.i_ID;
str_Name = _Tag.str_Name;
enum_Type = _Tag.enum_Type;
f_Duration = _Tag.f_Duration;
ar_statusEffects = new CStatusEffect[_Tag.ar_statusEffects.Length];
Array.Copy(_Tag.ar_statusEffects, ar_statusEffects, _Tag.ar_statusEffects.Length);
}
Do I have to use a different method to copy the array to the new tag? I thought Array.Copy would make a deep copy of the source array and stored it in the destination array. If it is in fact making a shallow copy, I understand where the problem is coming from now.
From Array.Copy Method (Array, Array, Int32):
If sourceArray and destinationArray are both reference-type arrays or
are both arrays of type Object, a shallow copy is performed. A shallow
copy of an Array is a new Array containing references to the same
elements as the original Array. The elements themselves or anything
referenced by the elements are not copied. In contrast, a deep copy of
an Array copies the elements and everything directly or indirectly
referenced by the elements.
Consider this fluent version of the StatusEffect class and its usage below:
public class StatusEffect
{
public Character Target { get; private set; }
public Character Originator { get; private set; }
public StatusEffect Init(Character target, Character originator)
{
Target = target.Clone()
Originator = originator.Clone();
return this;
}
//...
}
public CElementTag(CElementTag _Tag)
{
i_ID = _Tag.i_ID;
str_Name = _Tag.str_Name;
enum_Type = _Tag.enum_Type;
f_Duration = _Tag.f_Duration;
ar_statusEffects = _Tag.ar_statusEffects.Select(eff =>
new StatusEffect().Init(eff.Target, eff.Originator)).ToArray();
// ar_statusEffects = new CStatusEffect[_Tag.ar_statusEffects.Length];
// Array.Copy(_Tag.ar_statusEffects, ar_statusEffects, _Tag.ar_statusEffects.Length);
}
Because you're passing in references to the objects via your Init() method, you're not actually "copying" the objects, just maintaining a reference to the same underlying objects in memory.
If you have multiple players with the same references to the same underlying objects, then changes made by player 1 will effect the objects being used by player 2.
Having said all that, you're not actually disposing the objects in your Destory method. Just setting the local instance references to Null which shouldn't affect any other instances of StatusEffects. Are you sure something else isn't disposing the objects, or that you haven't properly init'd your other instances.
If you do want to take a full copy of the passed in objects, take a look at the ICloneable interface. It looks like you want to pass in a copy of the objects into each Player.
public class Character : ICloneable
{
// Character data
//Implement Clone Method
}
public class StatusEffect
{
private Character target;
private Character originator;
public void Init(Character _Target, Character _Originator)
{
target = _Target.Clone()
originator = _Originator.Clone();
}
The fields aren't shared(static) among other instances. So calling target = null; in Destroy() won't affect other instances.
StatusEffect effect1 = new StatusEffect();
effect1.Init(player1, player2);
StatusEffect effect2 = new StatusEffect();
effect2.Init(player1, player2);
// Time goes by
effect2.Destroy();
// Some more time goes by
// accessing effect1.target won't give a `NullReferenceException` here unless player1 was null before passed to the init.
effect1.Destroy();
I think you did forget the Init(..) on the other instances. Every time you create an instance of StatusEffect, you need to call Init(...).
Update:
This line will clear the reference to the effect, but you never recreate it:
ar_statusEffects[ar_statusEffects.Length - 1] = null;
so the next time you call ar_statusEffects[x].Update() or Initialize() etc it will throw a NullReferenceException
If you want to clear out effects within you array, you could create an Enable bool in the effect, this way you only have to set/reset it.
for(int i = 0; i < ar_statusEffects.Length; i++)
if(ar_statusEffects[i].IsEnabled)
ar_statusEffects[i].Update();
Why don't you use a List instead? Arrays will be faster as long you don't have to shuffle in it. (like circulair buffers etc)
Thanks to Keith Payne I figured out where the problem was. I was creating a deep copy of CElementTag, but not of my ar_statusEffects array. I wrongly assumed Array.Copy was creating a deep copy of an array when it actually was not.
I implemented the IClonable interface for my CStatusEffect and use the Clone() method to create a true deep copy for each member of the static array and add it to the new tags ar_statusEffects array. This way I have seperate instances of the effects instead of references to the same static effect.
Thanks to everyone, especially Keith Payne, for their help and support!

Null Exception in Program

I have a program like this
class Program
{
static void Main(string[] args)
{
test objtest = new test();
objtest.Name = "vikas";
Test(objtest);
//objtest = null; when I uncomment this line it shows me exception
Console.WriteLine(objtest.Name);
Console.ReadLine();
}
private static void Test(test objtest)
{
objtest.Name = "chetan";
objtest = null;
}
}
class test
{
private string _Name = string.Empty;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}
Output:
chetan
Second program:
class Program
{
static void Main(string[] args)
{
test objtest = new test();
objtest.Name = "vikas";
Test(objtest);
objtest = null;
try
{
Console.WriteLine(objtest.Name);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException + " " + ex.Message);
}
Console.ReadLine();
}
private static void Test(test objtest)
{
objtest.Name = "chetan";
objtest = null;
}
}
class test
{
private string _Name = string.Empty;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}
Output:
Object reference not to set an instance of object
Why?
When I set objtest = null; in Test it shows me the value, but when I set null in same it shows me error.
Added after #kmatyaszek post:
In first program
static void Main(string[] args)
{
test objtest = new test();
objtest.Name = "vikas"; // **I am assigning this value**
Test(objtest);
//objtest = null; when I uncomment this line it shows me exception
Console.WriteLine(objtest.Name);
Console.ReadLine();
}
private static void Test(test objtest)
{
objtest.Name = "chetan";
objtest = null;
}
Why it is displaying "chetan" not "vikas"??
Classic confusion caused by reference type that is being passed by value.
I will give a rather short and simple answer here; those interested to learn more and in depth are more than welcome to read Jon Skeet article on Parameter passing in C# and similar article with diagrams, by Lee Richardson.
Reference type, in short, is any type that is not primitive or struct. Any custom defined class is hence a reference type.
When instance of such class is passed to a function, what actually happens is that a pointer to this instance is passed. More accurately, a copy of the pointer is being passed since by default parameters are passed by value.
When you have this line:
test objtest = new test();
New instance of the class test is being created and assigned address in the memory. Every time you refer to the variable objtest, that address will be used for example:
objtest.Name = "vikas";
The runtime engine will go the address assigned when the instance was created, look for the place reserved for the property Name and change the contents there to "vikas". The change is immediate and permanent for this instance.
When you have such function signature:
private static void Test(test objtest)
The actual parameter that is passed "behind the scenes" is the address of the instance in the memory. Whenever you refer to the parameter objtest inside the function, the runtime engine will go to the address passed as the actual parameter. So having this line inside the function:
objtest.Name = "chetan";
Is exactly the same as having it outside the function: it will look for the place reserved for the property Name in the memory address passed, and change the contents there to "chetan". Yet again, this change is immediate and permanent for that instance. For this thing (changing properties) it doesn't matter if you're using ref or not, as you are dealing with reference type.
However, being passed by value (e.g. without ref keyword) means that the memory address is being copied and the function only get the copy, very much like passing integer. Any change to the copy will not affect the original value. Thus, when you have this line inside the function:
objtest = null;
You change the copy to point on nothing, however the variable outside the function still point to the same address and won't be null.
If you have such function signature:
private static void Test(ref test objtest)
Then it means the address itself is passed by reference, hence changing the variable holding the address will cause it to be changed outside the function as well.
This pretty much sums it up, I don't bring anything new here just clarifying things with what I deem more simple explanation.
You have problem here with passing parameter to function.
Default parameters are passed by value.
The attempt to reassign the parameter to a different memory location only works inside the method Test and does not affect the original variable objtest in Main method.
So when you add ref to parameter in Test function in two cases behaviour will be the same, because all of the changes that take place inside the method Test affect the original object objtest in Main method.
First example from your question with ref parameter:
class Program
{
static void Main(string[] args)
{
test objtest = new test();
objtest.Name = "vikas";
Test(ref objtest);
//objtest = null; when I uncomment this line it shows me exception
Console.WriteLine(objtest.Name);
Console.ReadLine();
}
private static void Test(ref test objtest)
{
objtest.Name = "chetan";
objtest = null;
}
}
class test
{
private string _Name = string.Empty;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}
In the second example first you set null to original object and after that you want read property Name, so you will get NullReferenceException.
Well, you're using an object with a value of null, which causes the exception. null for reference types means "points to zero". When you're trying to access the value stored at address zero, you get a NullReferenceException.
You're setting objtest to null before you access the Name property, so you're trying to access the Name property of the object stored at address zero, which makes no sense.

Link Two Properties/Variables

Let me explain my situation. I have a program who reads an external connection and gives me an array of integers (or booleans). Those inputs should feed an object that has some properties (X, Y, Z, for example). So, if a read a value on array, i should write those values in the properties. Is there a way to pass those values by ref (for example) ? Thinking logically , the best way way would be pointers (property X pointing to array[0]), but these aren't very unclear to me.
I can create a way to look for changes in array (but is a very large array, +60000), then update my object. But i think this would be a bad ideia.
Sorry if i wrote any crap, i'm just starting on C#.
Some pseudo code to help.
class obj
{
int X {get; set;}
public obj(ref int x)
{
X = x;
}
}
class main
{
void main()
{
int a;
obj test = new obj(ref a);
}
}
So if: a = 10, obj.X = 10 too.
public class MyClass
{
private int[] backingArray;
public int X
{
get
{
if (backingArray == null)
return -1;
else
return backingArray[0];
}
}
public MyClass(int[] array)
{
if (array.Length > 0)
backingArray = array;
}
}
class Main
{
void Main()
{
int[] array = new int[] { 2 };
MyClass test = new MyClass(array);
array[0] = 6;
Console.WriteLine(test.X);//prints 6
}
}
Of course this only works with reference types (arrays are reference types). If you wanted to do this whole thing with a value type, you'd need to "wrap" it in some reference type. You can use a class such as the following to wrap anything if you don't have anything convenient.
public class Wrapper<T>
{
public T Value { get; set; }
}
It's not possible to use ref in the manor that you've shown in the OP. You wouldn't be able to store the value that was passed by reference. If you could, then you could end up passing some value on the stack and then having the created object that holds the reference living longer than the item on the stack. If that happened you would end up with a reference it a location in memory that no longer holds the variable you intended. This was somewhat of a gotcha in C++ that the designers of C# went out of their way to ensure can't happen (at least not without a lot of work).

Categories