Solved

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

Posted on 2006-06-08
7
538 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Long URL in SMS 6 32
How to use xmlReader and idatareader  ? 4 55
how to read json value 2 27
C#.NET and microsoft certification. 3 25
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

932 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

12 Experts available now in Live!

Get 1:1 Help Now