Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ref out parameters.

Posted on 2008-06-16
8
Medium Priority
?
334 Views
Last Modified: 2013-12-17
I have the following:


            InitialPrices.localhost.InitialPriceReportingService WS
               = new InitialPrices.localhost.InitialPriceReportingService();
            int Id = 0;
            string Status = null;
            WS.GetGradeCodeDetailID("WHT", 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(string GradeName, out int GradeCodeDetailID, out string Status)
        {
            string procedureName = "init_price.PRICE_LIST_REPORTING.get_grade_code_dtl_id";
            try
            {
                Database db = DatabaseFactory.CreateDatabase("InitialPrices.Properties.Settings.ConnectionString");
                DbCommand dbCommand = db.GetStoredProcCommand(procedureName);

                db.AddInParameter(dbCommand, "p_grade_name", DbType.String, GradeName);
                db.AddOutParameter(dbCommand, "p_grade_code_dtl_id", DbType.Int32, 4);
                db.AddOutParameter(dbCommand, "p_status", DbType.String, 7);
                db.ExecuteNonQuery(dbCommand);
               

                GradeCodeDetailID = Convert.ToInt32( dbCommand.Parameters[1].Value, CultureInfo.InvariantCulture);
                Status = dbCommand.Parameters[2].Value.ToString();
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return;
        }


0
Comment
Question by:mathieu_cupryk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Accepted Solution

by:
Zippit earned 2000 total points
ID: 21798186
change this line:
WS.GetGradeCodeDetailID("WHT", ref Id, ref Status);

to:
WS.GetGradeCodeDetailID("WHT", out Id, out Status);

after the call to 'GetGraceCodeDetailID' the variables ID and Status will have the output values in them.
0
 
LVL 7

Expert Comment

by:Zippit
ID: 21798214
ref and out are used mainly the same way.  They allow the called function to change the values of the parameters and have the updated values returned to the caller.  The difference between the two is that a ref parameter must be initialized before the call to the function is made.  An out parameter does not need to have it's value initialized before the function call is made as the function will initialize the value.

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
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 21798255
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
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 15

Expert Comment

by:Solar_Flare
ID: 21799260
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
0
 

Author Comment

by:mathieu_cupryk
ID: 21805589
I get a no overload for:

 InitialPrices.InitialPriceReportingServiceWS.InitialPriceReportingService WS = new InitialPrices.InitialPriceReportingServiceWS.InitialPriceReportingService();
                            int GradeCodeDetailID;
                            string Status;
                           
                            WS.GetGradeCodeDetailID(gradeName, out GradeCodeDetailID, out Status);
                         


in the web service:

0
 

Author Comment

by:mathieu_cupryk
ID: 21805593
[WebMethod]
        public void GetGradeCodeDetailID(string gradeName, out int GradeCodeDetailID, out string Status)
        {

            string procedureName = "init_price.PRICE_LIST_REPORTING.get_grade_code_dtl_id";
            try
            {
                Database db = DatabaseFactory.CreateDatabase("InitialPrices.Properties.Settings.ConnectionString");
                DbCommand dbCommand = db.GetStoredProcCommand(procedureName);

                db.AddInParameter(dbCommand, "p_grade_name", DbType.String, gradeName);
                db.AddOutParameter(dbCommand, "p_grade_code_dtl_id", DbType.Int32, 4);
                db.AddOutParameter(dbCommand, "p_status", DbType.String, 7);
                db.ExecuteNonQuery(dbCommand);


                GradeCodeDetailID = Convert.ToInt32(dbCommand.Parameters[1].Value, CultureInfo.InvariantCulture);
                Status = dbCommand.Parameters[2].Value.ToString();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
       }
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 21807272
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'.
0
 

Author Closing Comment

by:mathieu_cupryk
ID: 31467800
Great job!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question