Solved

C# set to null in constructor

Posted on 2010-08-13
10
393 Views
Last Modified: 2012-05-10
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
Comment
Question by:MatthewOsosky
  • 3
  • 2
  • 2
  • +3
10 Comments
 

Accepted Solution

by:
Daxxad earned 125 total points
Comment Utility
0
 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 125 total points
Comment Utility
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
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
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
 
LVL 11

Expert Comment

by:Kusala Wijayasena
Comment Utility
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
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
Hi,

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

Regards,
VSS
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
Comment Utility
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
 
LVL 3

Assisted Solution

by:man2002ua
man2002ua earned 125 total points
Comment Utility
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
 
LVL 5

Assisted Solution

by:KiasChaos83
KiasChaos83 earned 125 total points
Comment Utility
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
 
LVL 1

Author Comment

by:MatthewOsosky
Comment Utility
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
 
LVL 5

Expert Comment

by:KiasChaos83
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now