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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

For example:

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)

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
 
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 said:
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
 
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;
}
};
 
Never mind, Sorry I used wrong testing SQL statement.
Thanks very much.

nick said:
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 said:
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
 
nick said:
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
 
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 said:
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
 
Back
Top