Sunday, July 22, 2012

How to call .Net library from Excel

This method has been verified using Visual Studio 2010 and MS Office 2010. It works!!

(Reference: http://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/ )

(1) Create a public .Net class and have a public method therein. ClassInterface will allow ‘intelligence’ feature on VBA, so it is useful to have. For example:

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Runtime.InteropServices

    ClassInterface(ClassInterfaceType.AutoDual)_
    Public Class DotNetClass
      Public Sub DotNetMethod(ByRef input As String, ByRef output As String)
        output = "Hello" + input
      End Sub
    End Class

    Let’s assume the filename is Test.dll

(2) On the project Assembly Information, give a check to “Make assembly COM-Visible”

(3) Build library

(4) On Excel , create VBA code for testing. For example,           

   Sub Button1_Click()
   Dim testClass As New DotNetClass
   Dim inp As String
   Dim out As String
   inp = "World"
   Call testClass.DotNetMethod(inp, out)
   MsgBox (out)
   End Sub

(5) For deployment to others who doesn’t have a programming environment, the deliverables are Test.dll only.

(6) To register all public classes contained in Test.dll, and generates and registers the type library Test.tlb, which contains definitions of all the public types defined in Test.dll. Open Command Window, Use following command for registration:

  C:\windows\microsoft.net\Framework\v4.0.30319\regasm.exe /codebase Test.dll /tlb: Test.tlb

An important advice is that you must use 32bit version of regasm.exe in .Net 4.0 even if your computer is 64 bit. It is because .NET library is compiled for 32 bit (in VS2010).

Thursday, April 26, 2012

Things to know when you choose Aspen reactor model

I think this is useful, concise information to know about the reactor model in Aspen Plus (and Dynamics as well). Thanks to original commenter - David Tremblay. His original comment is found in LinkedIn comment ID 78253172 .

Aspen Plus includes three types of reactor models. The type you choose depends on the level of rigor you want to use and the amount of information you have available:

(1) Balance/conversion reactors - RSTOIC, RYIELD - These reactors are for mass and energy balance purposes. You specify the conversion or yield and the reaction stoichiometry. In essence, you tell Aspen the expected result and it handles the details of the mass, energy, and species balances.

(2) Equilibrium reactors - REQUIL, RGIBBS - These reactor models are appropriate for fast reactions that reach equilibrium quickly (although there are ways to specify approach to equilibrium for non-ideal cases). RGIBBS is the most flexible model. It allows multiple phases (including multiple solid phases) and multiple species. This model uses Gibbs free energy minimization to predict results. It requires accurate thermodynamics since Gibbs energy is calculated from enthalpy and entropy.

(3) Rate-based kinetic reactors - RBATCH, RCSTR, RPLUG These models are appropriate when you know the reaction kinetics. You describe kinetics using one of the built-in reaction models (power law, LHHW, etc.) or your own user-defined kinetic subroutine. RBATCH and RCSTR are able to represent reactors with solid-phase catalysts. RPLUG can represent tubular or multi-tube plug flow reactors. RCSTR represents any well mixed stirred tank (or fluid bed) reactors. RBATCH is for batch reactors. These reactor models are more predictive, but they require more information to describe reaction rates.

Thursday, January 12, 2012

Some compiler options to improve computing performance in VB.NET

I often see that some people claims that VB is slower than C#. I have talked with different people and searched information to confirm whether it is true. This could be untrue but my conclusion so far is that there's no fundamental difference in between two language types within .NET Framework. Then why the programmers complains that VB is slower. It seems that the default compiler options set by the IDE (Visual Studio) is more conservative for VB than C#. As I can easily forget such valauable information, I decide to write the information in the blog.

In VS2010,

1. Compiler Options under Compiler Page

Set "Option Strict" = On


2. Advanced Compiler Settings within the Compiler Options under Compiler Page

Give a check for "Remove integer overflow checks"
Give a check for "Enable optimizations"
Set On for "Generate serialization assembles" --> I was told this is good for WPF XAML performance


If anyone has an ojection or has an opinion, please leave your note, so that I can learn from it.