Saturday, February 25, 2012

Overloading add/subtract operators for CLR UDT

Hi,

I have an implementation of the UDT - 3-dimentional vector. In my code I have implemented add, subtract and multiply methods for the type. I have also implemented overloaded operators for +/-/* in my C# code. Those overloaded operator are working as expected in C# tests. However when I��m trying to use +/-/* operators in T-SQL over my UDT it returns the following error:

Invalid operator for data type. Operator equals add, type equals Vector.

The following fragment does work:

DECLARE @.v1 Vector, @.v2 Vector, @.v3 Vector;

SELECT @.v1 = CAST('1,1,1' as Vector), @.v2 = CAST('2,2,2' as Vector)

SELECT @.v1 'v1', @.v2 'v2', @.v1.[Add](@.v2) 'v1 + v2'

And this fragment does not work:

DECLARE @.v1 Vector, @.v2 Vector, @.v3 Vector;

SELECT @.v1 = CAST('1,1,1' as Vector), @.v2 = CAST('2,2,2' as Vector)

SELECT @.v1 'v1', @.v2 'v2', @.v1+@.v2 'v1 + v2'

I guess that SQL Server is not aware of the operators�� overload I have implemented in the C# code. Is there any way to instruct SQL Server to use overloaded operators in the T-SQL so the code will look naturally @.a + @.b instead of @.a.[Add](@.b) and as a result use standard summary functions SUM() instead of writing user defined aggregate function for the Vector type field?

Maxim

? No, unfortunately overloading in SQLCLR is not possible. By the way, I prefer to do statics for Add/Subtract/etc, so instead of: @.a.Add(@.b) You'd do: type::Add(@.a, @.b) That way it's a little bit closer to the "a + b" syntax... But obviously not quite the same. You'll also still have to create your own UDA. -- Adam MachanicPro SQL Server 2005, available nowhttp://www..apress.com/book/bookDisplay.html?bID=457-- <Maxim Michtchenko@.discussions.microsoft.com> wrote in message news:03e7313f-081c-44f5-89f0-ce44d7de911d@.discussions.microsoft.com... Hi, I have an implementation of the UDT - 3-dimentional vector. In my code I have implemented add, subtract and multiply methods for the type. I have also implemented overloaded operators for +/-/* in my C# code. Those overloaded operator are working as expected in C# tests. However when I��m trying to use +/-/* operators in T-SQL over my UDT it returns the following error: Invalid operator for data type. Operator equals add, type equals Vector. The following fragment does work: DECLARE @.v1 Vector, @.v2 Vector, @.v3 Vector; SELECT @.v1 = CAST('1,1,1' as Vector), @.v2 = CAST('2,2,2' as Vector) SELECT @.v1 'v1', @.v2 'v2', @.v1.[Add](@.v2) 'v1 + v2' And this fragment does not work: DECLARE @.v1 Vector, @.v2 Vector, @.v3 Vector; SELECT @.v1 = CAST('1,1,1' as Vector), @.v2 = CAST('2,2,2' as Vector) SELECT @.v1 'v1', @.v2 'v2', @.v1+@.v2 'v1 + v2' I guess that SQL Server is not aware of the operators�� overload I have implemented in the C# code. Is there any way to instruct SQL Server to use overloaded operators in the T-SQL so the code will look naturally @.a + @.b instead of @.a.[Add](@.b) and as a result use standard summary functions SUM() instead of writing user defined aggregate function for the Vector type field? Maxim|||

Thanks,

I guess I'll have to accept that. By the way I agree with you - static method looks clearner.

Maxim

|||

I have a method that is overloaded. One takes 3 parameters and one takes 5. If overloading does not work, why does the CLR Function that I am trying to create work if I pass in 5 parameters? Does it take the method with the most parameters?

No comments:

Post a Comment