RBrattas
RBrattas

Reputation: 121

C# method to return var

I like to return var from my method. I manage to use dynamic, but then my try-catch-finally does not work. Could you improve my method to return var? I do not need to use dynamic, I am looking for the best method to return var.

Thank you so much in advance. Rune

public dynamic m_DEMO_Return_var_method(string vpSchemaName, string vpTableName)
{

    var var_List = new List<string>();

    //try
    //{

    DataTable iDataTable = new DataTable();

    var_List.Clear();
    foreach (DataRow iDataRow in iDataTable.Rows)
    {
        var_List.Add(iDataRow["COLUMN_NAME"].ToString());
    }

    var vColumnName = var_List.ToArray();

    return vColumnName;

    //}
    //catch (Exception im_Exception)
    //{
    //    return vColumnName;
    //}
    //finally
    //{
    //}
}

Upvotes: 5

Views: 51985

Answers (6)

captainsac
captainsac

Reputation: 2490

You can update your code as follows.

public dynamic m_DEMO_Return_var_method(string vpSchemaName, string vpTableName)
{

    var var_List = new List<string>();
    var vColumnName = var_List.ToArray();
    try
    {

        DataTable iDataTable = new DataTable();

        var_List.Clear();
        foreach (DataRow iDataRow in iDataTable.Rows)
        {
            var_List.Add(iDataRow["COLUMN_NAME"].ToString());
        }

        vColumnName = var_List.ToArray();

        return vColumnName;

    }
    catch (Exception im_Exception)
    {
        return null;
    }
    finally
    {
    }

    return vColumnName;
}

The method will return null if vColumnName variable will not contain any data or if any exception will occure.

Upvotes: 11

shooky
shooky

Reputation: 141

Although this doesn't directly answer your question of "returning var" it does solve your problem, similar answers exist above but applying a method I use often to your exact code:

     public string[] demo(string vpSchemaName, string vpTableName)
     {
        List<string> string_List = new List<string>();

        DataTable iDataTable = new DataTable();

        foreach (DataRow iDataRow in iDataTable.Rows)
        {
            string_List.Add(iDataRow["COLUMN_NAME"].ToString());
        }

        return string_List.ToArray();
    }

Upvotes: 1

Yair Nevet
Yair Nevet

Reputation: 13003

Could you improve my method to return var? I do not need to use dynamic, I am looking for the best method to return var.

var is not a type but a C# keyword for shortening and making the code more readable (an implicitly typed local variable) - read here about it more deeply. So forget about returning var - Use a real type in your method signature, just like: int, string etc.

As it's looked now, you're returning string array, so change the return type to string[], look:

public string[] m_DEMO_Return_var_method(string vpSchemaName, string vpTableName)

As for the try-catch-finally block, it would not work for you because vColumnName is declared in the try scope (code block) - You can always declare it higher, in the method scope for example.

BTW, you're just instanciating the DataTable but not filling it first, where should the data come from?:

DataTable iDataTable = new DataTable();

You're also clearing the var_List without filling it first:

var_List.Clear();

In addition, you're better use another name for your method name, as the accepted convention in c# is Pascal case for methods naming:

GetVarMethod

Upvotes: 8

terrybozzio
terrybozzio

Reputation: 4542

Var cannot be used as method return value neither as method parameter.Since you are returning a collection you can make your return type IEnumerable:

public IEnumerable<T> m_DEMO_Return_var_method<T>(string vpSchemaName, string vpTableName)
{
     .......

     .......

     return vColumnName as IEnumerable<T>;
}

Upvotes: 2

Douglas
Douglas

Reputation: 54877

The type of the object that you are returning is always string[]. You could change the return type of your method without affecting its functionality:

public string[] m_DEMO_Return_var_method(string vpSchemaName, string vpTableName)

This would still allow you to use var when calling the method:

var result = m_DEMO_Return_var_method("schema", "table");

Edit: Your catch block isn't compiling because you're attempting to return a variable, vColumnName, that is not declared within its scope. You can fix this by returning null or an empty string[].

catch
{
    return null;
    // return new string[0];   // (alternative)
}

Upvotes: 6

lightbricko
lightbricko

Reputation: 2709

Simply change the return type of the method to object and you can return anything declared using the var keyword whatever type it happens to be.

Upvotes: 0

Related Questions