473,657 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ValueTy pe[]' 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 "ArrayFille r.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.ValueTy pe[]' to
// 'System.Drawing .Point[]'

using System;
using System.Drawing;
using System.Collecti ons.Generic;
using ArrayFiller;

namespace ConsoleApplicat ionArrayTest
{
class ArrayTestProgra m
{
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.WriteLi ne (points[i].ToString ());
}
}
}
Nov 17 '05 #1
2 6881
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******** ******@TK2MSFTN GP09.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
2843
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) { Byte bytearrayinput = StringAndByteManipulation.ConvertStringToByteArray(textToEncrypt); //DES instance System.Security.Cryptography.TripleDESCryptoServiceProvider des = new
2
5749
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 Explicit On Option Compare Binary
5
4427
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 way to convert these to straight C#-compatible straight " byte" arrays? PS: The C++ DLL is actually managed and I have access to the source. Perhaps there is a simpler syntax for doing the conversion there, and returning a C# compatible array...
9
2586
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 explain it. I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the "Managed Extensions for C++ Reference"....
3
1771
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 System::Byte ByteArray __gc;
0
3931
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
2
1148
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 'System.Drawing.Point' conversion problem in the C# project. How is this supposed to be done? ------------------------------------------------------------------- C++ project files ------------------------------------------------------------------- ...
2
2145
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 function empcode = empcode->TrimStart ('0'); This is result: c:\T02010_NET_ora9\cgi-bin\programs\TimeReader\TimeReaderWinService.cpp(322): error C2664: 'System::String::TrimStart' : cannot convert parameter 1 from 'char' to '__wchar_t __gc'...
5
6099
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 if there is any magic that I'm missing that easily marshals the native array into a managed array, or do I need to write my own code to allocate the managed array<and loop through it copying the native data into the managed array? TIA - Bob
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.