Author Topic: C++ vector to C#  (Read 5673 times)

0 Members and 1 Guest are viewing this topic.

Jerry J

  • Newt
  • Posts: 48
C++ vector to C#
« on: November 26, 2009, 07:53:37 PM »
I'm trying to convert some FEA code I found on the web to C#.  I'm too inept at C++ to even turn the existing code to a dll library.

The C++ uses vector for piecing together the matrix. The equiv. in C# seems to be List<double> according to various searches. However, I'm getting confused on this bit:

vector< vector<double> > cBeam::GetGeometry()
{
   
    vector< vector<double> > v;
   
    v.push_back(L);
    v.push_back(vector<double>(L.size(),E));
    v.push_back(I);
    v.push_back(R);
   
    return v;
}

I was going to try this:

List<List<double>> GetGeometry()
      {
         List<List<double>> v = new List<List<double>>();
         v.Add(L);
         List<double> EL = new List<double>();
         EL.Add(L.Count());
         EL.Add(E);
         v.Add(EL);
         v.Add(I);
         v.Add(R);

         return v;
      }

but since this is a recurring piece, thought I'd see what some experts think.

Thanks for any help or suggestions.

sinc

  • Guest
Re: C++ vector to C#
« Reply #1 on: November 27, 2009, 10:38:02 AM »
I don't really think we can give much input with only that little segment of code.

It looks like you may be doing something that will work.  But since all we can see is this little tiny piece, there's not much to say about what it does.  For example, why are you returning a List of Lists?  Why not just return a List?  What you're doing might make sense, but there's no way to tell from just this snippet.

There are also other questions that come to mind.  For example, you are adding L to your List.  That implies that L is a double.  But in the next line, you are creating a new list, and including L.Count() in that list.  Count() is not a valid method for a double.

This piece of code might make more sense to someone who has any idea what L, E, I, and R represent.

Jerry J

  • Newt
  • Posts: 48
Re: C++ vector to C#
« Reply #2 on: November 27, 2009, 03:34:40 PM »
I suppose I could attach the C++ code that I downloaded when I figure out how.  It's too long to post here.
Basically L is a length attached to a member index, (list); as are E (modulus of elasticity); R (joint parameters, fixed, pinned..); I (moment of inertia).
All of these are lists of doubles attached to an index of the member to which they apply.  This structure is repeated for loads.  I am really trying to replicate the C++ code's structures and their behavior.  The C# List<> seems pretty close to C++ vector until you get to List<List<>>. I noticed later that C# has a stack<> collection object.  It may be closer to the C++ vector than a List<>, but C++ uses push_back, which I believe puts the item at the end of the stack whereas stack.push puts it at the beginning.  Maybe I'm wrong.  Being totally inept at C++, the line
v.push_back(vector<double>L.size(),E));
really baffles me.  I suppose that C++ must convert the L.size to a double implicitly, maybe I should add -- L.Count() as double ?

The main thing is to set up the list so that the index of the items reads the same as the C++ code: Latter in the program these type of structures are read into matrix (array structures) in various forms by using for loops; columnwise, rowwise.  I really didn't want to have to come up with my own matrix creation methods.

How do you attach a file to posts?  Here is the link I downloaded from ->

http://download.softpedia.com/dl/8fcac5e1a296a556f6c227f81361b50d/4b03c4ee/100143822/software/science/cba-0.3.3-win32.zip

the file is cbeam_class.cpp with of course cbeam_class.h

Thanks for any help.