J.G.Sable
J.G.Sable

Reputation: 1388

Calling a function from another class in C#/.NET MVC App

I'm trying to create better separation of concerns for code reuse in my program, that way I don't have a bloated controller that does all these different things.

for instance, in my application I have a user profile where users can upload a profile pic. If they don't customize their profile pic, I set the default profile pic to an avatar. I do this through a method to check if their profile pic string is null.

I created a folder called HelperMethods and created a class called UserHelperMethods which currently has one function:

namespace HiRatik.Stories.HelperMethods
{
    public class UserHelperMethods
    {
        //checks if the user's profile pic is null and sets it to default pic if it is
        public string GetUserProfilePic(ApplicationUser user)
        {
            if (user.ProfilePic == null)
            {
                user.ProfilePic = "profile_pic_default.png";
            }

            return user.ProfilePic;
        }
    }
}

enter image description here

Now, in the controller, under the controller's folder, I added using HiRatik.Stories.HelperMethods;

and tried to call the public function GetUserProfilePic from the UserController. But I'm getting an error on the implementation. I'd like to be able to place a lot of these general functions related to users in another class like UserHelperMethods to clean up the bulk in the controller, but I'm missing something on the implementation. the using statement at the top is grayed out, so it's not picking up the function call. Any ideas?

enter image description here

Upvotes: 2

Views: 8884

Answers (5)

khaled  Dehia
khaled Dehia

Reputation: 947

you could update your code to one of the following A -

namespace HiRatik.Stories.HelperMethods
{
    public class UserHelperMethods
    {
         private static UserHelperMethods _instance = null;
         public static UserHelperMethods Instance()
         {
           if(_instance == null)
           {
             _instance = new UserHelperMethods();
           }
           return _instance;
         }
        //checks if the user's profile pic is null and sets it to default pic if it is
        public string GetUserProfilePic(ApplicationUser user)
        {
            if (user.ProfilePic == null)
            {
                user.ProfilePic = "profile_pic_default.png";
            }

            return user.ProfilePic;
        }
    }
}

and inside your Controller just use call it like this

UserHelperMethods.Instance().GetUserProfilePic(founduser);

Or the easiest way

var helperObj = new UserHelperMethods();
helperObj.GetUserProfilePic(founduser);

the diff you won't need to create instance all the time in diff controllers

I wish this help you !!

Upvotes: 0

Matthew Eskolin
Matthew Eskolin

Reputation: 658

I would consider making these methods static.

namespace HiRatik.Stories.HelperMethods
{
    public class UserHelperMethods
    {
        //checks if the user's profile pic is null and sets it to default pic if it is
        public static string GetUserProfilePic(ApplicationUser user)
        {
            if (user.ProfilePic == null)
            {
                user.ProfilePic = "profile_pic_default.png";
            }

            return user.ProfilePic;
        }
    }
}

If the helper methods don't rely on any state within the UserHelperMethods object, this will make it much easier to call your methods from anywhere, as there is no longer a need to create an instance of the UserHelperMethods type. You can call the method like this.

UserHelperMethods.GetUserProfilePic(foundUser);

Upvotes: 1

Saif
Saif

Reputation: 2679

just create instance of the class

var myInstance = new UserHelperMethods();

then just use myInstance object to access the functions in UserHelpMethods class

so you can call any function in UserHelpMethods like this

myInstance.FunctionName();

so in your case it will be like

myInstance.GetUserProfilePic(foundUser);

Upvotes: 0

Kajetan Kazimierczak
Kajetan Kazimierczak

Reputation: 59

You may want to make this into an Extension. Then you will be able to call it like this:

user.GetProfilePic();

The changes you have to do is, to make both your class and method static and have the this keyword before the parameter. Something like

public static class ApplicationUserExtensions
    {
        //checks if the user's profile pic is null and sets it to default pic if it is
        public static string GetProfilePic(this ApplicationUser user)
        {
            if (user.ProfilePic == null)
            {
                user.ProfilePic = "profile_pic_default.png";
            }

            return user.ProfilePic;
        }
    }

Upvotes: 1

Jimenemex
Jimenemex

Reputation: 3166

You need to add an instance of the helper method class to every class you want to use it in.

UserHelpMethods helper = new UserHelperMethods();

then you can use it as:

helper.GetUserProfilePic(foundUser);
...
help.DoSomethingImportant(foundUser);

Upvotes: 3

Related Questions