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("chendur.pandiya@gmail.com", "******", 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?
You need to pass them as declared variables, with the out
keyword:
bool isOk;
DataTable dtContact;
string strError;
gm.GetContacts("chendur.pandiya@gmail.com", "******",
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.
You have to put "out" when calling the method - gm.GetContacts("chendur.pandiya@gmail.com", "******", 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.
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.
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("chendur.pandiya@gmail.com", "******", out boolIsOK, out dt, out strerr)
精彩评论