Solved

C# set to null in constructor

Posted on 2010-08-13
10
403 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
ID: 33435511
0
 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 125 total points
ID: 33435550
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
ID: 33435554
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
ID: 33435706
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
ID: 33435812
Hi,

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

Regards,
VSS
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 33435836
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
ID: 33436025
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
ID: 33436071
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
ID: 33438421
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
ID: 33438476
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

22 Experts available now in Live!

Get 1:1 Help Now