Reputation: 11
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
}
Upvotes: 0
Views: 3901
Reputation: 6006
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" } });
}
Upvotes: 0
Reputation: 50346
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.
Upvotes: 0
Reputation: 5380
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
Upvotes: 1
Reputation: 161801
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.
Upvotes: 0