Convert a string to bool

  • Thread starter Thread starter ReidarT
  • Start date Start date
R

ReidarT

I have a value from a field (int) in a table like
string strPlayerLoginStatus = dsPlayer2.Tables[0].Rows[0][1].ToString();

I like to convert the strPlayerLoginStatus from string to int to use it in
an if statement

How do I convert the variable?

regards

reidarT
 
I have a value from a field (int) in a table like
string strPlayerLoginStatus = dsPlayer2.Tables[0].Rows[0][1].ToString();

Why don't you get it as you want directly?

// if Rows contains an int
int intPlayerLoginStatus = (int)dsPlayer2.Tables[0].Rows[0][1];

// if Rows contains something similar to an int
int intPlayerLoginStatus = Convert.ToInt32(dsPlayer2.Tables[0].Rows[0][1]);
I like to convert the strPlayerLoginStatus from string to int to use it in
an if statement

How do I convert the variable?

Another way?

// if Rows contains a int as string
int intPlayerLoginStatus =
int.Parse(dsPlayer2.Tables[0].Rows[0][1].ToString());
 
Do you want the bool to be true if you have a record and false if you do
not?

If so... Give the method a return typ of bool and return it based on the
result.

public bool GetTheData()
{
if(the table has records...)
{
return true;
}
else
{
return false;
}
}

public void CallingMethod()
{
if(GetTheData)
{
do something...
}
else
{
do something else...
}
}

Hope this helps.
bobc
 
I like to convert the strPlayerLoginStatus from string to int to use it in
an if statement

You would need to know which int value is ok (say 1) and which is not ok.

if (some_value == 1)
{
// do something
}

Unless we are completely misunderstanding you,
you need to read a textbook on C#. You shouldn't
use this ng to learn the basics of C#, but to help
you solve problems after the learning stage.

If you have a good library in the neighbourhood
photocopy the first 100 pages of C# Design Patterns by James W. Cooper
ISBN 0-201-84453-2 Don't read the rest of the book. Don't buy the book.
It gives you a super brief and well written introduction into the basics of
C#.

Else buy / read
Learning C# O'Reilly ISBN 0-596-00376-5
 
try
{
int value = int.Parse( strPlayerLoginStatus );
}
catch (System.FormatException ex)
{
// handle exception
}
 
Zach said:
Unless we are completely misunderstanding you,
you need to read a textbook on C#. You shouldn't
use this ng to learn the basics of C#, but to help
you solve problems after the learning stage.

I disagree with you, Zach. The ng is open to all, regardless of experience.
If a question is too trivial for you, don't answer it.

To ReidarT
You are welcome here.
If you have a good library in the neighbourhood
photocopy the first 100 pages of C# Design Patterns by James W. Cooper
ISBN 0-201-84453-2 Don't read the rest of the book. Don't buy the book.

I've heard a lot of weird things before, but this is pretty weird. The book
has a few minor errors in the use of C#, specifically in the demonstration
of a single pattern. The rest of the book is above average, and the first
chapters are excellent (as you noted). If he or she doesn't know design
patterns, the OP may benefit from reading this book in its entirety
(although I usually recommend Shalloway's book for folks just learning DP).


--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Thanks, thats good to hear
reidarT
Nick Malik said:
I disagree with you, Zach. The ng is open to all, regardless of
experience. If a question is too trivial for you, don't answer it.

To ReidarT
You are welcome here.


I've heard a lot of weird things before, but this is pretty weird. The
book has a few minor errors in the use of C#, specifically in the
demonstration of a single pattern. The rest of the book is above average,
and the first chapters are excellent (as you noted). If he or she doesn't
know design patterns, the OP may benefit from reading this book in its
entirety (although I usually recommend Shalloway's book for folks just
learning DP).


--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Nick Malik said:

Re Cooper:
If he or she doesn't know design patterns, the OP
may benefit from reading this book in its entirety
I've heard a lot of weird things before

Yes, I get the impression that you have!

(1) There are better books around on the subject of design patterns
(2) The subject matter and the examples used are totally unsuited for
an *absolute* starter.

Re using the ng by an *absolute* starter:
First reading an introductory text will get the OP up to
speed faster. To that end I suggested two excellent texts,
one very concise but sufficient for starters, the other
more comprehensive.
 
Back
Top