Solved

Errror message when compiling: Cannot convert from out int to out int?

Posted on 2006-06-08
7
545 Views
Last Modified: 2008-01-09
public DataTable GetEditManager(string tab, string state, int rows, int pageselected, out int nbrpages, string sort, byte sortdir, out int totaledits, string userid)
        {
            int a = nbrpages;
            int b = totaledits;

            if (dtProv == null)
                dtProv = taProv.GetData(tab, state, rows, pageselected, out a, sort, sortdir, out b, userid);
            return dtProv;

        }

taProv is a table adapter in asp.net and GetData is one of the methods that calls a stored procedure with output params.

Can you tell me how to get rid of the error?
0
Comment
Question by:sutorius
[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
  • 4
  • 2
7 Comments
 
LVL 11

Expert Comment

by:anyoneis
ID: 16867278
>>public DataTable GetEditManager(string tab, string state, int rows, int pageselected,
>>      out int nbrpages, string sort, byte sortdir, out int totaledits, string userid)
>>{
>>      int a = nbrpages;
>>      int b = totaledits;
>>....

First off, the two assignments are illegal. You have told the compiler that the nbrpages and totaledits parameters are "out" - meaning, considered uninitialized. So they can't appear on the right hand side of an assignment statement until they have been initialized.

Why not just pass them to GetData and get rid of a and b?

David
0
 
LVL 11

Assisted Solution

by:anyoneis
anyoneis earned 350 total points
ID: 16867299
Err... Because it still won't compile!?!?! Here is a simple console app that gets rid of the compiler errors. I think there is a compiler bug here...

[code]
using System;
using System.Data;
using System.Data.SqlClient;

namespace CompilerError
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            DataTable dtProv;

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
            }
            public DataTable GetEditManager(string tab, string state, int rows, int pageselected,
                  out int nbrpages, string sort, byte sortdir, out int totaledits, string userid)
            {
                  // Evidently, the compiler is insisting that we actually initialize these things within our scope.
                  // Even though we are using them as out parameters.
                  nbrpages = 0;
                  totaledits = 0;
                  if (dtProv == null)
                        dtProv = GetData(tab, state, rows, pageselected, out nbrpages, sort, sortdir, out totaledits, userid);
                  // Evidently, the compiler is insisting that we actually initialize these things prior to using them
                  nbrpages = 0;
                  totaledits = 0;
                  return dtProv;

            }

            DataTable GetData(string tab, string state, int rows, int pageselected, out int nbrpages,
                  string sort, byte sortdir, out int totaledits, string userid)
            {
                  nbrpages = 0;
                  totaledits = 0;
                  return new DataTable();
            }
      
      }
}

[/code]

David
0
 
LVL 3

Accepted Solution

by:
solublefish earned 150 total points
ID: 16868544
I think you're misunderstanding how "out" parameters work. They're essentially just like return values. They have no values on the way in, and have to be assigned before you return. The compiler is _supposed_ to complain if you use them before they're assigned or return before assigning them. If you want something that behaves like a C++ reference (int& totaledits), use a "ref" parameter.

I think the code you want is simply:

public DataTable GetEditManager(string tab, string state, int rows, int pageselected, out int nbrpages, string sort, byte sortdir, out int totaledits, string userid)
        {
            if (dtProv == null)
                dtProv = taProv.GetData(tab, state, rows, pageselected, out nbrpages, sort, sortdir, out totaledits, userid);
            return dtProv;
        }

DataTable GetData(string tab, string state, int rows, int pageselected, out int nbrpages,
               string sort, byte sortdir, out int totaledits, string userid)
          {
               nbrpages = 0;
               totaledits = 0;
               return new DataTable();
          }

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:sutorius
ID: 16870044
In both examples above, something I don't understand, why do you create a function called GetData? taProv.GetData Method call in the GetEditManager function is supposed to call the asp.net table adapter which has the stored procedure in that GetData method.
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 16873634
I created it just to get to a compilable example. It was a lot easier than creating a data adapter! :-)
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 16873688
solublefish: >> The compiler is _supposed_ to complain if you use them before they're assigned or return before assigning them.<<

Agreed, but in the case of my (and your) example, the "out" keyword in the invocation of GetData is a guarantee that the variable will be initialized before use, isn't it? But, your example won't compile! I found that really surprising.
0
 
LVL 2

Author Comment

by:sutorius
ID: 16877116
Thanks for the help.  Problem resolved.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
defining NULL or 0 10 50
parse string in c# 5 29
How to set focus on a dynamic control 18 26
asp.net repeater 2 13
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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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