Conversion Question: C# -> C++ CLI

  • Thread starter Thread starter karch
  • Start date Start date
K

karch

I am beginning to write my first applications with C++/CLI and was wondering
if someone could demonstrate the proper way to convert a sample piece of
code. I just need some help understanding the new syntax for handles and
iterating, etc. Thanks in advance for the help.

SoapFormatter formatter = new SoapFormatter();
byte[] buffer = new byte[dept.Capacity];
MemoryStream stream = new MemoryStream(buffer);

foreach (object o in dept) {
formatter.Serialize(stream, o);
}
 
Karch,
I am beginning to write my first applications with C++/CLI and was
wondering if someone could demonstrate the proper way to convert a sample
piece of code. I just need some help understanding the new syntax for
handles and iterating, etc. Thanks in advance for the help.

SoapFormatter formatter = new SoapFormatter();
byte[] buffer = new byte[dept.Capacity];
MemoryStream stream = new MemoryStream(buffer);

foreach (object o in dept) {
formatter.Serialize(stream, o);
}

[not tested]

SoapFormatter^ formatter = gcnew SoapFormatter();
array<Byte>^ buffer = gcnew array<Byte>(dept.Capacity);
MemoryStream^ stream = gcnew MemoryStream(buffer);

for each (Object^ o in dept) {
formatter->Serialize(stream, o);
}
 
Excellent! Worked perfectly, and I'm understanding how the new syntax works
(I think). One more question (I was experimenting while I waited for your
answer).

In this case, dept is an ArrayList:

formatter->Serialize(stream, dept);

but how do I cast dept to an Object^ (which is what the method is
expecting)? This is valid in C#, but having a problem in C++/CLI.

Thanks again.



Tomas Restrepo (MVP) said:
Karch,
I am beginning to write my first applications with C++/CLI and was
wondering if someone could demonstrate the proper way to convert a sample
piece of code. I just need some help understanding the new syntax for
handles and iterating, etc. Thanks in advance for the help.

SoapFormatter formatter = new SoapFormatter();
byte[] buffer = new byte[dept.Capacity];
MemoryStream stream = new MemoryStream(buffer);

foreach (object o in dept) {
formatter.Serialize(stream, o);
}

[not tested]

SoapFormatter^ formatter = gcnew SoapFormatter();
array<Byte>^ buffer = gcnew array<Byte>(dept.Capacity);
MemoryStream^ stream = gcnew MemoryStream(buffer);

for each (Object^ o in dept) {
formatter->Serialize(stream, o);
}
 
Karch,
Excellent! Worked perfectly, and I'm understanding how the new syntax
works (I think). One more question (I was experimenting while I waited for
your answer).

In this case, dept is an ArrayList:

formatter->Serialize(stream, dept);

but how do I cast dept to an Object^ (which is what the method is
expecting)? This is valid in C#, but having a problem in C++/CLI.

You shouldn't need to cast, since all objects have an implicit cast to
Object^.
What specific error are you getting?
 
I assume you have done something like this:

ArrayList al;
al.Add(1);
....

gcnew SoapFormatter()->Serialize(al);

This needs some explanation, since you have used a special C++/CLI feature
here: implicitly dereferenced variables. Read my post on the thread "using
C# when adding new feature" from yesterday for more details.

Marcus Heege
www.heege.net
 
Back
Top