Calling a Function from Another Class in C#/.Net MVC App

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

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);

.net mvc - class method calling a method of caller class

You just need to pass an instance of your object ClientsService inside SomeOtherClass.

public SomeOtherClass(ClientsService service, ApplicationDbContext db) 
{
this.service = service;
this.db = db;
}

public void SomeMethod(int i)
{
this.service.getObjectTypeName();
}

I would suggest reading more on Dependency Injection to make the whole process of management dependencies more robust and easier to understand.

Calling method from another class from another file C#

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();

How to make method call another one in classes?

Because the Method2 is static, all you have to do is call like this:

public class AllMethods
{
public static void Method2()
{
// code here
}
}

class Caller
{
public static void Main(string[] args)
{
AllMethods.Method2();
}
}

If they are in different namespaces you will also need to add the namespace of AllMethods to caller.cs in a using statement.

If you wanted to call an instance method (non-static), you'd need an instance of the class to call the method on. For example:

public class MyClass
{
public void InstanceMethod()
{
// ...
}
}

public static void Main(string[] args)
{
var instance = new MyClass();
instance.InstanceMethod();
}

Update

As of C# 6, you can now also achieve this with using static directive to call static methods somewhat more gracefully, for example:

// AllMethods.cs
namespace Some.Namespace
{
public class AllMethods
{
public static void Method2()
{
// code here
}
}
}

// Caller.cs
using static Some.Namespace.AllMethods;

namespace Other.Namespace
{
class Caller
{
public static void Main(string[] args)
{
Method2(); // No need to mention AllMethods here
}
}
}

Further Reading

  • Static Classes and Static Class Members (C# Programming Guide)
  • Methods (C# Programming Guide)
  • using static directive (C# Reference)

Accessing a view's function from another view

Yes, this can be achieved. I tried it with the default MVC project in Visual Studio 2015.

Content of About view:

@{
ViewBag.Title = "About";
}

@functions
{
public static string DoStuff()
{
return "Content from About view.";
}
}

Content of Index view:

@{
ViewBag.Title = "Home Page";
}

@_Page_Views_Home_About_cshtml.DoStuff()

... (other stuff)

In Index view there is a red squiggly line below _Page_Views_Home_About_cshtml displaying :

The name _Page_Views_Home_About_cshtml does not exist in the current context

Despite the error message, the application builds successfully and I can see the message from the About view when navigating to /Home/Index.

View



Related Topics



Leave a reply



Submit