Solved

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

Posted on 2006-06-08
7
543 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rest webservice call over https via c# 6 49
Hey Audio! 9 44
transaction in asp.net, sql server 6 43
How to read text with RegEx... 2 25
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 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