473,394 Members | 1,870 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,394 software developers and data experts.

using collections in c++

Hi all,

I want to iterate through a collection and at each item in the
collection access a particular property. In C# i know how to do this
easily, but with c++ I am a bit mystified Below is some sample code to
give you an idea of what i am trying to do.
SPGlobalAdmin * globAdmin = new SPGlobalAdmin();
SPVirtualServerCollection * servers = globAdmin->VirtualServers;
SPVirtualServer * vserver;
for (i = 0; i < servers->Count; i++)
{
//access properties of each vserver here
}

If this were c# I would just use a foreach loop like this

SPGlobalAdmin globAdmin = new SPGlobalAdmin();
foreach (SPVirtualServer vServer in globAdmin.VirtualServers)
{
string url = vServer.Url;
}
How to do this with c++?

Apr 30 '06 #1
3 1302
tessellated wrote:
Hi all,

I want to iterate through a collection and at each item in the
collection access a particular property. In C# i know how to do this
easily, but with c++ I am a bit mystified Below is some sample code to
give you an idea of what i am trying to do.
SPGlobalAdmin * globAdmin = new SPGlobalAdmin();
SPVirtualServerCollection * servers = globAdmin->VirtualServers;
SPVirtualServer * vserver;
for (i = 0; i < servers->Count; i++)
{
//access properties of each vserver here
}

If this were c# I would just use a foreach loop like this

SPGlobalAdmin globAdmin = new SPGlobalAdmin();
foreach (SPVirtualServer vServer in globAdmin.VirtualServers)
{
string url = vServer.Url;
}
How to do this with c++?


If you're using C++/CLI (VC++ 2005) then you can use for each (note the
space) just as you would in C#. Otherwise, read on.
What kind of a collection is SPVirtualServersCollection? Each collection
class has some API to allow access to it's members. In the framework
classes, this API is usually expressed as IEnumerable, IList, ICollection,
IDictionary (or their generic equivalents in 2.0), or a combination of
these.

The C# foreach statement simply packages usage of the IEnumerable API into a
neat bit of syntactic sugar.

foreach (T t in C)
{
// stuff
}

is 100% equivalent to (and translated by the compiler literally to):

IEnumerator e = C.GetEnumerator();
while (e.MoveNext())
{
T t = (T)e.Current;

// stuff
}

You can use the same code in Managed C++ (VS2002/2003) to access framework
collections.

Of course, if you're talking about a C++ native collection such as a
std::vector, or on of the MFC collection classes, then they have different
APIs - but I gather that's not the kind of collection you're talking about.

-cd
Apr 30 '06 #2
Well, I'm using VS2003 so the foreach option is not available. I did
discover the IEnumerator and that is working well, so that makes me
happy! :)

But now for my other issue. I need to grab the server's url out of the
collection in the form of a char*. The problem is addr below is of type
System::String and I don't know how to convert over.
System::Collections::IEnumerator * iEnum = NULL;
SPGlobalAdmin * globAdmin = new SPGlobalAdmin();
SPVirtualServerCollection * servers = globAdmin->VirtualServers;
iEnum = servers->GetEnumerator();

System::String * addr;
while(iEnum->MoveNext())
{
SPVirtualServer * vs =
static_cast<SPVirtualServer*>(iEnum->get_Current());
addr = vs->Url->ToString();
}

Apr 30 '06 #3
tessellated wrote:
Well, I'm using VS2003 so the foreach option is not available. I did
discover the IEnumerator and that is working well, so that makes me
happy! :)

But now for my other issue. I need to grab the server's url out of the
collection in the form of a char*. The problem is addr below is of
type System::String and I don't know how to convert over.
System::Collections::IEnumerator * iEnum = NULL;
SPGlobalAdmin * globAdmin = new SPGlobalAdmin();
SPVirtualServerCollection * servers = globAdmin->VirtualServers;
iEnum = servers->GetEnumerator();

System::String * addr;
while(iEnum->MoveNext())
{
SPVirtualServer * vs =
static_cast<SPVirtualServer*>(iEnum->get_Current());
addr = vs->Url->ToString();
}


See System::Runtime::InteropServices::Marshal::StringT oHGlobalAnsi

-cd
May 1 '06 #4

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

Similar topics

5
by: a | last post by:
Is there a performance hit if I have many "using" statements that are unnecessary? For example: using System.Collections; when nowhere in my code I'm using System.Collections namespace. I'm...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
14
by: Steve Teeples | last post by:
I don't understand why I cannot use a property to modify data within a struct. Can someone tell me why I get the error "Cannot modify the return value of "myData.TheData" because it is not a...
1
by: Thiero | last post by:
Hi I posted s thread but did have any reply, I am a new programmer and really wants someone to help me on how to use TreeMap for this code cos I want to it to be able to handle the options from 6...
1
by: Joachim | last post by:
Can you import namespaces into an ASP .NET 2005 web site project C# file? For instance, when I run using System.Collections.Generic; public partial class _Default : System.Web.UI.Page, IMainView...
6
by: Mark Rae | last post by:
Hi, I'm in the process of updating an ASP.NET v1.1 web app to v2. The app uses ActiveDirectory a great deal, and I'm trying to use the new System.Collections.Generic namespace where possible,...
3
by: Marco Shaw | last post by:
I've got some C# code to create a custom PowerShell cmdlet with these statements: .... using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; .... ...
3
by: Tony Johansson | last post by:
Hello! You can set target Module for AttributeUsage. I just wonder what does it mean with module ? //Tony
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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...

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.