[RESOLVED] [2005] Question about DLL and New
Ok, I wrote a class that I use in many projects that is used to encrypt and decrypt strings. I ahve decided to just compile it into a dll and use it like that, but what I was wondering is how do I change the class so that I do not have to call the new function when I dim the object? First of all, is there a way to do this and second, how?
for example right now I use it like so
VB Code:
Dim enc As New Encrypt.StringEncrypt
enc.Encrypt("Hello World")
What I would like to do is just write
VB Code:
Dim enc As Encrypt.StringEncrypt
enc.Encrypt("Hello World")
Thanks in advance
Re: Question about DLL and New
Make your Encrypt function a Shared function:
VB Code:
Friend Shared Function Encrypt (s As String) As String
Re: Question about DLL and New
When I do that, I can no longer call those functions
Re: Question about DLL and New
Make it public instead of friend. I didn't take into account that it is in a separate DLL.
Re: Question about DLL and New
When I do that I get the following error
Warning 1 Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated. C:\Documents and Settings\bhmahler\My Documents\Visual Studio 2005\Projects\EDMS\EDMS\frmAdministration.vb 3267 9 EDMS
with this code
VB Code:
Dim enc As Encrypt.StringEncrypt
enc.Encrypt("Hello")
This is my class for the dll
VB Code:
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.IO
Public Class StringEncrypt
Public Shared Function Encrypt(ByVal strText As String) As String
Dim strPwd As String = "***"
Dim i As Integer, c As Integer
Dim strBuff As String = String.Empty
For i = 1 To Len(strText)
c = Asc(Mid$(strText, i, 1))
c = c + Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff &= Chr(c And &HFF)
Next i
Return strBuff
End Function
Public Sub New()
MyBase.New()
End Sub
Public Shared Function Decrypt(ByVal strText As String) As String
Dim strPwd As String = "***"
Dim i As Integer, c As Integer
Dim strBuff As String = String.Empty
For i = 1 To Len(strText)
c = Asc(Mid$(strText, i, 1))
c = c - Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff = strBuff & Chr(c And &HFF)
Next i
Return strBuff
End Function
Public Shared Function GenerateHash(ByVal text As String) As String
Dim Ue As New UnicodeEncoding()
Dim ByteSourceText() As Byte = Ue.GetBytes(text)
Dim Md5 As New MD5CryptoServiceProvider()
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
Return Convert.ToBase64String(ByteHash)
End Function
End Class
Re: Question about DLL and New
Are you using 2003 or 2005?
Re: Question about DLL and New
2005 sorry, meant to put that in my title
Re: [2005] Question about DLL and New
I am not getting that Warning in 2003, but I think it is there to inform you that your code may be a bit misleading. Here is microsofts answer: http://msdn2.microsoft.com/en-us/library/y6t76186.aspx
In your code, you should just be able to go:
VB Code:
Encrypt.StringEncrypt.Encrypt("Blah")
To get it to work.
I don't have 2005 on my machine, but if you did something like:
VB Code:
Dim oMath As System.Math
Dim dblVal As Double
dblVal = oMath.Cos(6.0)
Do you get the same warning?
Re: [2005] Question about DLL and New
Ok I got it, Thanks for your help