Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

Reference to a non-shared member requires an object reference

I added sample code below and an image.  I have a DLL that i created which encrypts / decrypts a string.  When I add    Encrypts( some text ) I receive "Reference to a non-shared member requires an object reference" error.  Without the encrypt I do not recevie the error.

Any idea how to resolve?
Protected Sub btnLogin_Click(ByVal sender As Object, 
      ByVal e As EventArgs) Handles btnLogin.Click

   Dim _connStr As String = ConfigurationManager.ConnectionStrings("DBASE").ToString()

    Dim _login As New BOL.Login
    Dim ds As New DataSet
    If Page.IsValid Then

        Try
            _login.ConnectionString = _connStr
            _login.UserId = txtUserName.Text
            _login.Password = Encrypt
                              (txtPassword.Text.ToString())
        Catch

        End Try

Open in new window

Error.jpg
Avatar of kaufmed
kaufmed
Flag of United States of America image

You didn't post the code that defines your Encrypt and Decrypt methods (for obvious reasons, I'm sure), but it sounds like you didn't declare these methods as Shared, so they are instance methods--which means you need to create an instance of the class which houses these methods. Call the methods against the instance rather than as standalone functions.
Avatar of CipherIS

ASKER

How do I do that?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I added an image of the class - minus private info
Crypto.jpg
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By default, when you create a method in a class, you need an object to call it.

The way your class is built, you would need to to the following in order to call the method:
aes obj;
ojb.Encrypt("text")

Open in new window

In order to be able to call a method directly without having to declare a variable, as you do in your Click event, you need to declare the method as static (shared):
public static string Encrypt (string Data)

Open in new window

fixed my problem