Tudor Gafiuc
Tudor Gafiuc

Reputation: 145

Calling method from another class from another file C#

I have declared a namespace in Feed.aspx.cs . This namespace contains a class and this class contains a method: Feed.aspx.cs

namespace GetUser
{
public class MyFeedClass
{
   public string getUserID()
   {

       MembershipUser user = Membership.GetUser(HttpContext.Current.User.Identity.Name);
       HttpContext.Current.Session["x"] = user.ProviderUserKey.ToString();
       string test = (string)HttpContext.Current.Session["x"];
       return test;
   }

}

}

Now, from MyPage.aspx.cs, I'd like to call the getUserID() method. How can I do that?

Upvotes: 4

Views: 33388

Answers (2)

iceheaven31
iceheaven31

Reputation: 887

Make sure you include the namespace in your code-behind:

using GetUser;

Make the public function a static one in the MyFeedClass:

public static string getUserID() {...}

Then in your aspx.cs page you can now try to do:

string userId = MyFeedClass.getUserID();

Upvotes: 0

Carlos Landeras
Carlos Landeras

Reputation: 11063

I guess you are using Asp.NET?

You should create a new class inside your add_code folder. Move that namespace and the class inside the new created class

Then call it from your Feed.aspx.cs:

GetUser.MyFeedClass myfeed = new GetUser.MyFeedClass();
string result = myfeed.getUserID();

Upvotes: 5

Related Questions