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

HELP: GetSqlBinary conversion in C# and VB.NET

P: n/a
Anyone know what the VB.NET equivalent of this C# statement is...

byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

Tried as hard as I could to do it in VB.NET but I cannot seem to do it.
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Gareth Parris" <gc******@hotmail.comm> schrieb
Anyone know what the VB.NET equivalent of this C# statement is...

byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

Tried as hard as I could to do it in VB.NET but I cannot seem to do
it.


Untested:
dim segbody as byte() = directcast(sqlReader.GetSqlBinary(3), byte())
--
Armin

Nov 20 '05 #2

P: n/a
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of bytes,
so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte()) 'Fails
Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus
Nov 20 '05 #3

P: n/a
Thanks Fergus. Funnily enough the C# code works perfectly. I'll try your VB
example right now.

Cheers,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of bytes, so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte()) 'Fails Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus

Nov 20 '05 #4

P: n/a
Fergus. Excellant stuff that worked nicely!

"Gareth Parris" <gc******@hotmail.comm> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...
Thanks Fergus. Funnily enough the C# code works perfectly. I'll try your VB example right now.

Cheers,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of

bytes,
so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte())

'Fails
Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus


Nov 20 '05 #5

P: n/a
Hi Gareth,

That op_ Explicit was niggling away in my mind and I've just remembered.

C# provides the ability to define explicit conversions which take place
when a variable is cast from one form to another. op_Explicit is the function
that allows other languages to access that convesion.

So
(byte []) sqlReader.GetSqlBinary(3);
and
SqlBinary.op_Explicit (sqlReader.GetSqlBinary(3));
are equivalent.

Regards,
Fergus
Nov 20 '05 #6

P: n/a
I was not aware of that. That makes more sense now.

Many thanks again,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
Hi Gareth,

That op_ Explicit was niggling away in my mind and I've just remembered.
C# provides the ability to define explicit conversions which take place when a variable is cast from one form to another. op_Explicit is the function that allows other languages to access that convesion.

So
(byte []) sqlReader.GetSqlBinary(3);
and
SqlBinary.op_Explicit (sqlReader.GetSqlBinary(3));
are equivalent.

Regards,
Fergus

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.