Initializing Interface object in a method returns as null object

V

Vaibhav

Hello,
I have created a method which accepts interface object as
parameter. I have not created the passing object just declared it.

Ques: I get the passed Interface object as null? What could be the reason?

I have copied code below

namespace TestInterfaceProblem
{
public class Program
{
static void Main(string[] args)
{
//Declare interface IEmpolyee to null.
IEmployee employeeData = null;

SaveEmpData(employeeData);

if (employeeData != null)
{
Console.WriteLine("Employee Name: " + employeeData.Name);
Console.WriteLine("Employee Location: " +
employeeData.Location);
}
else
{
Console.WriteLine("Object for employee not created");
}
Console.Read();
}

// Just created for testing purpose.
public static void SaveEmpData(IEmployee empDetails)
{
if (null == empDetails)
{
empDetails = new Employee();
}
else { } // do nothing
}
}

public class Employee : IEmployee
{
public string Name
{
get { return "Allen"; }
}
}

public interface IEmployee
{
string Name { get;}
}
}
 
P

Peter Duniho

Hello,
I have created a method which accepts interface object as
parameter. I have not created the passing object just declared it.

Ques: I get the passed Interface object as null? What could be the
reason? [...]

Because the variable "employeeData" is never set to anything except null.

If you want to use the object that is created in the SaveEmpData() method,
you need to return the reference to that object from that method. You can
do this one of two ways: return the reference as the return value from the
method, or change the "empDetails" argument from "IEmployee empDetails" to
"out IEmployee empDetails". IMHO, the former is very much preferable.

Pete
 
V

Vaibhav

Yes it is out of scope and value can be retrieved if it is ref or out .
Just want to know why is such a behaviour?
 
P

Peter Duniho

Yes it is out of scope and value can be retrieved if it is ref or out .
Just want to know why is such a behaviour?

Why would it work any other way? You seem to be expecting the argument to
be passed by-reference by default. But that's just not how C# works.
It's not how any of the mainstream programming languages work.
 
C

Coskun Sunali [MVP]

Hi Vaibhav,

Not using the "ref" or "out" keywords will pass a copy of the reference to
your interface. This means, any change you make on it will be passed back to
the variable as it is a reference type. However, when you do not pass the
parameters with "ref" or "out" keyword, there is a difference. Initializing
the object passed through the parameter using a "new" keyword will not
affect the original object and will initialize a new instance.

For further reading, please visit
http://msdn.microsoft.com/en-us/lib...x#vclrfpassingmethodparameters_referencetypes .


--
Coskun Sunali
Microsoft MVP - ASP.NET
http://sunali.com
http://propeople.dk

Vaibhav said:
Yes it is out of scope and value can be retrieved if it is ref or out .
Just want to know why is such a behaviour?




Vaibhav said:
Hello,
I have created a method which accepts interface object as
parameter. I have not created the passing object just declared it.

Ques: I get the passed Interface object as null? What could be the
reason?

I have copied code below

namespace TestInterfaceProblem
{
public class Program
{
static void Main(string[] args)
{
//Declare interface IEmpolyee to null.
IEmployee employeeData = null;

SaveEmpData(employeeData);

if (employeeData != null)
{
Console.WriteLine("Employee Name: " + employeeData.Name);
Console.WriteLine("Employee Location: " +
employeeData.Location);
}
else
{
Console.WriteLine("Object for employee not created");
}
Console.Read();
}

// Just created for testing purpose.
public static void SaveEmpData(IEmployee empDetails)
{
if (null == empDetails)
{
empDetails = new Employee();
}
else { } // do nothing
}
}

public class Employee : IEmployee
{
public string Name
{
get { return "Allen"; }
}
}

public interface IEmployee
{
string Name { get;}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top