473,406 Members | 2,713 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,406 software developers and data experts.

ref Class and a value by reference

Hi, what i'm trying to do is obtaining a value from a function in a ref
class.
In this context I can't use the "ref" and "out" operators.
I have tried all i can imagine but still hasn't it working.
With an array it works great.

void Change(int a, array<int>^ m)
{
m[0]=a;
}

With this code, in the function that calls Change the value will be
changed.
But with an int it doesnt work
void Change(int a, int^ m)
{
m=a;
}

Any guess?

Aug 22 '06 #1
14 5769
>void Change(int a, int^ m)
{
m=a;
}

Any guess?
My guess is that you posted to the wrong newsgroup. This one is for
C#, not C++. But the C++/CLI reference parameter syntax is, IIRC

void Change(int a, int^% m)
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 22 '06 #2
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:Og**************@TK2MSFTNGP03.phx.gbl...
My guess is that you posted to the wrong newsgroup. This one is for
C#, not C++. But the C++/CLI reference parameter syntax is, IIRC

void Change(int a, int^% m)
Actually the syntax in C++ would be

void Change(int a, int& m) {}

-- Alan
Aug 22 '06 #3
In C++/CLI:
^% is for passing reference types by ref.
% is for passing value types by ref.
e.g.,
passing by ref:
void test(int %myInt, System::Object ^%myObject)

vs passing by value:
void test(int myInt, System::Object ^myObject)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Mattias Sjögren" wrote:
>
void Change(int a, int^ m)
{
m=a;
}

Any guess?

My guess is that you posted to the wrong newsgroup. This one is for
C#, not C++. But the C++/CLI reference parameter syntax is, IIRC

void Change(int a, int^% m)
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 22 '06 #4
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:34**********************************@microsof t.com...
In C++/CLI:
^% is for passing reference types by ref.
% is for passing value types by ref.
e.g.,
passing by ref:
void test(int %myInt, System::Object ^%myObject)

vs passing by value:
void test(int myInt, System::Object ^myObject)
David, why doesn't int& work?

