Reference to a non-shared member requires an object reference

Posted on 2011-10-13
Last Modified: 2012-06-21
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

            _login.ConnectionString = _connStr
            _login.UserId = txtUserName.Text
            _login.Password = Encrypt

        End Try

Open in new window

Question by:CipherIS
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    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.
    LVL 1

    Author Comment

    How do I do that?
    LVL 74

    Accepted Solution

    What is the name of the class where you defined Encrypt and Decrypt?
    LVL 1

    Author Comment

    I added an image of the class - minus private info
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    So, as stated by kaufmed, you need to create an instance of your "aes" class:

        aes crypto = new aes();
        _login.Password = crypto.Encrypt(txtPassword.Text.ToString()) // <-- here we are using our instance "crypto"
    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    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;

    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

    LVL 1

    Author Closing Comment

    fixed my problem

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now