user2124495
user2124495

Reputation: 59

Calling a method or piece of code from another file

A have a very large piece of code that I execute multiple times on my webpage, with only a slight difference each time (it uses information from a database. Sometimes it's within a foreach loop, using "row.nameofrow", and other times it's just a single record, using "query.nameofrow").

I'm pretty new to coding, and I'm wondering if there's a way to place that large selection of code into another file and call it each instance I use it (possibly using a parameter for whether it uses "row" or "query") instead of writing out the entire block of code each time. What kind of file would I need to use, and how would I call it?

Upvotes: 0

Views: 132

Answers (4)

Jastill
Jastill

Reputation: 656

Before tackling a website, do a really simple tutorial on classes.

Its much easier to start with simple console apps then asp sites.

starting here is a good place : http://www.csharp-station.com/tutorials/lesson1.aspx

spending a day or two to learn the basics will help you in the long run.

Upvotes: 2

e.beyer
e.beyer

Reputation: 173

You could create a public static class with a static method that takes in a parameter. You could then invoke this method call using var returnValue = StaticClassName.StaticMethodName(yourArgument);

Upvotes: 1

Z .
Z .

Reputation: 12837

Create a simple class and put the repeating code in a method. Easiest (but not recommended) would be to make the method static so you can call it without instantiating the class.

Upvotes: 1

griegs
griegs

Reputation: 22760

  1. Write a web service
  2. Write a dll that you can import into your other app and call

If you are totally within Visual Studio, add a project to your solution, write your code in a class within that project, expose it using "public", reference the project in your other project, instantiate the class and call the method.

This is kinda coding 101 and you need to go research the above. I can't describe it all without writing a massive post. Sorry

Upvotes: -3

Related Questions