0

I am C++ programmer, and I am stuck in a migration project where I need to convert below C++ code to C#. Need help on the same.

unsigned short** varData = new unsigned short*[ndata]; //Say ndata is 10

for(int i=0; i<ndata; i++) varData[i] = new ushort[nwp]; 

Thanks in advance.

3 Answers 3

4

This gives you a 2D array of 16bit unsigned integers, which is probably what you try to express in C++ by using pointer to pointer.

  int nData = 10;
  int nwp = 3;

  var varData = new UInt16[ nData, nwp ]; //varData is of type UInt16[,]

http://msdn.microsoft.com/en-us/library/2yd9wwz4(v=vs.71).aspx

Sign up to request clarification or add additional context in comments.

Comments

3

If I understand correctly, you use unsigned short** to store 2-dimensional array of ushort. In C# you can just declare it as

ushort[,] array = new ushort[m, n];

where m and n are the dimensions.

Comments

1

Use Jagged Arrays, not 2D array; if you need the same result as C++. See http://msdn.microsoft.com/en-us/library/2s05feca.aspx for more informations.

4 Comments

Why should he use Jagged Arrays instead? Link only answers are discouraged, please include at least a brief summery of the information (in the event that the link ever goes dead)
@ScottChamberlain Because C++ source is using Jagged Arrays, not 2D array.
It would be nice to explain that in your answer instead of "just taking your word for it" (I am not disagreeing with you, in fact I think you bring up a very good point. I just want to help you learn to make better answers at this beginning of your SO experience)
@ScottChamberlain Updated. Thank you for advanced. The truth is I want to explain more than I answered. But, I'm still learning English.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.