Solved

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

Posted on 2014-09-09
2
261 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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