Solved

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

Posted on 2006-06-08
7
546 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
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.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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