?syntax to recognize code in a 2nd file

I

iano

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
 
J

james

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
 
M

Michael C#

It looks like you're looking for Shared Subs and Shared Functions. These
are functions you can call without instantiating an instance of the object.
Here's a quick example to show the difference between a Shared Sub and a
Regular Class Method Sub:

' This class has to be instantiated to invoke the PrintHello method
Public Class SomeCode
Public Sub PrintHello()
Console.WriteLine("Hello World")
End Sub
End Class

' This class' Shared PrintHello method can be called without a
' MoreCode object being instantiated
Public Class MoreCode
Public Shared Sub PrintHello()
Console.WriteLine("Hello World")
End Sub
End Class

To call these functions, you would use code similar to the following:

' We instantiate a SomeCode object before we can call it's
' PrintHello() method
Dim s As New SomeCode
s.PrintHello()

' We don't need to instantiate a MoreCode object to call it's
' PrintHello() method
MoreCode.PrintHello()
 
C

Cor Ligthert

Iano,

When you want to use your MoreCode class in your other Class, than you can
make from your class an object. Instance it.

dim myMoreCode as New MoreCode.

Now you can use that object with myMoreCode.function(mystring).

This myMoreCode object, will be removed when your function that creates this
goes out of scope (reaches the end sub).

Strange when you are used too classic coding. Yes, however it keeps your
programs nice small at run time. Althoughs OOP uses more overhead what you
should have to forget to think about, because you don't win real time to
avoid that.

I hope this helps,

Cor
 
I

iano

Cor, your explantion helps a lot.
However, I managed to get a syntax error.

Rather than use myMoreCode in the executable part of the code, I
changed the
definition to:

Imports System

Namespace MoreCodecls <-- Should this be the same as the Class Name?

Public Class MoreCodecls

Public Sub New()

End Sub

Public Function strDBConnection(ByVal strServer As String, _
ByVal strDataBase As String) As String
....
and when I attempted to call the code from the form
Private Sub CboServersSelectedIndexChanged(sender As System.Object, e
As System.EventArgs)
Dim strConn As String
Dim MoreCode as New MoreCodecls

strConn = MoreCode.strDBConnection(Me.cboServers,"SomeName")
messagebox.Show(strConn)

End Sub
I got an error BC30182 in the line Dim MoreCode as New MoreCodecls

Hopefully, one more answer and I'll be able to complete this program.

IanO
 
C

Cor Ligthert

Iano,

That class is it a seperated project (a dll class libary by instance) (and
than you need to set a reference) or a class in the same project?

Cor
 
I

iano

As far as I am concerned it is in the same project.

But since I am such a beginner with dot Net, I may have done
*something* that makes the compiler think it is separate.

IanO
 
C

Cor Ligthert

Iano,

Than try to paste it on the same page (beneath it) as is your program, than
you know it for sure.

I hope this helps,

Cor
 
I

iano

In the meantime, I discovered
Public Module MiscFunctions


my other code

End Module

and the code is running!
Thanks Cor, I'll be trying the class again in a week or so.

IanO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top