iano said:
I am trying to clone a VB6 app in Vb.Net as a learning exercise. For
this effort I am not using Visual Studio.Net. So Far I have a form
with a label, combobox and a command button. As I have done in VB6, I
like to keep form code in the form and other code in a separate module
so it is easier to reuse.
As I understand it, regular modules do not exist in VB.net so
I added a class to the project and called it MoreCode. It seemed to me
that I could execute methods in the class by using
MoreCode.DoSomething( argument list).
But so far, I have not succeeded in getting the VB compiler to
recognize the MoreCode class.
Please tell me in as much detail as possible (or point to an example)
that illustrate what is needed.
Thanks in Advance,
IanO
Here's something I did from some other examples I found online to encrypt and decrypt text:
the new myEncryptDecrypt class
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class MyEncryptDecrypt
' Encrypt the text
Public Shared Function EncryptText(ByVal strText As String) As String
Return Encrypt(strText, "&%#@?,:*")
End Function
'Decrypt the text
Public Shared Function DecryptText(ByVal strText As String) As String
Return Decrypt(strText, "&%#@?,:*")
End Function
'The function used to encrypt the text
Private Shared Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
Dim des As New DESCryptoServiceProvider
Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
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
'The function used to decrypt the text
Private Shared Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Dim inputByteArray(strText.Length) As Byte
Try
byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
End Class
Then I call it this way:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
strtext = TextBox1.Text
TextBox2.Text = MyEncryptDecrypt.EncryptText(strtext)
Button2.Enabled = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
strtext = TextBox2.Text
TextBox3.Text = MyEncryptDecrypt.DecryptText(strtext)
End Sub
strtext is what is passed to the myEncryptDecrypt Class.
Each function is called( Decrypt or Encrypt) and strtext is passed to that function. Once I got it figured out correctly,
whenever
I type in myEncryptDecrypt. then Intelli-sense would show,,,,,,,,,,Decrypt & Encrypt as the choices and then after the
( , Like this: TextBox2.Text = myEncryptDecrypt.Decrypt(strtext) <<<<<<< the string I wanted to decrypt.
So, that is my basic understanding of how to do a Class. I am not real good at it, but, I think this will give you the general
idea.
james