2

The code below is giving an error for Left(strEncrKey, 8). The error says that public property Left has no parameters return.

Code

Public Function Encrypt(ByVal strText As String) As String Dim strEncrKey As String = "welcome123" Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF} Try **Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))** Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText) Dim des As New DESCryptoServiceProvider Dim ms As New MemoryStream Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write) cs.Write(InputByteArray, 0, InputByteArray.Length) cs.FlushFinalBlock() Return Convert.ToBase64String(ms.ToArray()) Catch ex As Exception Return ex.Message End Try End Function 

1 Answer 1

2

This code is probably inside a Form. Well, Left is a property of that form and the compiler things that you mean it (i.e. Me.Left).

To disambiguate this you have to use the fully qualified name of the Left function – or better, not use it at all (it’s deprecated). Use the String class methods instead:

Dim bykey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8)) 

Notice that I’ve omitted the parentheses and the As … on the type declaration. Denoting the type is redundant if you have Option Strict and Option Infer specified in the project options, which I highly recommend.

Sign up to request clarification or add additional context in comments.

2 Comments

s.Substring(8) is not the same as Left(s, 8) - you want .Substring(0, 8). And there's nothing about deprecation in the docs - indeed, Left has the perhaps-nice property of not throwing when the string hasn't got aas many characters as are asked for, whereas Substring(0, 8) will...
@AakashM MSDN sucks in this regard, that’s all I can say. Functions such as Left aren’t portable since they are omitted from VB core mode (think Windows Phone 7 etc.) … Microsoft is overly cautious when labelling features as “deprecated” even though they should have been dropped long ago.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.