Solved

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

Posted on 2006-06-08
7
537 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
  • 4
  • 2
7 Comments
 
LVL 11

Expert Comment

by:anyoneis
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Author Comment

by:sutorius
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help.  Problem resolved.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now