Reinitializing an object not working

I have some problems with returning an object to some default values.

So I have an object called a point of type MPoint (my own class). If point is used more than a certain number of times, I want to reset it back to the default. So I created a method called CheckPointExpired shown below.

The problem is that the method gets called and if the Point's PointUsed property is greater than MaxResuse, it sets the point object to null and creates a new point. However, when debugging my code in the RunPoint method, I see that the point object hasn't changed its value. I do not understand why?

As I understand it, when I pass a point object to the CheckPointExpired method, it is passed as a reference, so any change to the object should be reflected back to the RunPoint method? Obviously I don't understand anything.

  public MPoint RunPoint(MPoint point)
  {
      // first check if point has expired
      CheckPointExpired(point)

      // rest of my code
  }

  void CheckPointExpired(MPoint point)
  {
     if (point.PointUsed >= point.MaxResuse)
     {
        int lvl = point.Level;
        int maxLB = point.MaxLookBack;
        int maxReuse = point.MaxResuse;
        int order = point.Order;            

        point = null;
        point = new MPoint(maxReuse, maxLB, lvl, order);
     }
  }

      

+3


source to share


2 answers


You need to pass the dot as a reference using the keyword ref

:

void CheckPointExpired(ref MPoint point)
{
    if (point.PointUsed >= point.MaxResuse)
    {
       int lvl = point.Level;
       int maxLB = point.MaxLookBack;
       int maxReuse = point.MaxResuse;
       int order = point.Order;            

       point = null;
       point = new MPoint(maxReuse, maxLB, lvl, order);
   }
}

CheckPointExpired(ref point);

      

Otherwise, you can only change the field or point properties and they will be visible on the original object, but the assignment will not change the argument passed. Even though the classes are reference types, when you don't use the ref

parameter there is actually a copy of the original reference.Eg:



string foo = "Foo";
string bar = foo;

      

Here, the assignment bar = foo

copies the reference of foo to bar. So there are two different links that point to the same location. When you initialize a bar with a different value, it just discards the old link and has no effect on foo

:

bar = "bar";

      

+4


source


You forgot to reference the function to change the value. You passed it by value / read-only in order to function. ref

Keyword for reference, The facility passes the object by its address, which allows you to edit and / or modify the object within a function.

void CheckPointExpired(ref MPoint point)
  {
     if (point.PointUsed >= point.MaxResuse)
     {
        int lvl = point.Level;
        int maxLB = point.MaxLookBack;
        int maxReuse = point.MaxResuse;
        int order = point.Order;            

        point = null;
        point = new MPoint(maxReuse, maxLB, lvl, order);
     }
  }

      



You will also need to pass the address to the function, otherwise the function will not be able to read the address of your object.

public MPoint RunPoint(MPoint point)
  {
      // first check if point has expired
      CheckPointExpired(ref point)

      // rest of my code
  }

      

+1


source







All Articles