Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2007

Tip: Looking for answers? Try searching our database.

CLI ref structs.  Is there any benefit to using it.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DaTurk - 19 Apr 2007 21:01 GMT
Hi,

I'm coding a layer into my application using CLI, between a unmanaged
and a c# layer.  So, I have to marshal some unmanaged c++ structures
to structures usable in c#.

My solution was to marshal the unmanaged C++ structures to CLI ref
structs, these being usable in the c# layer.  Why I did it this way is
I want all of the business logic in the CLI layer.  Because he c#
layer is purely presentaion.

My question is, what is the point of using CLI ref structs?
Bruno van Dooren - 20 Apr 2007 07:50 GMT
> I'm coding a layer into my application using CLI, between a unmanaged
> and a c# layer.  So, I have to marshal some unmanaged c++ structures
[quoted text clipped - 6 lines]
>
> My question is, what is the point of using CLI ref structs?

Hi,

the point of using ref structs is that they translate to classes with public
members on the C# side without the need for marshalling. As a result, you
can pass those structures to the C# side or to .NET methods.
If these structures do not leave the CLI side, there is no point to
marshaling them because C++/CLI is designed to use unmanaged structures and
classes without a problem. In that case, marshaling them to managed
stuctures is redundant.

Signature

Kind regards,
   Bruno van Dooren  MVP - VC++
   http://msmvps.com/blogs/vanDooren
   bruno_nos_pam_van_dooren@hotmail.com

DaTurk - 20 Apr 2007 17:11 GMT
> > I'm coding a layer into my application using CLI, between a unmanaged
> > and a c# layer.  So, I have to marshal some unmanaged c++ structures
[quoted text clipped - 22 lines]
>    http://msmvps.com/blogs/vanDooren
>     bruno_nos_pam_van_doo...@hotmail.com

I'm not sure I understand.  Why wouldn't I just use a CLI class then.
What's the difference?  Is there any real benefit though?
Ben Voigt - 20 Apr 2007 18:32 GMT
> I'm not sure I understand.  Why wouldn't I just use a CLI class then.
> What's the difference?  Is there any real benefit though?

Are you asking about "ref struct vs ref class" or "ref struct vs native
class" or "ref struct vs value struct"?

ref struct and ref class are related in the same way as native struct and
native class -- there is no runtime difference.  The only thing affected is
that struct, ref struct, and value struct all start with implicit "public:"
and class, ref class, and value class all start with implicit "private:".
ref class and ref struct are both C++/CLI equivalents to C# struct, all
three generate a garbage collected pass-by-ref CLR type, and all three have
different default member visibility.
Tamas Demjen - 20 Apr 2007 18:57 GMT
> ref class and ref struct are both C++/CLI equivalents to C# struct

I think you meant C# class.

Tom
Ben Voigt - 20 Apr 2007 19:38 GMT
>> ref class and ref struct are both C++/CLI equivalents to C# struct
>
> I think you meant C# class.

Yes indeed.

ref class and ref struct are both C++/CLI equivalents to C# class
value class and value struct are both C++/CLI equivalents to C# struct

Thanks for calling out my typo.
Tamas Demjen - 20 Apr 2007 18:55 GMT
> I'm not sure I understand.  Why wouldn't I just use a CLI class then.
> What's the difference?  Is there any real benefit though?

C++/CLI -------> C#
====================
ref class -----> class
ref struct ----> class
value class ---> struct
value struct --> struct

The C++/CLI keyword "ref" always indicates a reference type (C# class),
while the "value" keyword always refers to a value type (C# struct).

What confuses you is that in C# class and struct mean completely
different things than in C++. In C++, the only difference between class
and struct is whether members are public or private by default. In C#,
on the other hand, a class is a reference type, and a struct is a value
type, and they have nothing to do with public vs private access.

Tom

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.