ACP
ACP

Reputation: 35264

C# out parameter value passing

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?

Upvotes: 5

Views: 19066

Answers (4)

BladeWise
BladeWise

Reputation: 1013

Since the definition of your function

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

requires that you pass some out parameters, you need to respect the method signature when invoking it

gm.GetContacts("<username>", "<password>", out boolIsOK, out dtContatct, out strError);

Note that out parameters are just placeholders, so you don't need to provide a value before passing them to the method. You can find more information about out parameters on the MSDN website.

Upvotes: 1

ChrisBD
ChrisBD

Reputation: 9209

I would suggest that you pass a bool variable instead of a literal value and place the out keyword before them.

bool boolIsOK = true;
gm.GetContacts("[email protected]", "******", out boolIsOK, out dt, out strerr)

Upvotes: 0

Petar Minchev
Petar Minchev

Reputation: 47403

You have to put "out" when calling the method - gm.GetContacts("[email protected]", "******", out yourOK, out dt, out strerr);

And by the way, you don't have to do DataTable dt = new DataTable(); before calling. The idea is that the GetContacts method will initialize your out variables.

Link to MSDN tutorial.

Upvotes: 3

David M
David M

Reputation: 72930

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.

Upvotes: 7

Related Questions