Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

C# -> C++ .Net translation

Probably a simple question:

How would you translate the following VC# code into VC++?

double[] dataX = new double[pointCount];
double[] dataY = new double[pointCount];

for (int i = 0; i < pointCount; ++i)
dataX[i] = dataY[i] = i;

xyPlot.PlotXY(dataX, dataY);

I thought of:

double* dataX = new double[pointCount];
double* dataY = new double[pointCount];

for (int i = 0; i < pointCount; ++i)
dataX[i] = dataY[i] = i;

xyPlot.PlotXY(dataX, dataY);

but PlotXY() does not like it. It seems that the type has to be __gc[] but I am not too familiar with it.

Thanks.
0 Kudos
Message 1 of 6
(4,680 Views)
Give this a shot:

const int pointCount = 100;
Double dataX[] = __gc new Double[pointCount];
Double dataY[] = __gc new Double[pointCount];

for (int i = 0; i < pointCount; ++i)
dataX[i] = dataY[i] = i;

xyPlot->PlotXY(dataX, dataY);

For more information on managed arrays in Managed C++, see the __gc
Arrays
section in MSDN.

- Elton
0 Kudos
Message 2 of 6
(4,680 Views)
If the PlotXY method is expecting .NET arrays (that is what __gc[] means), then I'll assume you want to use the .NET controls in C++. In that case, the code you are looking for is:

double dataX __gc[] = new double __gc[pointCount];
double dataY __gc[] = new double __gc[pointCount];

for (int i = 0; i < pointCount; ++i)
dataX[i] = dataY[i] = i;

xyPlot->PlotXY(dataX, dataY);

You have to explicitly tell the C++ compiler that you want to create a .NET array instead of a normal C array using the __gc keyword. You also need to use the pointer member access operator "->" instead of just a period to access the PlotXY method on xyPlot, but you must have done that at some point to get the error you describe.

That being said, the managed extensions to C+
+ are clunky, they have some very confusing syntax at times, and they don't flatten out the learning curve at all if you are using .NET controls and libraries. I would recommend using managed extensions in C++ as little as possible. If you need to have .NET code and native C++ code in the same application, I would recommend doing the bulk of your application in C# and writing a managed C++ dll that you call from your C# application.

Good luck with your application.

Tony H
Measurement Studio
0 Kudos
Message 3 of 6
(4,680 Views)
Thanks for the info and sorry for the "." typo.

A couple of questions regarding your last remark:

VB, VC++ and VC# .7 all are .Net languages. As you said, accessing the .Net libraries is easier in VB or VC# and, as a matter of fact, most .Net examples are written in these languages (I wonder why VC++ got 'left behind').

However I read that VC# is not as flexible and powerful as VC++.

What are the pro and cons of sticking to VC++ or migrating to VC# for .Net programming? Are MFC / VC++ supposed to be replaced by .Net / VC# ultimately?

Thanks.
0 Kudos
Message 4 of 6
(4,680 Views)
Basically Microsoft didn't put the resources in to making MC++ a first-class .NET language from the start. Note that there wasn't a Windows Forms designer for MC++ in Visual Studio .NET 2002. They are still actively developing MFC and C++, though. They added a Windows Forms designer in 2003 and I would look for continued improvements in the future. I don't forsee either .NET or C# replacing MFC or C++ any time soon.

Until they bring their C++ managed support up to par with C#/VB.NET, though, I would only use MC++ for .NET programming if (1) you have a large C++ code base already and just want to use some .NET Framework or Measurement Studio classes in a few places without rewriting the code (2) you have a definite need for some C++ language feature that is not available in C#, such as templates or multiple inheritance (3) you have some interop needs that aren't met by C# such as using a DLL that contains C++ objects instead of C entry points.

Although MC++ is more flexible and powerful than C#, the price you pay for that flexibility and power is substantially increased complexity. The interop in C# is sufficient for the vast majority of native code libraries and objects you might be using and you aren't faced with the headaches of keeping track of objects on both a managed (garbage collected) heap and an unmanaged heap. In MC++ you also need to keep in mind the type of objects you are using in each line of code because it impacts what you can do. For instance, you still can't have multiple inheritance with a .NET class, but you can with a native class, destruction occurs differently for managed and native types, native types can't always be directly used in the .NET context as you saw with your arrays, etc.

If you intend applications for network deployment, you should also know that it is currently impossible to write secure, verifiable code in MC++.

That is just my opinion. I hope it helps. Good luck with your project.

Tony H
Measurement Studio
0 Kudos
Message 5 of 6
(4,680 Views)
Thanks for the information.
mc.
0 Kudos
Message 6 of 6
(4,680 Views)