473,811 Members | 3,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to iterate a gcroot

Hi all,

In C++/CLI (VS 2005) I have a C++ module compiled with /clr that contains a
module-level gcroot variable:

static gcroot<MyList^m _myList = gcnew MyList;

In the above statement, MyList is a managed class that inherits from
Generic::List. I want to iterate through m_myList like this:

for each (MyItem^ myItem in m_myList) {
[do something with myItem]
}

but I get error c3285: for each statement cannot operate on variables of type
'gcroot<T>'

So, how do I iterate through a List that's stored in a gcroot?

TIA - Bob
Jun 27 '08 #1
6 6193
Hi Bob,

I will perform some research on this issue and provide a reply to you ASAP.
Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== ===========
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #2
Hi Bob,

Sorry for letting you wait.

C++/CLI "for each" statement iterates over objects that implement the
IEnumerable interface, while gcroot does not implement this interface.
Luckily, gcroot provides an operator T conversion, so you can just
static_cast in your example to the MyItem^ type. I have written a sample
using System::String^ type to demonstrate it.

// The following generates the same error as your program
#include <vcclr.h>
using namespace System;

class CppClass {
public:
gcroot<String^s tr; // can use str as if it were String^
CppClass() {}
};

int main(array<Syst em::String ^^args)
{
CppClass c;
c.str = gcnew String("hello") ;

for each ( Char c in c.str )
Console::Write( c);
return 0;
}
Error 1 error C3285: for each statement cannot operate on variables of type
'gcroot<T>'
// The following code compiles well.
#include <vcclr.h>
using namespace System;

class CppClass {
public:
gcroot<String^s tr; // can use str as if it were String^
CppClass() {}
};

int main(array<Syst em::String ^^args)
{
CppClass c;
c.str = gcnew String("hello") ;

for each ( Char c in static_cast<Str ing^>(c.str) )
Console::Write( c);
return 0;
}

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== ===========
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #3
Sorry, a little typo, you should static_cast in your example to the MyList^
type. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== ===========
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #4
""Jeffrey Tan[MSFT]"" <je***@online.m icrosoft.comwro te in message
news:Bl******** ********@TK2MSF TNGHUB02.phx.gb l...
Hi Bob,

Sorry for letting you wait.

C++/CLI "for each" statement iterates over objects that implement the
IEnumerable interface, while gcroot does not implement this interface.
Luckily, gcroot provides an operator T conversion, so you can just
static_cast in your example to the MyItem^ type.
Thanks (as always) Jeffrey!

This also answers another question that I had. The gcroot wrapper is great
as long as you are assiging values to it or accessing members contained by
it, but I have run into other scenarios where I need to get my hands on the
actual underlying managed reference.

I submitted this as a documentation bug to Microsoft Connect:
https://connect.microsoft.com/Visual...dbackID=353206

Jun 27 '08 #5
Hi Bob ,

Glad to see my reply can help you. If you need further help, please feel
free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== ===========
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 30 '08 #6
// The following code compiles well.
#include <vcclr.h>
using namespace System;

class CppClass {
public:
gcroot<String^s tr; // can use str as if it were String^
CppClass() {}
};

int main(array<Syst em::String ^^args)
{
CppClass c;
c.str = gcnew String("hello") ;

for each ( Char c in static_cast<Str ing^>(c.str) )
Console::Write( c);
I would suggest introducing a new variable instead of adding a cast, the
cast can hide type incompatibiliti es.

i.e.

String^ const s = c.str;
for each ( Char c in s ) { ... }
return 0;
}

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== ===========
Delighting our customers is our #1 priority. We welcome your comments
and suggestions about how we can improve the support we provide to
you. Please feel free to let my manager know what you think of the
level of service provided. You can send feedback directly to my
manager at: ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.

Jul 29 '08 #7

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

Similar topics

0
1623
by: igal.ioffe | last post by:
Hi All, I've run into a problem, converting a native project into mixed mode, where usage of gcroot point in mixed mode causes a fatal CLR engine exception. Here is a tiny code extract: ------------------ mixeddll.cpp ---------- #include <vcclr.h>
2
2740
by: Steve McLellan | last post by:
Hi, I see that GCHandle can encapsulate a weak reference, but gcroot appears to only allow 'normal' GCHandles. Is this correct or is there something I've missed? Ta, Steve
5
2641
by: Gerhard Menzl | last post by:
Has anyone ever tried to sort a Standard Library container of gcroots? I have run into the problem that somewhere deep in the Library logic (in line 338 of <memory>, to be precise) the destructor of a class _Temp_iterator tries to obtain the address of a gcroot, but fails because gcroot::operator& is private. -- Gerhard Menzl #dogma int main ()
2
2010
by: Dave | last post by:
I have a few C# .NET components which might implement one or several interfaces, let say I1, I2 and I3. The unmanaged C++ client consumes this component using gcroot template pointer. How client application can find out what interfaces implement certain instance of the pointer? I need some kind of QueryInterface method which allows me to query for certain interface. How can I do that if I use gcroot template? Thanks.
0
1487
by: Rob Haynes | last post by:
I have a DLL with vanilla C functions implemented. I'd like these functions to become wrappers for a managed object. So, I was thinking that the first call would save a pointer statically to the managed object that future calls could use. I found this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vcconconvertingmanagedextensionsforcprojectsfrompureintermediatelanguagetomixedmode.asp and have implemented it...
2
8531
by: Maxwell | last post by:
Hello, Im using MC++ VS.NET 2003 and am quite confused with with gcroot template and its use. The issue I am confused about is the need to (or not) delete a pointer to a managed object that you have created using gcroot. For example(from Managed VC++ 2003 Step by Step by Microsoft Press p510): class UnmanagedClass {
4
4562
by: John | last post by:
I'm having a major problem trying to use value types like System::Drawing::Rectangle with std::vector. Is it possible to use STL containers with these type of objects, or am I just doing something wrong? Thanks! using namespace System::Drawing; #include <vector>
6
7003
by: =?Utf-8?B?QWw=?= | last post by:
I am storing an array of strings in an unmanaged MFC class using gcroot as follows: gcroot<array<System::String^>^m_pArr; Nothing out of the ordinary there. However, if I try to use "delete m_pArr" in the code in order to deterministically delete it, I get the following error in VS05: error C2440: 'delete' : cannot convert from 'gcroot<T>' to 'void *' If I use the same with something other than an array i.e. using gcroot and
6
4599
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I have a class hierarchy distributed over 3 native C++ dlls. The base class has a .NET Windows.Form for status output via a gcroot<>. The gcroot is declared private - the sub classes only have access via a protected "print"-method. I need the different dlls as the sub classes implement the base class's pure virtual methods using different technologies. To use the native classes from outside their dlls I use the...
0
9731
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
10651
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
10393
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
10405
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
10136
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
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.