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