returning a string and bool type from a member of a class

G

Guest

Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only return
one thing with the function. Thanks.

public bool checkdate(String s_date)
{
b_convert = true ;
DateTime dt_temp1 = Convert.ToDateTime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateTime(s_date);
}
catch (Exception ex)
{
errormsg = ex.Message;
b_convert = false;
}
return b_convert;
}
 
J

Jon Skeet [C# MVP]

Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only return
one thing with the function.

You'll need to use an out parameter:

public bool checkdate(string input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
...
}

Jon
 
P

Peter Duniho

Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only
return one thing with the function. Thanks.

You can use a parameter with the "out" keyword to allow additional
information to be return:

public bool checkdate(Strings s_date, out String s_error)
{
b_convert = true ;
s_error = "";

DateTime dt_temp1 = Convert.ToDateTime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateTime(s_date);
}
catch (Exception ex)
{
s_error = ex.Message;
b_convert = false;
}
return b_convert;
}

There are lots of other ways to do this, but the above is pretty much what
the "out" keyword is for.

Pete
 
G

Guest

Hi thanks for the response, do you use a return statement for the out
variable or only the bool ?
I am getting an error (the out parameter message must be assigned to before
control leaves the current method) Here is what I have
public bool checkdate(string input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
return b_convert;
...
}
 
G

Guest

I fixed the previous error, had not assigned null to the string message, just
wondering where I use the member how do I set that up to accept a string and
a bool
string stemp bool btemp = member (date); ?
thanks again
 
P

Peter Duniho

I fixed the previous error, had not assigned null to the string message,
just wondering where I use the member how do I set that up to accept a
string and a bool

Are you asking how to call the method? You call it the same way you'd
call any method with two string parameters, except that for the "out"
parameter you have to include the "out" keyword:

string stemp;
bool btemp = checkdate(date, out stemp);

On return, btemp will have the same value it did with the previous version
of the method, and the string stemp will contain the error message, if any.

Pete
 
G

Guest

You can use the OUT Parameter to return any message from inside a method to
outside the method. Try like the below,

bool DoSome(string strInput, out string strOutput)
{
strOutput = "Returned from Method";
return true;
}
 
J

Jon Skeet [C# MVP]

Paul said:
I fixed the previous error, had not assigned null to the string message, just
wondering where I use the member how do I set that up to accept a string and
a bool
string stemp bool btemp = member (date); ?

I suggest you read up on out/ref parameters in a book about C#. My own
page about parameter passing has some information, but I'd suggest
reading about it via a more dedicated resource.

See http://pobox.com/~skeet/csharp/parameters.html
 
C

Chris Dunaway

Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only return
one thing with the function. Thanks.

public bool checkdate(String s_date)
{
b_convert = true ;
DateTime dt_temp1 = Convert.ToDateTime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateTime(s_date);
}
catch (Exception ex)
{
errormsg = ex.Message;
b_convert = false;
}
return b_convert;
}

Why re-invent the wheel? This method already exists in the form of
DateTime.TryParse and DateTime.TryParseExact.

Chris
 
G

Guest

Paul said:
Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only return
one thing with the function. Thanks.

public bool checkdate(String s_date)
{
b_convert = true ;
DateTime dt_temp1 = Convert.ToDateTime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateTime(s_date);
}
catch (Exception ex)
{
errormsg = ex.Message;
b_convert = false;
}
return b_convert;
}

An alternative to the out parameter recommended by several you
could return a struct.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top