Dennis
Dennis

Reputation: 393

How to call a method/function from another class file

I tried to make this function (and others) and put it in a separate class file in my project that's under "/Helpers/UploadFiles.cs"

namespace Artikelhantering.Helpers
{
public class UploadFiles
{

    private void EnsureDirectoriesExist(string SKU)
    {

        // if the directory doesn't exist - create it. 
        if (!System.IO.Directory.Exists("//servername/wwwroot/prodimg/" + SKU))
        {
            System.IO.Directory.CreateDirectory("//servername/wwwroot/prodimg/" + SKU);
        }

    }
}

Then in the controller I added using Artikelhantering.Helpers;, it's also added to the namespace section of the web.config file and also to global.asa.cx.

Then I figured I could call it from an ActionResult in my controller like this

[ChildActionOnly]
public ActionResult _EnumerateFolder(string SKU)
        {
            // create directory if it does not exist
            EnsureDirectoriesExist(SKU);

            ViewBag.SKU = SKU;
            var folder = Directory.EnumerateFiles("//servername/wwwroot/prodimg/" + SKU);
            return View(folder);
        }

But all I get is:

Error 2 The name 'EnsureDirectoriesExist' does not exist in the current context

Tried calling it by writing it as UploadFiles.EnsureDirectoriesExist(); but that doesn't work either. How am I supposed to call these methods without having them all in the same file? I would like to organize this better.

Upvotes: 0

Views: 41914

Answers (5)

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

  1. First thing that is not correct here is a method accessibility levels. In order to invoke method from outside of the class body it should be public.
  2. Also, way that you are invoking this method is also incorrect. To do it in desired way you should make your class static to avoid creating an instance of a class to invoke method.

So:

public static class Helper 
{
      public static void EnsureDirectoriesExist(string SKU)
      {
           ...
      }
}

Upvotes: 1

roybalderama
roybalderama

Reputation: 1640

First, change the access modifier of EnsureDirectoriesExist to public then try to change your ActionResult _EnumerateFolder method with the code below:

public ActionResult _EnumerateFolder(string SKU)
{
    // create directory if it does not exist
    new UploadFiles.EnsureDirectoriesExist(SKU);

    ViewBag.SKU = SKU;
    var folder = Directory.EnumerateFiles("//servername/wwwroot/prodimg/" + SKU);
    return View(folder);
}

Upvotes: 1

Don Angelo Annoni
Don Angelo Annoni

Reputation: 380

make your directory method public and static. Then you can call it something like this

Artikelhantering.Helpers::UploadFiles.EnsureDirectoriesExist(SKU);

If you can't change the signature... you can make a public wrapper method and call it the same way. If you cannot make the method static, then you should create first an instance of your class and finally call the new public wrapper method.

Upvotes: 0

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

Mark your class as static then try this:

public static class UploadFiles
{

    public void EnsureDirectoriesExist(string SKU)
    {
        //your code
    }
}

Then:

public ActionResult _EnumerateFolder(string SKU)
{
        UploadFiles.EnsureDirectoriesExist(SKU);

        //your code
}

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

The method is private. You can not access private members of other classes.

Also some other problems here:

  1. The method you wrote is an instance method, so you need to have an instance of the class to call the method.
  2. If you want to call it using UploadFiles.EnsureDirectoryExists(), you need to make it a class method (static).
  3. I'm not sure whether you can create a new directory the way you try to do it. If you are trying to create the directory on the same machine that this code is running on, use local file names.

Sample code for 1):

UploadFiles uf = new UploadFiles();
uf.EnsureDirectoryExists();

Sample code for 2):

public class UploadFiles
{
    public static void EnsureDirectoriesExist(string SKU)
    {
        // if the directory doesn't exist - create it. 
        if (!System.IO.Directory.Exists("//servername/wwwroot/prodimg/" + SKU))
        {
            System.IO.Directory.CreateDirectory("//servername/wwwroot/prodimg/" + SKU);
        }
    }
}

I furthermore suggest that you google for a C# tutorial that provides you with information on what classes are and how they can be used.

Upvotes: 5

Related Questions