String to Byte()
Messages   Related Types
This message was discovered on microsoft.public.dotnet.faqs.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Mark Finch via .NET 247 (VIP)
I am trying to get past a simple error that I am recieving when attempting to use a valid VB6 class in VB.Net

my code is as follows:

in VB6

Public Function putN(ByVal str, buffer As Variant, dec As Long) As Boolean
putN = False
Dim Strb() As Byte
Dim e As Long
Dim i As Long
e = UBound(buffer)

Strb = str
For i = e To 0 Step -1
buffer(i) = Strb(i * 2)
Next
putN = True
End Function

In VB.Net it is:

Public Function putN(ByVal str As String, ByRef buffer As Object, ByVal dec As Long) As Boolean
putN = False
Dim Strb() As Byte
Dim e As Long
Dim i As Long
e = UBound(buffer)
Strb = str

For i = e To 0 Step -1
buffer(i) = Strb(i * 2)
Next
putN = True
End Function

I can not get the VB.Net to compile without the error:

Value of type 'String' cannot be converted to '1-dimensional array of Byte'

Any suggestions on how to recode?

Thanks
--------------------------------
From: Mark Finch

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>kGlZyhGSLkCuYeARENbgHg==</Id>
Reply to this message...
 
    
jamie
try using the system.bitconverter class.

good luck
jamie

[Original message clipped]

VB.Net
[Original message clipped]

Reply to this message...
 
    
Poolbeer \(MCP\)
Mark,

You could use the Encoding class in the System.Text namespace.

-- Your method --
Public Function putN(ByVal str As String, ByRef buffer As Object, ByVal dec
As Long) As Boolean
putN = False
Dim Strb() As Byte
Dim e As Long
Dim i As Long
e = UBound(buffer)
Strb = str <----- Here is the Error: You're trying to convert a
string to a byte array.

For i = e To 0 Step -1
buffer(i) = Strb(i * 2)
Next
putN = True
End Function

-- Modified Method --
Public Function putN(ByVal str As String, ByRef buffer As Object, ByVal dec
As Long) As Boolean
putN = False
Dim Strb() As Byte
Dim e As Long
Dim i As Long
e = UBound(buffer)

'**********************************
Strb = Encoding.Default.GetBytes(str.Chars)
'***************************************

For i = e To 0 Step -1
buffer(i) = Strb(i * 2)
Next
putN = True
End Function

Hope it helps.

--

Poolbeer (MCP)

"Mark Finch via .NET 247" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
> I am trying to get past a simple error that I am recieving when attempting
to use a valid VB6 class in VB.Net
[Original message clipped]

Reply to this message...
 
 
System.Text.Encoding




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com