Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

C# set to null in constructor

I want ot know if there is a way to have the constructor of a class set the instance equal to null under certain conditions based off the arguments. Example in code, but the compiler will not allow this=null;
public class TOAST
{
     public TOAST(int x)
     {
          if(x%2==0) this = null;
     }
}

Open in new window

0
MatthewOsosky
Asked:
MatthewOsosky
  • 3
  • 2
  • 2
  • +3
4 Solutions
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

You cannot make an object's instance null in that same class. But you can try in some other one as shown in code.

By the way what is the requirement of doing such. Please disclose why you needed this so that we can think accordingly.

Regards,
VSS

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hello obj = new Hello();
            obj = null;

            if (obj != null)
                obj.Call();
            Console.ReadKey();
        }
    }

    class Hello
    {
        public Hello()
        {

        } 

        public void Call()
        {
            Console.WriteLine("Fucker");

        }
    }
}

Open in new window

0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

I am sorry for the line "Fucker". I was writing it as "Trucker". It is just a mistake. I apologize for that nonsense.

Regards,
V.S.Saini
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kusala WijayasenaSoftware EngineerCommented:
Do like this:

-Kusala
public class Toast
{
    public Toast(int x)
    {
        //Do Something
    }
    
    public void MyFunction()
    {
        //Do Something
    }

}

public static class ToastObjectFactory
{
    public static Toast Create(int x)
    {
        if (x % 2 == 0)
        {
            return null;
        }
        else
        {
            return new Toast(x);
        }
    }
}


Toast obj = ToastObjectFactory.Create(10);
if (obj != null)
{
    //Do Something
}

Open in new window

0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

I think he is asking to set instance null not to return value null.

Regards,
VSS
0
 
Kusala WijayasenaSoftware EngineerCommented:
Hi VSS

There isn't explicit way of setting a "instance" null

if you do like

Hello obj = new Hello();
obj = null;

or

Hello obj = null;

both are same

-Kusala

0
 
man2002uaCommented:
only static methods
public class TOAST
    {
        public static TOAST TOASTER(int x){
            if (x % 2 == 0) return null;
            return new TOAST(x);
        }
        public TOAST(int x)
        {
            //if (x % 2 == 0) this = null;
        }
    }
0
 
KiasChaos83Commented:
Hi There,

You need to understand that when you have a variable like

MyType x = new MyType();

x isn't actually the object that is created. x is actually just a pointer to the object created. So if you were to do x = null; then sure the pointer would become null but the object itself still exists in memory until it is garbage collected.

Hope that helps.
0
 
MatthewOsoskyAuthor Commented:
I know that an object factory is a solution, but it's not what I want to know. I'm just looking for a shortcut to assigning the object to be null. The consenus is that it is not possible
0
 
KiasChaos83Commented:
I think that sums it up, it's not possible.

Considering the circumstances, I guess the easiest-to-code approach would be to create a static method.

E.g. instead of doing

MyType x = new MyType();

You could do

MyType x= MyType.Construct(...);

With the static method like

public class MyType
{
public static MyType Construct(...) {
if (condition) return null;
return new MyType();
}
}
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now