By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

How to convert C# out parameter to C++?

P: n/a
For example:

public static void FillRow(Object obj, out SqlDateTime timeWritten, out
SqlChars message, out SqlChars category, out long instanceId)

Apr 18 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
public static void FillRow(Object obj, out SqlDateTime timeWritten, out
SqlChars message, out SqlChars category, out long instanceId)


public:
static void FillRow(
Object ^ obj,
[Out] SqlDateTime % timeWritten,
[Out] SqlChars ^ % message,
[Out] SqlChars ^ % category,
[Out] long % instanceId);

I believe SqlDateTime is a value class, and SqlChars is a ref class. You
only need the caret for ref classes. The % symbol means pass by reference.

Tom
Apr 18 '06 #2

P: n/a
Thanks, after add % and [Out] (seems it works fine without [Out] too?). The
code finally works.

However, when debugging, I keep get:

Cannot find either column "dbo" or the user-defined function or aggregate
"dbo.MyUDF", or the name is ambiguous.
"Tamas Demjen" wrote:
public static void FillRow(Object obj, out SqlDateTime timeWritten, out
SqlChars message, out SqlChars category, out long instanceId)


public:
static void FillRow(
Object ^ obj,
[Out] SqlDateTime % timeWritten,
[Out] SqlChars ^ % message,
[Out] SqlChars ^ % category,
[Out] long % instanceId);

I believe SqlDateTime is a value class, and SqlChars is a ref class. You
only need the caret for ref classes. The % symbol means pass by reference.

Tom

Apr 18 '06 #3

P: n/a
Btw, the code:

#include "stdafx.h"

using namespace System;
using namespace System::Data;
using namespace System::Data::Sql;
using namespace System::Data::SqlTypes;
using namespace Microsoft::SqlServer::Server;
using namespace System::Collections;
using namespace System::Runtime::InteropServices;

// select dbo.MyUDF()
// go
public ref class AddNewUDF
{
public:
[SqlFunctionAttribute(
FillRowMethodName = "FillRow",
TableDefinition = "Message nvarchar(20), Category nvarchar(10),
instanceId Int"//
)]
static IEnumerable^ MyUDF()
{
DataTable dt;
dt.Columns->Add("Test");
dt.Rows->Add(1);
return dt.Rows;
}

static void FillRow(Object obj, [Out] SqlChars^ % message, [Out]
SqlChars^ % category, [Out]long % instanceId)
{
message = gcnew SqlChars("Test");
category = gcnew SqlChars("Test");
instanceId = 212;
}
};
Apr 18 '06 #4

P: n/a
Never mind, Sorry I used wrong testing SQL statement.
Thanks very much.

"nick" wrote:
Thanks, after add % and [Out] (seems it works fine without [Out] too?). The
code finally works.

However, when debugging, I keep get:

Cannot find either column "dbo" or the user-defined function or aggregate
"dbo.MyUDF", or the name is ambiguous.
"Tamas Demjen" wrote:
public static void FillRow(Object obj, out SqlDateTime timeWritten, out
SqlChars message, out SqlChars category, out long instanceId)


public:
static void FillRow(
Object ^ obj,
[Out] SqlDateTime % timeWritten,
[Out] SqlChars ^ % message,
[Out] SqlChars ^ % category,
[Out] long % instanceId);

I believe SqlDateTime is a value class, and SqlChars is a ref class. You
only need the caret for ref classes. The % symbol means pass by reference.

Tom

Apr 18 '06 #5

P: n/a
nick wrote:
Thanks, after add % and [Out] (seems it works fine without [Out] too?).


C#: void f(ref int x);
same as
C++: void f(int% x);

C#: void f(out int x);
same as
C++: void f([Out] int% x);

The only difference is the [Out] attribute, which I think is mainly used
to determine the marshalling strategy. The [Out] attribute suggests that
the function doesn't use the argument as an input, it is strictly an
output, so it only needs to be marshalled in one direction. If you omit
[Out], it suggests that the function reads AND writes the given
argument, and therefore it must be marshalled back and forth. The
difference may not be important in a desktop application, but in a Web
service (or any distributed system) you might want to pay attention to
these details, in order to avoid unnecessary network traffic.

Example:

void GetValue([Out] int% x) { x = 10; }
// x is written only

void AddValue(int% x) { x = x + 10; }
// x is read and written

Tom
Apr 18 '06 #6

P: n/a
BTW, 'long' in C# is Int64 in C++/CLI, not 'long'.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Tamas Demjen" wrote:
public static void FillRow(Object obj, out SqlDateTime timeWritten, out
SqlChars message, out SqlChars category, out long instanceId)


public:
static void FillRow(
Object ^ obj,
[Out] SqlDateTime % timeWritten,
[Out] SqlChars ^ % message,
[Out] SqlChars ^ % category,
[Out] long % instanceId);

I believe SqlDateTime is a value class, and SqlChars is a ref class. You
only need the caret for ref classes. The % symbol means pass by reference.

Tom

Apr 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.