Solved

How to convert vba macro function into c#.net code

Posted on 2014-09-09
2
257 Views
Last Modified: 2014-09-11
Hi,

Could you please help me out regarding how to convert the code from vba macro to c#.net 3.5

Private Type Struct_XYZ
  CellID As Long
  P_Pos(1 To 3) As Double
  W_IDs(1 To Max_Num_XYZ) As Long
  W_Vectors(1 To 3, 1 To Max_Num_XYZ) As Double
  W_Distance(1 To Max_Num_XYZ)
  Shortest As Double
  W_Vector_Shortest(1 To 3) As Double
  W_ID_Shortest As Long
  Number_of_XYZ As Integer
End Type

Thanks you in advance.
0
Comment
Question by:mannevenu26
2 Comments
 
LVL 100

Assisted Solution

by:mlmcc
mlmcc earned 200 total points
ID: 40315549
Something like this

Private class Struct_XYZ
{
   Long CellID;
   Double[ ]  P_Pos;
   Long[ ] W_IDs; 
   Double[ ] W_Vectors;
   Double [ ] W_Distance; 
   Double Shortest; 
   Double[ ] W_Vector_Shortest;
   Long W_ID_Shortest;
   int Number_of_XYZ ;
}

Open in new window


mlmcc
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 300 total points
ID: 40315604
You would also need to initialize the arrays.  So if Max_XYZ is a variable (as apposed to a constant), then it would be:
struct Struct_XYZ
{
    public Struct_XYZ(int Max_Num_XYZ)
    {
        CellID = 0;
        P_Pos = new double[3]; //indexed 0-2
        W_IDs = new long[Max_Num_XYZ]; //indexed 0 to Max_Num_XYZ-1
        W_Vectors = new double[3, Max_Num_XYZ];  //indexed 0-2 & 0 to Max_Num_XYZ-1
        W_Distance = new double[Max_Num_XYZ]; //indexed 0 to Max_Num_XYZ-1
        Shortest = 0;
        W_Vector_Shortest = new double[3];  //index 0-2
        W_ID_Shortest = 0;
        Number_of_XYZ = 0;
    }
    long CellID;
    double[] P_Pos;
    long[] W_IDs;
    double[,] W_Vectors;
    double[] W_Distance;
    double Shortest;
    double[] W_Vector_Shortest;
    long W_ID_Shortest;
    int Number_of_XYZ;
}

Open in new window


All of the arrays are 0 based (as apposed to 1 based in VBA),   So to access the 3rd item in the W_Distance array, you would do this:
   StructInst.W_Distance[2]=5;

In C# you have both classes and structs.  A class is allocated on the heap (the variable is a pointer to the allocated memory), while a struct is allocated on the stack (the variable is the direct memory pointer to the stack).  The main difference, is a struct is passed by value when it is used as a function parameter (a copy is sent to the function).  For a class, the variable is passed by reference.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now