Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call a non-static function when I cannot make the function itself static

As part of a uni assignment, I need to create a self-checkout simulation in C# and add an additional function – in this case a clubcard points system.

I’ve created a Members class who will hold the points, but I’m trying to have my winforms UI display member name when the scan membership card button is clicked.

The GetName() function is below –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Members
    {
        // Attributes
        protected int membershipID;
        protected string firstName;
        protected string lastName;
        protected int clubcardPoints;

        // Constructor
        public Members(int membershipID, string firstName, string lastName, int clubcardPoints)
        {
            this.membershipID = membershipID;
            this.firstName = firstName;
            this.lastName = lastName;
            this.clubcardPoints = clubcardPoints;
        }

        // Operations
        public string GetName()
        {
            string name = firstName + " " + lastName;
            return name;
        }
        ...
    }

And here is a snippet from the UpdateDisplay function in UserInterface.cs.

    void UpdateDisplay()
    {
        // DONE: use all the information we have to update the UI:
        //     - set whether buttons are enabled
        //     - set label texts
        //     - refresh the scanned products list box

        if (selfCheckout.GetCurrentMember() != null)
        {
            lblMember.Text = Members.GetName();
        }
        ...
     }

Members.GetName() can’t be used because it isn’t a static function, but I don’t know if I can convert it into a static function. What would be the best way to get this working?

Appreciate any help, I hope it makes sense! Pretty new to this so I’m not sure if I’m wording it correctly.

>Solution :

Can you call selfCheckout.GetCurrentMember().GetName()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading