Rich
Rich

Reputation: 121

Passing a value from an assembly to an application in C#

If I have an assembly which contains a function that manipulates a string and then returns that string, and I then have a separate C# application which calls the function from the assembly...

How can I pass the manipulated string from the assembly into the application?

For example, If I have this assembly...

using System;

namespace test
{

    public class Class1
    {
        string inputString = "hello";
        string outputString;

        public static string Convert(ref inputString, ref outputString)
        {
            outputString = inputString.ToUpper();
            return outputString;
        }
    }
}

And I have this application which calls the Convert function within the assembly...

using System;
using test;

public class Class2
{
    public static void Main()
    {
        Class1.Convert();
    }
}

How can I get the returned outputString into the Class2 application? I can't reference it in the Main() function so how can I pass it in?

Upvotes: 2

Views: 1437

Answers (4)

Mike Mooney
Mike Mooney

Reputation: 11989

It looks like you want to pass in a valid and then return another value? Then get rid of the of properties on your Class1, they are not necessary. Just make the input string the parameter of the Convert function, and then return the output.

using System;

namespace test
{

    public class Class1
    {
        public static string Convert(string inputString)
        {
            string outputString = inputString.ToUpper();
            return outputString;
        }
    }
}

and:

using System;
using test;

public class Class2
{
    public static void Main()
    {
        string thisIsMyReturnedString = Class1.Convert("whatever the input value should be");
    }
}

Upvotes: 4

James Hull
James Hull

Reputation: 3689

If it's two different projects inside the same solution add a reference to the project. If it's two different solutions, add a reference to the dll.

Upvotes: 0

Louis Kottmann
Louis Kottmann

Reputation: 16618

You need to add reference to your dll in the main project:

In the main project, right click References --> Add Reference
Once that is done, you can use Convert like this:

string result = Class1.Convert(ref myInputString, ref myOutputString);

On a sidenote, your return is useless, you already are using the parameters as reference.
Your method's signature should be void:

public static void Convert(ref inputString, ref outputString)
{
    outputString = inputString.ToUpper();
}

Then you'd call it like this:

string input = "My InPuT";
string output = "";
Class1.Convert(ref input, ref output);
//Here, output = "MY INPUT"

Upvotes: 1

Adam V
Adam V

Reputation: 6356

EDIT:

Your Convert() function only needs to take in one parameter, since you're returning the other value. The parameter shouldn't be marked as ref unless you're going to modify it within your function.


Replace:

Class1.Convert();

with

string inputValue = "old value";
string returnValue = Class1.Convert(ref inputValue);

Upvotes: 2

Related Questions