C# is what is called a
strongly typed language: this means it will not automatically try to convert any data type into another (with a few exceptions where the change will not lose any data, such as
int
to
double
for example). This is one of it's strengths, because it means that potential problems can be caught at compile time, not run time - which means that testing becomes much simpler.
Where you want to convert a string value to a different type, you have to either cast or parse it. The first is saying "Just make it so" and is only useful when there is an immediate similarity between the two datatypes: converting a
double
to an
int
for example:
double d = 1234.56789;
int i = (int) d;
This discards the fractional part of d - .56789 - and assigns the integral part to i - it;s the equivalent of saying
double d = 1234.56789;
int i = 1234;
But that only works between closely related types, where a cast operator has been defined. There is no such operator for
string
to
int
, or
string
to
double
for example.
For those, you need to Parse the string, which means to look at the individual characters that make up the string and generate a number from them. (There are good reasons for this, including that the string may not contain a number in base 10, so it has to be a specific separate function to ensure that the number is processed correctly.)
And what you are doing doesn't make any sense at all!
Firstly, strVar doesn't have any content (because you have not assigned anything to it), so it will contains the default value for a class:
null
which can't be parsed at all! And
bool
contains only
true
or
false
, neither of which are strings, so there is no way at all to directly compare the two: it's like trying to compare an apple with a Ferrari in terms of bra size: there is no relationship between the two items that means that can be directly compared at all!
string
values are compared with other
string
values, and that comparison can generate a bool:
if (myString == "Hello")
{
...
}
And when it comes to RadioButtons, you can't compare them directly either, except with other RadioButtons to establish which button you are talking about:
foreach (Control c in Controls)
{
RadioButton rb = c as RadioButton;
if (rb != null)
{
if (rb == myRadioButton)
{
...
}
}
}
To find out if they are checked, you need to use a specific property of the button:
if (myRadioButton.Checked)
{
...
}
I think you are trying to jump too far ahead of yourself - this is all very basic stuff and it needs to be well understood before you start working with Generics in any way!