Solved

C# set to null in constructor

Posted on 2010-08-13
10
408 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 
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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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