Question

My goal is to pass myDictionary from function1 to function2 and print it from function2. I am a newbie so it is quite possible my syntax or format is incorrect.

// This is my main program
class Program
{
    static void Main(string[] args)
    {
        function1();
    }
}

// Create myDictionary in this function and then pass it to function2
static public void function1()
{
    Dictionary<string, string> myDictionary = new Dictionary<string, string>();
    myDictionary.Add("FirstName", "John");
    myDictionary.Add("LastName", "Walter");

    // passing myDictionary to another function
    function2(myDictionary);
}

// Passing myDictionary to this function
static public function2(myDictionary<string, string>)
{
    // print myDictionary from here 
}

No correct solution

OTHER TIPS

Declare you 2nd function like this:

//passing myDictionary to this function
static public void function2(Dictionary<string, string> myDictionary)
{
    foreach (var v in myDictionary)
        Console.WriteLine(string.Format("{0}: {1}", v.Key, v.Value));
}

Cheers

public static Dictionary<string, string> function2(Dictionary<string, string> myDictionary)
{
    // print myDictionary from here 
}

For each parameter, you need to specify the type, followed by the name of the parameter.

A method or function may have a list of parameters. That's the part between the parentheses after the method's name:

void FunctionName( /* parameters here */ )
{
}

A parameter always looks like this:

Type name

Where Type is the type of the value, and name is any legal identifier you want. If you want multiple parameters, separate them with comma's:

void MethodName(Type1 name1, Type2 name2)
{
}

Also note the void before the method name. This indicates that the method is expected to return no value. if you want your function to return a value, simply replace void by the type of value you want it to return.


In your case, Dictionary<string, string> is the type, and the name could be, for example, someDictionary. Note that it doesn't have to be myDictionary, but it can be.

static public void PrintDictionary(Dictionary<string, string> someDictionary)
{
    // print someDictionary from here 
}

And then you call it like you did:

PrintDictionary(myDictionary);

The rest of your code is correct. You'll only have to find out how to print your dictionary. For example:

static public void PrintDictionary(Dictionary<string, string> someDictionary)
{
    foreach(KeyValuePair<string, string> pair in someDictionary)
    {
        Console.Write(pair.Key);
        Console.Write(" => ");
        Console.WriteLine(pair.Value);
    }
}

Note that I changed the name function2 to PrintDictionary in my examples. A method or function name should be a meaningful name, and the C# convention is to start it with an upper-case letter.

You need not add a seperate variable you can call the function2 like below.

static public void function1()
{

    // passing myDictionary to another function
    function2(new Dictionary<string, string> { { "FirstName", "John" }, { "LastName", "Walter" } });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top