Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C# and excel class issue

ksparks212ksparks212 Posts: 1Member
I need to know how to call an external Excel function from a class. The excel formula works fine inside the main function but not in the separate class. The code is below

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel;

namespace TestNormSInv
{
public class optionMath
{
public double testDouble(double price)
{
double result = 0.0;
//The line below produces the error !!!!!!!!!!!!!!!!!!!!!
result = WorksheetFunction.NormSDist(price);
return result;
}

public double testTriple(double price)
{
double result = 0.0;
//The line below produces the error !!!!!!!!!!!!!!!!!!!!!
result = WorksheetFunction.NormSDist(price);
return result;
}
}
class CalcExcel
{
static void Main(string[] args)
{
double result = 0.0;
Excel.Application oXL;
try {
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = false;
oXL.UserControl = false;

// This code works
result = oXL.WorksheetFunction.NormSDist(1.04);
Console.WriteLine("Normsdist 1.04: {0}", result);
optionMath OM = new optionMath();

//This does not work
result = OM.testDouble(1.04);
}
catch (SqlException e)
{
// Display error
Console.WriteLine("Error: " + e);
}
finally {
Console.WriteLine("Connection closed.");
}
}
}
}

Comments

Sign In or Register to comment.