I am working on a .NET project. In a DAO class (using ADO.NET but this is not important) I have a method like this:
public static List<Inoltro> GetListaInoltriUltimiGiorni(int IdUor, string Protocollista, int NumeroGiorni, DBConnection config)
{
string query = PROT_INOLTRO.INOLTRI_ULTIMI_N_GIORNI_BY_UOR_AND_PROTOCOLLISTA;
List<Inoltro> result = new List<Inoltro>();
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
try
{
cmd.Parameters.AddWithValue("@IdUor", IdUor);
cmd.Parameters.AddWithValue("@Protocollista", Protocollista);
cmd.Parameters.AddWithValue("@NumeroGiorni", NumeroGiorni);
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
// Read advances to the next row.
while (reader.Read())
{
.........................................
.........................................
.........................................
}
}
}
}
catch (Exception ex)
{
con.Close();
throw ex;
}
}
}
return result;
}
As you can see I am creating and returning this object:
List<Inoltro> result = new List<Inoltro>();
The problem is that Visual studio give me the following error on this method signature:
Inconsistent accessibility: return type
List<Inoltro>
is less accessible than method MyClass.GetListaInoltriUltimiGiorni(....)
Why? What am I missing? How can I fix it?
Your method is public
but Inoltro
is internal
. You cannot have a public
method that exposes an internal
type. Either make the method internal
or the type public
.
While we are looking at your code, a few things come to mind.
First, the try catch
is unnecessary, since the using
already does a Close
on the connection if an exception is thrown. Eliminate the try catch
.
Second, for your future reference, never say throw ex;
in a catch
. Say throw;
to re-throw an exception. The difference is subtle. throw;
re-throws the exact exception that was caught. throw ex;
re-throws the exception but resets the stack trace to the current trace, not the original trace.
The only time you want to do throw ex;
is when you deliberately want to obscure where an exception came from, for instance, because there is a trust boundary between the current code and the caller. However, in that case it would be better still to throw a new, generic exception.
Third, I don't understand why you have both a while
and an if
to check to see if the set has rows. Surely if the set is empty then the Read
will return false, so there was never a need for the if
, right?
Fourth, this is just a style point; normally we would format
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
as
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
using (SqlCommand cmd = new SqlCommand(query, con))
{
It is common to have a using
whose body is only another using
, and in that scenario the unnecessary braces and indentation do not make the code easier to understand.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.