C# out parameter value passing

ACP picture ACP · Jun 9, 2010 · Viewed 16.6k times · Source

I am using contactsreader.dll to import my Gmail contacts. One of my method has the out parameter. I am doing this:

Gmail gm = new Gmail();
DataTable dt = new DataTable();
string strerr;
gm.GetContacts("[email protected]", "******", true, dt, strerr);
// It gives invalid arguments error..

And my Gmail class has

public void GetContacts(string strUserName, string strPassword,out bool boolIsOK,
out DataTable dtContatct, out string strError);

Am I passing the correct values for out parameters?

Answer

David M picture David M · Jun 9, 2010

You need to pass them as declared variables, with the out keyword:

bool isOk;
DataTable dtContact;
string strError;
gm.GetContacts("[email protected]", "******",
    out isOk, out dtContact, out strError);

In other words, you don't pass values to these parameters, they receive them on the way out. One way only.