-- Alan
Aug 22 '06 #5
That works fine also. I posted before I saw your post so I wasn't implying
yours was wrong.
Our converters use % to be consistent with the style of passing managed
reference types by ref. It's easier for some people to understand that "%"
means by ref, regardless of whether it's a value type or a reference type
(where it's combined with the 'hat').
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Alan Pretre" wrote:
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:34**********************************@microsof t.com...
In C++/CLI:
^% is for passing reference types by ref.
% is for passing value types by ref.
e.g.,
passing by ref:
void test(int %myInt, System::Object ^%myObject)

vs passing by value:
void test(int myInt, System::Object ^myObject)

David, why doesn't int& work?

-- Alan
Aug 22 '06 #6
I have to disagree, int& doesnt work in C++/CLI, the correct syntax is int%
as you mentioned before.

Willy.

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:60**********************************@microsof t.com...
| That works fine also. I posted before I saw your post so I wasn't
implying
| yours was wrong.
| Our converters use % to be consistent with the style of passing managed
| reference types by ref. It's easier for some people to understand that
"%"
| means by ref, regardless of whether it's a value type or a reference type
| (where it's combined with the 'hat').
| --
| David Anton
| www.tangiblesoftwaresolutions.com
| Instant C#: VB to C# converter
| Instant VB: C# to VB converter
| Instant C++: C#/VB to C++ converter
| C# Code Metrics: Quick metrics for C#
|
|
| "Alan Pretre" wrote:
|
| "David Anton" <Da********@discussions.microsoft.comwrote in message
| news:34**********************************@microsof t.com...
| In C++/CLI:
| ^% is for passing reference types by ref.
| % is for passing value types by ref.
| e.g.,
| passing by ref:
| void test(int %myInt, System::Object ^%myObject)
|
| vs passing by value:
| void test(int myInt, System::Object ^myObject)
| >
| David, why doesn't int& work?
| >
| -- Alan
| >
| >
| >
Aug 22 '06 #7

"Alan Pretre" <no@spamwrote in message
news:eY****************@TK2MSFTNGP06.phx.gbl...
| "David Anton" <Da********@discussions.microsoft.comwrote in message
| news:34**********************************@microsof t.com...
| In C++/CLI:
| ^% is for passing reference types by ref.
| % is for passing value types by ref.
| e.g.,
| passing by ref:
| void test(int %myInt, System::Object ^%myObject)
| >
| vs passing by value:
| void test(int myInt, System::Object ^myObject)
|
| David, why doesn't int& work?
|
David is right, passing value types by ref must use %.
This is managed C++/CLI (managed code using /clr compileroption) compiler
syntax not C++/ISO, & if for unmanaged code (or mixed) only.

Try to compile this using cl /LD /clr ....
#using <system.dll>

using namespace System;
using namespace System::Collections::Generic;

public ref class MyClass
{
public:
void TestFunc2(int& o) {...}
};

you'll get :
error C4956: 'int &' : this type is not verifiable
.... for the function argument o.

While this:
....
public ref class MyClass
{
public:
void TestFunc2(int& o)
{}
};
#pragma unmanaged
void TestFunc2(int& o) {...}

will pass when compiled with /clr, but produces unverifiable code, it won't
compile at all using clr:pure or /clr:safe.

Willy.


Aug 22 '06 #8
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:Oz**************@TK2MSFTNGP05.phx.gbl...
David is right, passing value types by ref must use %.
This is managed C++/CLI (managed code using /clr compileroption) compiler
syntax not C++/ISO, & if for unmanaged code (or mixed) only.
<snip>
will pass when compiled with /clr, but produces unverifiable code, it
won't
compile at all using clr:pure or /clr:safe.

I see what you are saying... but I don't see the same thing with this
program in VS.NET 2005,

#include "stdafx.h"
#include "stdio.h"

using namespace System;

static void Change(int& m) {
m = 3;
return;
}

int main(array<System::String ^^args) {
int m = 1;
Change(m);
printf("%d\n", m);
return 0;
}
These two project settings compile cleanly with no warnings...
Common Language Runtime Support (/clr)
Pure MSIL Common Language Runtime Support (/clr:pure)

While this one gives 466 errors, probably int& is in their somewhere, but so
is everything + the kitchen sink.
Safe MSIL Common Language Runtime Support (/clr:safe)
-- Alan
Aug 22 '06 #9
Thanks for the clarification Willy.
I know we did a technical evaluation of the proper C++/CLI parameter
protocol for our converters, and I recall now that the use of % for passing
value types by ref was not merely for consistency purposes with the passing
of reference types by ref.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Willy Denoyette [MVP]" wrote:
>
"Alan Pretre" <no@spamwrote in message
news:eY****************@TK2MSFTNGP06.phx.gbl...
| "David Anton" <Da********@discussions.microsoft.comwrote in message
| news:34**********************************@microsof t.com...
| In C++/CLI:
| ^% is for passing reference types by ref.
| % is for passing value types by ref.
| e.g.,
| passing by ref:
| void test(int %myInt, System::Object ^%myObject)
| >
| vs passing by value:
| void test(int myInt, System::Object ^myObject)
|
| David, why doesn't int& work?
|
David is right, passing value types by ref must use %.
This is managed C++/CLI (managed code using /clr compileroption) compiler
syntax not C++/ISO, & if for unmanaged code (or mixed) only.

Try to compile this using cl /LD /clr ....
#using <system.dll>

using namespace System;
using namespace System::Collections::Generic;

public ref class MyClass
{
public:
void TestFunc2(int& o) {...}
};

you'll get :
error C4956: 'int &' : this type is not verifiable
.... for the function argument o.

While this:
....
public ref class MyClass
{
public:
void TestFunc2(int& o)
{}
};
#pragma unmanaged
void TestFunc2(int& o) {...}

will pass when compiled with /clr, but produces unverifiable code, it won't
compile at all using clr:pure or /clr:safe.

Willy.


Aug 22 '06 #10

"Alan Pretre" <no@spamwrote in message
news:e8**************@TK2MSFTNGP03.phx.gbl...
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:Oz**************@TK2MSFTNGP05.phx.gbl...
| David is right, passing value types by ref must use %.
| This is managed C++/CLI (managed code using /clr compileroption)
compiler
| syntax not C++/ISO, & if for unmanaged code (or mixed) only.
|
| <snip>
|
| will pass when compiled with /clr, but produces unverifiable code, it
| won't
| compile at all using clr:pure or /clr:safe.
|
|
| I see what you are saying... but I don't see the same thing with this
| program in VS.NET 2005,
|
| #include "stdafx.h"
| #include "stdio.h"
|
| using namespace System;
|
| static void Change(int& m) {
| m = 3;
| return;
| }
|
| int main(array<System::String ^^args) {
| int m = 1;
| Change(m);
| printf("%d\n", m);
| return 0;
| }
|
|
| These two project settings compile cleanly with no warnings...
| Common Language Runtime Support (/clr)
| Pure MSIL Common Language Runtime Support (/clr:pure)
|
| While this one gives 466 errors, probably int& is in their somewhere, but
so
| is everything + the kitchen sink.
| Safe MSIL Common Language Runtime Support (/clr:safe)
|

Yes, but while your code compiles to MSIL (option pure) without a warning,
it generates unverifiable code, that means it cannot be called from pure
managed language code like C# (even not using PInvoke), native pointers
(int&) are non verifiable, really.

Try running peverify on the produced assembly Compiled with /clr, you'll get
a Unverifiable PE header error..., compile it with 'pure' as option and
watch the error list scroll of the screen when running PEverify. That means
that these assemblies are standalone non callable from anything else than
C++/CLI.

My code compiles to mixed code (the one with the #pragma unmanaged) which is
unverifiable by nature (the PE header indicates this), but it's still
callable from C# (the managed portion only though.

Note also, a ref class member function cannot use int&, the correct syntax
is int%.

Willy.

Aug 22 '06 #11

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:1C**********************************@microsof t.com...
| Thanks for the clarification Willy.
| I know we did a technical evaluation of the proper C++/CLI parameter
| protocol for our converters, and I recall now that the use of % for
passing
| value types by ref was not merely for consistency purposes with the
passing
| of reference types by ref.

That's the right decission, & in reality denotes a native pointer, which are
non verifiable attributes, it should not be used in managed code.

Willy.
Aug 22 '06 #12
OK thanks.

I made the leap to C# four years ago, they've added a bit since I last
worked in C++. Can't say that I miss it.

-- Alan
Aug 22 '06 #13

"Alan Pretre" <no@spamwrote in message
news:uA**************@TK2MSFTNGP04.phx.gbl...
| OK thanks.
|
| I made the leap to C# four years ago, they've added a bit since I last
| worked in C++. Can't say that I miss it.

Well, I find C++/CLI quite attractive, but there is still a lot to do at the
level of type unification, don't know if the C++ team still cares that much
about this.. Guess they are now busy with bug solving and to get the
stl/clr stuff right for the Orcas release.
Willy.
Aug 22 '06 #14
Ok. thanks a lot. It was the % character.
I really didn't know it existed :P
I have to take a look at C++/CLI i think
Willy Denoyette [MVP] wrote:
"Alan Pretre" <no@spamwrote in message
news:uA**************@TK2MSFTNGP04.phx.gbl...
| OK thanks.
|
| I made the leap to C# four years ago, they've added a bit since I last
| worked in C++. Can't say that I miss it.

Well, I find C++/CLI quite attractive, but there is still a lot to do at the
level of type unification, don't know if the C++ team still cares that much
about this.. Guess they are now busy with bug solving and to get the
stl/clr stuff right for the Orcas release.
Willy.
Aug 22 '06 #15

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

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
13
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes....
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
6
by: alan | last post by:
I'm creating a sort-of "wrapper" class which (partly) acts like a variable. Something like: template<class t> class cell{ t curval; public: /*public for debugging only - will be private in...
1
by: jc | last post by:
RE: Why use a CollectionBase class here vs dataset or dataview? I'm looking at some vb.net 2005 code that was generated from a homegrown Codesmith Template that generate all of the retreival and...
16
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName),...
2
by: Reggie | last post by:
Hi and TIA! I have a class file located in my root directory with all me web pages. I call/use the class and it works fine. I have no imports statements aspx or codebehind. My question is why? ...
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: 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:
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...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.