Solved

C# set to null in constructor

Posted on 2010-08-13
10
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

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!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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