Cruze
Cruze

Reputation: 3

Use a function or method with variables in them in another class? C#

Im trying to use a method or function from another class, This is how my code looks from the class i want to use the functions from.

public class Crypting
{
    internal static void EncryptFile(string inputFile, string outputFile)
    {

     // My code is in here
    }

So I try to use this function in my main form like this.

 private void primeButton14_Click(object sender, EventArgs e)
    {
      Crypting.EncryptFile();
    }

It gets red marked and says "No overload for method 'EncryptFile' takes 0 arguments"

When I remove string inputFile, string outputFile It does not get red marked however I need those 2 arguments for that function to work.

How do I use this function in my main form? Thanks in advance. :)

Upvotes: 0

Views: 43

Answers (3)

jjczopek
jjczopek

Reputation: 3379

To add what @roryap said here: https://stackoverflow.com/a/34004763/327079 - while defining your method, you can also define default values for the parameters on the method:

public class Crypting
{
    internal static void EncryptFile(string inputFile = @"c:\input.txt", string outputFile = @"c:\output.txt")
    {
        Console.WriteLine("inputFile: {0} outputFile: {1}", inputFile, outputFile);
    }
}

Then if you use it, you can get following results:

Crypting.EncryptFile(); -> inputFile: c:\input.txt outputFile: c:\output.txt
Crypting.EncryptFile(@"c:\otherInputFile.txt"); -> inputFile: c:\otherInputFile.txt outputFile: c:\output.txt
Crypting.EncryptFile(@"c:\otherInputFile.txt", @"c:\otherOutputFile.txt"); -> inputFile: c:\otherInputFile.txt outputFile: c:\otherOutputFile.txt

It's hard to tell whether it will work for your actual case, but that's what language offers you.

Upvotes: 0

Aman Sharma
Aman Sharma

Reputation: 15

your function EncryptFile needs two parameters to work on, as you specified i.e. input file and output file location. you need to pass them while calling the method. like Crypting.EncryptFile("somestring", "somestring");

Upvotes: 0

rory.ap
rory.ap

Reputation: 35318

The message is pretty clear: you have to provide arguments:

EncryptFile(string inputFile, string outputFile) // see the two arguments, inputFile and outputFile?

Call it e.g. like this:

Crypting.EncryptFile(@"c:\myinputfile.txt", @"c:\myoutputfile.txt");

Upvotes: 1

Related Questions