473,787 Members | 2,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(Strin g s_date)
{
b_convert = true ;
DateTime dt_temp1 = Convert.ToDateT ime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateT ime(s_date);
}
catch (Exception ex)
{
errormsg = ex.Message;
b_convert = false;
}
return b_convert;
}
--
Paul G
Software engineer.
May 11 '07 #1
11 1986
On May 11, 4:51 pm, Paul <P...@discussio ns.microsoft.co mwrote:
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(strin g input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
...
}

Jon

May 11 '07 #2
On Fri, 11 May 2007 08:51:02 -0700, Paul <Pa**@discussio ns.microsoft.co m
wrote:
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(Strin gs s_date, out String s_error)
{
b_convert = true ;
s_error = "";

DateTime dt_temp1 = Convert.ToDateT ime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateT ime(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
May 11 '07 #3
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(strin g input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
return b_convert;
...
}

--
Paul G
Software engineer.
"Jon Skeet [C# MVP]" wrote:
On May 11, 4:51 pm, Paul <P...@discussio ns.microsoft.co mwrote:
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(strin g input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
...
}

Jon

May 11 '07 #4
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
--
Paul G
Software engineer.
"Jon Skeet [C# MVP]" wrote:
On May 11, 4:51 pm, Paul <P...@discussio ns.microsoft.co mwrote:
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(strin g input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
...
}

Jon

May 11 '07 #5
On Fri, 11 May 2007 09:29:02 -0700, Paul <Pa**@discussio ns.microsoft.co m
wrote:
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
May 11 '07 #6
got it working, thanks!
--
Paul G
Software engineer.
"Peter Duniho" wrote:
On Fri, 11 May 2007 08:51:02 -0700, Paul <Pa**@discussio ns.microsoft.co m>
wrote:
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(Strin gs s_date, out String s_error)
{
b_convert = true ;
s_error = "";

DateTime dt_temp1 = Convert.ToDateT ime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateT ime(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
May 11 '07 #7
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;
}
--
Every thing is perfect, as long as you share!!!

Don''t forget to rate the post
"Paul" wrote:
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(Strin g s_date)
{
b_convert = true ;
DateTime dt_temp1 = Convert.ToDateT ime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateT ime(s_date);
}
catch (Exception ex)
{
errormsg = ex.Message;
b_convert = false;
}
return b_convert;
}
--
Paul G
Software engineer.
May 11 '07 #8

If that is an actual method (and not just a demo), then you need to read:

http://blogs.msdn.com/kcwalina/archi...16/396787.aspx

Esp the
TryParse Pattern
section
Aka, your function breaks about 2 or 3 of the "rules".


"Paul" <Pa**@discussio ns.microsoft.co mwrote in message
news:29******** *************** ***********@mic rosoft.com...
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(Strin g s_date)
{
b_convert = true ;
DateTime dt_temp1 = Convert.ToDateT ime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateT ime(s_date);
}
catch (Exception ex)
{
errormsg = ex.Message;
b_convert = false;
}
return b_convert;
}
--
Paul G
Software engineer.

May 11 '07 #9
Paul <Pa**@discussio ns.microsoft.co mwrote:
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

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
20375
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But , for example, when the file doesnt exists, i should not return any reference to a bad constructed object, so i need something as a NULL reference object. I suppose i could return a pointer instead, but i have some code written with references which...
3
2613
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have similar behavior. My class looks like this: #ifndef long_int_H #define long_int_H #include <string> using namespace std; typedef valarray<complex<double> > VCD;
5
3108
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
3
2865
by: Christian Christmann | last post by:
Hi, I'm working on my first STL program. My class BitSet has three private attributes size, curbit and the STL bit_vector data which contains some bits. Here is the code for the constructors: BitSet::BitSet() :last(-1)
4
2402
by: cpisz | last post by:
At least that is what I think I want to do. What is the proper way for me to return multiple data member objects from an accessor method in my class while ensuring the data does not get changed, but allowing iteration through the data? I tryed returning a const vector but the compiler does not like the idea of my using an iterator to look through the vector down the road...I assume because an iterator acts like a pointer and allows you...
2
8523
by: Philippe F. Bertrand | last post by:
Summary - What should a C# bool map to in C++? Are return types different? I have lots of methods declared with bool parameters and bool return type which appear to work without any problems but one method in particular seems to always return true. The method is declared in C# as private static extern
5
19600
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
1
5554
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two other properties... bool error; string lastError; My whole class looks like this... using System;
6
2934
by: shapper | last post by:
Hello, I have a method: Roles(CultureInfo culture, RoleType? type, bool? open, bool? beginWithEmpty, string userRoles) How can I pass an empty string as userRoles? I tried: Roles(Thread.CurrentThread.CurrentCulture, RoleType.Administrator, false, true, new string)
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.