Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# out parameter value passing

Tags:

c#

parameters

out

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?

like image 663
ACP Avatar asked Jun 09 '10 07:06

ACP


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


3 Answers

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.

like image 113
David M Avatar answered Oct 03 '22 09:10

David M


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.

like image 44
Petar Minchev Avatar answered Oct 01 '22 09:10

Petar Minchev


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.

like image 32
BladeWise Avatar answered Oct 01 '22 09:10

BladeWise