Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2014-09-09
Medium Priority
Last Modified: 2014-09-11

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.
Question by:mannevenu26
LVL 101

Assisted Solution

mlmcc earned 800 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

LVL 21

Accepted Solution

MogalManic earned 1200 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:

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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

564 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