Need this visual basic code converted to C#

tatton777
tatton777 used Ask the Experts™
on
Hello,
My credit card processor provided a VB DLL for me to use in sending data to and receiving data from them. My website is in C# so I need to figure out how to incorporate this DLL into it.

In the example from the credit card processor I am to add a reference to SendPmt.dll within the VB project and then use this code

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Initialize string
        Dim strData As String
        Dim strReturn As String

        Dim objSendPmt As SendPmt.clsSendPmt
        objSendPmt = CreateObject("SendPmt.clsSendPmt")

        strData = Replace(txtInput.Text, vbCrLf, Chr(10))

        strReturn = objSendPmt.SendPayment(strData, "test")

    End Sub

Open in new window


So far in my C# attempt I have added a reference to the SendPmt.dll from the processor and a reference to Microsoft.VisualBasic.dll. I think I am on the right track using the Microsoft.VisualBasic.dll but I can't seem to get it to work.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
converter.telerik.com

protected void  Button1_Click(object sender, System.EventArgs e)
{
	// Initialize string
	string strData = null;
	string strReturn = null;

	SendPmt.clsSendPmt objSendPmt = default(SendPmt.clsSendPmt);
	objSendPmt = Interaction.CreateObject("SendPmt.clsSendPmt");

	strData = Strings.Replace(txtInput.Text, Constants.vbCrLf, Strings.Chr(10));

	strReturn = objSendPmt.SendPayment(strData, "test");

}

Open in new window

Author

Commented:
I get a No overload for method 'CreateObject' takes '1' argument

Even though the VB example works with one parameters, CreateObject is defined as taking two.

CreateObject(string ProgId, string ServerName)
pass it string.Empty as default value
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Top Expert 2011
Commented:
Here is another option (no VB.NET namespace dependencies):
(you can also have objSendPmt as a dynamic variable, though that probably slower)

// using System.Runtime.InteropServices.Automation;

protected void  Button1_Click(object sender, System.EventArgs e)
{
	// Initialize string
	string strData = null;
	string strReturn = null;

	var objSendPmt = (SendPmt.clsSendPmt)Activator.CreateInstance(Type.GetTypeFromProgID("SendPmt.clsSendPmt"));
     // dynamic objSendPmt = Activator.CreateInstance(Type.GetTypeFromProgID("SendPmt.clsSendPmt"));

	strData = Strings.Replace(txtInput.Text, "\r\n", "\n");

	strReturn = objSendPmt.SendPayment(strData, "test");
}

Open in new window

Author

Commented:
I got both of these examples to work. However, they both return the same error.

"ERR: Unable to send data to remote host 26021"

I am assuming this is coming from within the SendPmt.dll.

I just verified that the VB code worked and it does. Why would C# throw this curve ball?

Author

Commented:
Nevermind. I got this to work with the Activator code. THANKS!
Anuradha GoliSystems Development / Support Specialist

Commented:
protected void  // ERROR: Handles clauses are not supported in C#
Button1_Click(object sender, System.EventArgs e)
{
	// Initialize string
	string strData = null;
	string strReturn = null;

	SendPmt.clsSendPmt objSendPmt = default(SendPmt.clsSendPmt);
	objSendPmt = Interaction.CreateObject("SendPmt.clsSendPmt");

	strData = Strings.Replace(txtInput.Text, Constants.vbCrLf, Strings.Chr(10));

	strReturn = objSendPmt.SendPayment(strData, "test");

}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik, @toddanglin
//Facebook: facebook.com/telerik
//=======================================================

Open in new window

Author

Commented:
Thanks so much for this excellent code. The other code examples didn't fail to compile, however, yours is the only code that correctly instantiated the VB dll.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial