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
.
Related Topics
How to Save/Download Pdf Embedded in Web Page Without a Pdf Filename
Copy Few Items from One List to Another List of Same Type in C#
How to Asynchronously Wait for X Seconds and Execute Something Then
How to Use Telegram API in C# to Send a Message
Removing Text Between 2 Strings
How to Pass Multiple Arguments in Processstartinfo
How to Refresh or Show Immediately in Datagridview After Inserting
How to Determine If .Net Core Is Installed
How to Delete a Read-Only File
Asp.Net Core API Post Parameter Is Always Null
How to Reference Assemblies Using Visual Studio Code
How to Return a File (Filecontentresult) in ASP.NET Webapi
How to Round Up the Time to the Nearest X Minutes
How to Get Number of Rows Using Sqldatareader in C#
Securely Store a Password in the Application in C#
Using Linq to Groupby and Sum Datatable
Linq to SQL Left Outer Join Using Lambda Syntax and Joining on 2 Columns (Composite Join Key)