Wednesday, September 5, 2018

Accessing C# Method from Excel using VBA

TASK DESCRIPTION:

Excel button click event accessing User Form from Excel which has textboxes and Login button to provide arguments to c# method and retrieve output in excel.

Steps:

1.      Create Class library in C# (create method)

Fig 1.1 create class library

Fig 1.2: create class library























































2.      C# Code:


Namespace
using System.Runtime.InteropServices;

[
ComVisible(true)]
public class Class1
{
public int Add (int a, int b)
{
  return a + b;
}
}



3.      Create reference of Project library

Fig 1.3:  COM-visible property exposes public methods and functions in machine assembly to other applications not written in .NET.


4.    Enable Register for COM Interop

Fig 1.4: Enable Register for COM interop






 

5.    Excel: Enable Developer bar on top panel. File --> Options (POP Up window will appear)




Fig 1.5: Excel Option 
 

6.    View Developer toolbar

Fig 1.5: Button Draw


7.    View code toolbar in Developer Panel --> sheet window will popup --> tools --> References --> scroll down for your class library exe file and press Ok




8.    Create user form with two textboxes and a Login button and paste below code in Login button click event





VBA Code:

Dim sheet As Worksheet
Dim tools As TestProject_Excel.Class1
Dim value As String
Dim result As String
Dim uiValue As String
Dim uiValue1 As String

Set sheet = ActiveSheet
value = sheet.Cells(1, 1)
Set tools = New TestProject_Excel.Class1

uiValue = UserForm1.TextBox1.Text
uiValue1 = UserForm1.TextBox2.Text
Unload UserForm1

If uiValue = vbNullString Then
 UserForm1.Error = "UserName can not be empty"

ElseIf uiValue1 = vbNullString Then
 UserForm1.Error = "Password can not be empty"

Else
result = tools.Add(uiValue, uiValue1)
sheet.Cells(1, 2) = result
End If

OUTPUT:





Tuesday, April 17, 2018

C#.Net frameworks

Here are C# .NET Framework versions with their main features:
C#.NET framework 1.0 and 2.0 are basic framework which comes in  Visual Studio 2002 and Visual Studio 2003.






Monday, January 5, 2015

Programming Fundamentals



Programming is a core activity of computer science. It is a skill for creation. Once you can do it, make computer to touch the sky with your own concepts and thinking power. A program is just a plan or instruction for a machine to follow.

Example: Program are like recipes

The chef writes the instructions clearly enough that anyone can follow them. They tell you the actions that you would be performed during baking cake, there is no point to baking cake then stirring vigorously.
So like baking cake we have to give clear instruction to machine to follow otherwise our cake may be baked but burnt or soggy. 

Variables:
  • Data storage is the main purpose of using variables. 
  • This information data stores temporary in RAM.
  • This data have value, name and memory address.
  • Signed variables was available earlier.  
What are signed variables discuss later in this article.
Ex: This example shows proper behavior of variables functionality storage.





Declaring or initializing a variable:

DataType  VariableName  =  Value;
string StudentName= “Student” ;
char decision= “Y”;
int x = 12;
double x= 23;
decimal  x= 23.4454;
float x=34322.343;
bool gender= true;  

Note: Data type that define which type value is stored in a variable. There are many more data types in C# which are:



 

Concept of Signed and Unsigned Variables:

Signed variables are those have a sign
Ex: int a = -1;


Unsigned variables are those don’t have any sign
Ex: int a = 1;



Previous concept of sign and unsigned variable was both sign(-) and unsign(+) mark have separate 64 bit slot allocation in memory and 64 bit slot allocation in memory for their value(1).

New Concept is if they have sign(-) mark then it uses same behavior as previous but if they don’t have sign then their would be only 64bit memory allocation for value(1).