mathieu_cupryk
asked on
ref out parameters.
I have the following:
InitialPrices.localhost.In itialPrice ReportingS ervice WS
= new InitialPrices.localhost.In itialPrice ReportingS ervice();
int Id = 0;
string Status = null;
WS.GetGradeCodeDetailID("W HT", ref Id, ref Status); // These values need to comback from the webservice the id and status.
I am not sure how to work with ref and out.
The method is as following:
[WebMethod(Description = "Get Grade Code Detail based on the grade name")]
public void GetGradeCodeDetailID(strin g GradeName, out int GradeCodeDetailID, out string Status)
{
string procedureName = "init_price.PRICE_LIST_REP ORTING.get _grade_cod e_dtl_id";
try
{
Database db = DatabaseFactory.CreateData base("Init ialPrices. Properties .Settings. Connection String");
DbCommand dbCommand = db.GetStoredProcCommand(pr ocedureNam e);
db.AddInParameter(dbComman d, "p_grade_name", DbType.String, GradeName);
db.AddOutParameter(dbComma nd, "p_grade_code_dtl_id", DbType.Int32, 4);
db.AddOutParameter(dbComma nd, "p_status", DbType.String, 7);
db.ExecuteNonQuery(dbComma nd);
GradeCodeDetailID = Convert.ToInt32( dbCommand.Parameters[1].Va lue, CultureInfo.InvariantCultu re);
Status = dbCommand.Parameters[2].Va lue.ToStri ng();
}
catch (Exception ex)
{
throw (ex);
}
return;
}
InitialPrices.localhost.In
= new InitialPrices.localhost.In
int Id = 0;
string Status = null;
WS.GetGradeCodeDetailID("W
I am not sure how to work with ref and out.
The method is as following:
[WebMethod(Description = "Get Grade Code Detail based on the grade name")]
public void GetGradeCodeDetailID(strin
{
string procedureName = "init_price.PRICE_LIST_REP
try
{
Database db = DatabaseFactory.CreateData
DbCommand dbCommand = db.GetStoredProcCommand(pr
db.AddInParameter(dbComman
db.AddOutParameter(dbComma
db.AddOutParameter(dbComma
db.ExecuteNonQuery(dbComma
GradeCodeDetailID = Convert.ToInt32( dbCommand.Parameters[1].Va
Status = dbCommand.Parameters[2].Va
}
catch (Exception ex)
{
throw (ex);
}
return;
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
No much difference between 'ref' and 'out'. But you need to know it.
An argument passed to a ref parameter must first be initialized. 'out' parameter does not have to be explicitly initialized before being passed to an out parameter. This is major difference. So you may use either of two.
See msdn for more information. They have good example and good explanations:
'ref':
http://msdn.microsoft.com/en-us/library/14akc2c7(VS.71).aspx
'out':
http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.71).aspx
An argument passed to a ref parameter must first be initialized. 'out' parameter does not have to be explicitly initialized before being passed to an out parameter. This is major difference. So you may use either of two.
See msdn for more information. They have good example and good explanations:
'ref':
http://msdn.microsoft.com/en-us/library/14akc2c7(VS.71).aspx
'out':
http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.71).aspx
if you can, it is a good idea not to use out and ref parameters for web services as they are stateless.
if you must then make sure you read http://kbalertz.com/322624/Proxy-Class-First-Parameter-Service-Method-Returns-Return-Value-Reference.aspx
if you must then make sure you read http://kbalertz.com/322624/Proxy-Class-First-Parameter-Service-Method-Returns-Return-Value-Reference.aspx
ASKER
I get a no overload for:
InitialPrices.InitialPrice ReportingS erviceWS.I nitialPric eReporting Service WS = new InitialPrices.InitialPrice ReportingS erviceWS.I nitialPric eReporting Service();
int GradeCodeDetailID;
string Status;
WS.GetGradeCodeDetailID(gr adeName, out GradeCodeDetailID, out Status);
in the web service:
InitialPrices.InitialPrice
int GradeCodeDetailID;
string Status;
WS.GetGradeCodeDetailID(gr
in the web service:
ASKER
[WebMethod]
public void GetGradeCodeDetailID(strin g gradeName, out int GradeCodeDetailID, out string Status)
{
string procedureName = "init_price.PRICE_LIST_REP ORTING.get _grade_cod e_dtl_id";
try
{
Database db = DatabaseFactory.CreateData base("Init ialPrices. Properties .Settings. Connection String");
DbCommand dbCommand = db.GetStoredProcCommand(pr ocedureNam e);
db.AddInParameter(dbComman d, "p_grade_name", DbType.String, gradeName);
db.AddOutParameter(dbComma nd, "p_grade_code_dtl_id", DbType.Int32, 4);
db.AddOutParameter(dbComma nd, "p_status", DbType.String, 7);
db.ExecuteNonQuery(dbComma nd);
GradeCodeDetailID = Convert.ToInt32(dbCommand. Parameters [1].Value, CultureInfo.InvariantCultu re);
Status = dbCommand.Parameters[2].Va lue.ToStri ng();
}
catch (Exception ex)
{
throw (ex);
}
}
public void GetGradeCodeDetailID(strin
{
string procedureName = "init_price.PRICE_LIST_REP
try
{
Database db = DatabaseFactory.CreateData
DbCommand dbCommand = db.GetStoredProcCommand(pr
db.AddInParameter(dbComman
db.AddOutParameter(dbComma
db.AddOutParameter(dbComma
db.ExecuteNonQuery(dbComma
GradeCodeDetailID = Convert.ToInt32(dbCommand.
Status = dbCommand.Parameters[2].Va
}
catch (Exception ex)
{
throw (ex);
}
}
Yeah, well. I'd agree with Solar_Flare. But I can't really see any problems for this specific case. And I'd use 'out' parameters too, not 'ref'.
ASKER
Great job!
If the parameter is declared as an out parameter, like the following:
public function muFunction(ref int param1)
then it needs to be called as follows:
int myParam1 = 1234;
myFunction(ref myParam1);
// now do something with myParam1 and its new value
if the function uses an out parameter like the following:
public function muFunction(ref int param1)
then it is used and called as follows:
int myParam1;
myFunction(out myParam1);
// now do something with myParam1 and its new value