Initializing Interface object in a method returns as null object

  • Thread starter Thread starter Vaibhav
  • Start date Start date
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;}
}
}
 
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
 
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?
 
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.
 
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;}
}
}
 
Back
Top