473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem converting managed array (C++) to C# array

The following code is from a solution with a C++ project and a C# project.
The C++ project creates a managed array and the C# project tries to use it.
But I am having a 'System.ValueType[]' to 'System.Drawing.Point[]'
conversion problem in the C# project. How is this supposed to be done?

-------------------------------------------------------------------
C++ project files
-------------------------------------------------------------------

// File: ArrayFiller.h
//
// Language: C++ with managed extensions
//
// The class, ArrayOfPoints, manages an array of Point objects. Since
// System::Drawing::Point is a managed class, it cannot be stored in a
// native array so the managed array must be used.

#pragma once

using namespace System;
using namespace System::Drawing; // Point

namespace ArrayFiller
{
public ref class ArrayOfPoints
{
// This class maintains an array of Points. Since Point is
// a managed type it cannot be contained in a native array, so a
// managed array must be used.
array <Point^>^ arrayOfPoints;
int nSize;

public:
ArrayOfPoints ()
{
// Create a managed array to store Point objects
nSize = 20;
arrayOfPoints = gcnew array <Point^> (nSize);

// Populate the array with some Point objects.
for (int i = 0; i < nSize; ++i)
{
arrayOfPoints[i] = gcnew Point (10 * i, 10 * i + 1);
}
}
property array <Point^>^ TheArray
{
array <Point^>^ get ()
{
return arrayOfPoints;
}
}
property int Size
{
int get ()
{
return nSize;
}
}
};

}

-------------------------------------------------------------------

// File: ArrayFiller.cpp
//
// This is the main DLL file.

#include "stdafx.h"

#include "ArrayFiller.h"

-------------------------------------------------------------------
C# project file
-------------------------------------------------------------------
// File: ArrayTest.cs
//
// Language: CSharp
//
// This file contains a class that attempts to use an array of Point
// objects that is maintained in a managed C++ project. The line
//
// points = filler.TheArray;
//
// fails with the error message:
// error CS0029: Cannot implicitly convert type 'System.ValueType[]' to
// 'System.Drawing.Point[]'

using System;
using System.Drawing;
using System.Collections.Generic;
using ArrayFiller;

namespace ConsoleApplicationArrayTest
{
class ArrayTestProgram
{
static void Main (string[] args)
{
ArrayOfPoints filler = new ArrayOfPoints ();
int nSize = filler.Size;
Point[] points = new Point[nSize];
//points = filler.TheArray;

for (int i = 0; i < nSize; ++i)
Console.WriteLine (points[i].ToString ());
}
}
}
Nov 17 '05 #1
2 1139
Very simple, you should not declare as:
array <Point^>^ arrayOfPoints;
but, instead, use: array <Point>^ arrayOfPoints;


Value type are not ^ed
Nov 17 '05 #2
Very simple indeed. Thanks. I have to get a better handle on the ^
operator.

"Lloyd Dupont" <net.galador@ld> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
Very simple, you should not declare as:
array <Point^>^ arrayOfPoints;


but, instead, use:
array <Point>^ arrayOfPoints;


Value type are not ^ed

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) {...
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
5
by: _BNC | last post by:
I've converted " byte" to "byte *" at times, using 'unsafe' and fixed { .... }, but the reverse does not seem to work. In this case, a C++ DLL returns a byte * and a length. What is the best...
2
by: Dick Swager | last post by:
The following code is from a solution with a C++ project and a C# project. The C++ project creates a managed array and the C# project tries to use it. But I am having a 'System.ValueType' to...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
3
by: Wild Wind | last post by:
Hello, I made a post relating to this issue a while back, but I haven't received any answer, so here I am again. I am writing a mixed C++ dll which uses the following declaration: typedef...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: brian_harris | last post by:
I am tring to use trimstart to remove leading zeros, but all things I try give a compiler error on converting data. I am programing in C++ .net vs2003. This is one of my earlier attempts to call...
5
by: Bob Altman | last post by:
Hi all, I'm writing C++/CLI code (compiled with /clr) that accepts a native char array (and its length) and calls a managed routine that expects an array of System::Byte. I was just wondering...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.