3

As the title suggest, this is my first C# try, so please go easy. (As a newb I promise to ask a bunch of easy questions for the C# pros out there to get you some easy points!) I'm using ExcelDNA to create a UDF in Excel, which will query our mysql database. I've added the ExcelDNA and mysql connector dll's as references. I have the following code, which produces a few errors:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;  
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using ExcelDna.Integration;
using MySql.Data.MySqlClient;

namespace my_test
{
public partial class ThisAddIn
{
    [ExcelFunction(Description = "Multiplies two numbers", Category = "Useful functions")]
    public static MultiplyThem(string[] args)
    {

        string connString = "Server=localhost;Port=3306;Database=test;Uid=root;password=p-word";
        MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "SELECT field_value FROM customers";
        try
        {
            conn.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        string myvariable = "bad";

        MySqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            myvariable = reader["field_value"].ToString;
        }

        return myvariable.ToString;
    }




    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

}
}

Here's the errors:

Error 1 Cannot convert method group 'ToString' to non-delegate type 'double'. Did you intend to invoke the method?
Error 2 Method must have a return type
Error 3 Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?
Error 4 Since 'my_test.ThisAddIn.MultiplyThem(string[])' returns void, a return keyword must not be followed by an object expression

usr951
  • 73
  • 1
  • 6

6 Answers6

5

Error 1: ToString is a method. you need reader["field_value"].ToString();

Error 2: All methods must specify what is the type of object they return. In this case, you want MultiplyThem to return a string. public static string MultiplyThem(string[] args)

Error 3: See Error 1

Error 4: See Error 2

Jimmy
  • 89,068
  • 17
  • 119
  • 137
3

If you're going to use Excel-DNA, you need to take out the references to the Visual Studio Tools for Office (VSTO) assemblies, and the corresponding bits in your code - you can't mix the two frameworks in one assembly. The VSTO parts are the ones called Microsoft.Office.Tools... So I suggest:

  1. Remove the using Microsoft.Office.Tools.Excel;
  2. Your add-in class need not be partial (there isn't likely to be other 'parts').
  3. Remove the ThisAddIn_Startup and ThisAddIn_Shutdown - also part of the VSTO framework.

Your Console.WriteLine is unlikely to go anywhere - rather use ExcelDna.Logging.LogDisplay.WriteLine.

Another hint: Set the reference to ExcelDna.Integration.dll to be Copy Local: true in the property sheet for the reference. That way you don't get an unnecessary copy of this assembly in your output directory.

If you are using Visual Studio 2010 your library will probably target .NET 4.0. Remember to set the runtime version in the .dna file:

<DnaLibrary RuntimeVersion="v4.0" >
    <ExternalLibrary Path="MyAddIn.dll" />
</DnaLibrary>
Govert
  • 16,387
  • 4
  • 60
  • 70
2

Here you go. You have two basic problems:

1) The method needs to have a return type specified or void. Since you are returning a string, I have added string between static and the method name.

2) ToString is a method. When you use it, you need to add the parens, such as .ToString().

public static string MultiplyThem(string[] args)
{

    string connString = "Server=localhost;Port=3306;Database=test;Uid=root;password=p-word";
    MySqlConnection conn = new MySqlConnection(connString);
    MySqlCommand command = conn.CreateCommand();
    command.CommandText = "SELECT field_value FROM customers";
    try
    {
        conn.Open();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

    string myvariable = "bad";

    MySqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        myvariable = reader["field_value"].ToString();
    }

    return myvariable;
}
competent_tech
  • 44,465
  • 11
  • 90
  • 113
2

All the errors are fairly self-explanatory.

1) Declare the method with return type string:

public static string MultiplyThem(string[] args)
//            ^^^^^^

2) Use ToString(): return myvariable.ToString();

3) Last error will have gone away.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

Couple things to note here:

  1. Your method should have a return signature (i.e. string or double) or should have a return signature of void. If you are wishing to return the string, your method should read:

    public static void MultiplyThem(string[] args)
    
  2. When you read a value from a reader, you need to access it as follows:

    myvariable = reader["field_value"].ToString();
    
  3. The ToString method is a method, as so needs to be called as such.
Daniel Mallott
  • 298
  • 5
  • 7
2

Change myvariable.ToString to myvariable.ToString().

Change your method declaration from public static MultiplyThem(string[] args) to public static string MultiplyThem(string[] args).

Jeremy Wiggins
  • 7,239
  • 6
  • 41
  • 56