Problem with function argument
-
i am working on a project in which i'm dealing with business layer and data layer where business layer has a reference of data layer. when business layer call a function of data layer sending ref object variable as parameter function signature in data layer is: "public bool add(ref object objagent)" this ref object variable is actually an instance of business layer class, but when i proceed with function assigning values like: string stragentname; stragentname=objagent.agentname; upon complilation it gives me error:"object doesnot contain a definition for agentname"
-
i am working on a project in which i'm dealing with business layer and data layer where business layer has a reference of data layer. when business layer call a function of data layer sending ref object variable as parameter function signature in data layer is: "public bool add(ref object objagent)" this ref object variable is actually an instance of business layer class, but when i proceed with function assigning values like: string stragentname; stragentname=objagent.agentname; upon complilation it gives me error:"object doesnot contain a definition for agentname"
That's because you are passing the thing as an object, instead of whatever type it really is. Your method signature should probably be something like this:
public bool Add( Agent agent )
{
string agentName = agent.agentName;
...
}From what you've shown, there is no need to have the
ref
keyword in there. Also, you don't need Hungarian notation with C#. Matt Gerrans -
That's because you are passing the thing as an object, instead of whatever type it really is. Your method signature should probably be something like this:
public bool Add( Agent agent )
{
string agentName = agent.agentName;
...
}From what you've shown, there is no need to have the
ref
keyword in there. Also, you don't need Hungarian notation with C#. Matt Gerransthanks Matt but i can not declare Agent type in this function, because it is defined in data layer that do not have reference of business layer. therefore, i have to declare object type. :)
-
thanks Matt but i can not declare Agent type in this function, because it is defined in data layer that do not have reference of business layer. therefore, i have to declare object type. :)
If you can't reference the class, then you can't do anything with it (unless you use reflection). If you don't want the data layer to know the business class, then make an interface with the properties you want to expose, and make the business class implement it, and reference the interface in the data layer. --- b { font-weight: normal; }
-
If you can't reference the class, then you can't do anything with it (unless you use reflection). If you don't want the data layer to know the business class, then make an interface with the properties you want to expose, and make the business class implement it, and reference the interface in the data layer. --- b { font-weight: normal; }
Guffa's suggestion is probably the way you should go. Another quick-and-dirty option, if Add() only needs a few things, is to use parameters. For example:
Add( string agentName, int agentId );
Another one that's even more of a hack is to pass an attribute/value Hashtable. Matt Gerrans