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

Conversion Question: C# -> C++ CLI

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);
}
Nov 17 '05 #1
6 2147
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);
}
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/
Nov 17 '05 #2
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)" <to****@mvps.org> wrote in message
news:eM**************@TK2MSFTNGP12.phx.gbl...
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);
}
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/

Nov 17 '05 #3
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?
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/
Nov 17 '05 #4
"Cannot convert parameter 2 from 'System::Collections::ArrayList' to
'System::Object^'"

"Tomas Restrepo (MVP)" <to****@mvps.org> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
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?
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/

Nov 17 '05 #5
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
"karch" <no****@spamu.com> wrote in message
news:uf*************@TK2MSFTNGP10.phx.gbl...
"Cannot convert parameter 2 from 'System::Collections::ArrayList' to
'System::Object^'"

"Tomas Restrepo (MVP)" <to****@mvps.org> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
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?
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/


Nov 17 '05 #6
If you're doing much conversion from C# to C++/CLI, download the demo of our
C# to C++ converter (www.tangiblesoftwaresolutions.com).

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"karch" wrote:
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);
}

Nov 17 '05 #7

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

Similar topics

2
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors,...
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
26
by: David W. Fenton | last post by:
A client is panicking about their large Access application, which has been running smoothly with 100s of thousands of records for quite some time. They have a big project in the next year that will...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
47
by: rawCoder | last post by:
Hi, Just wanted to know if there is any speed difference between VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc. ..NETs Convert.To<...> methods. And which is better to be...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.