Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am having string variable in c# asp.net in which i require that string variable in if condition. If condition satisfies then it should execute it whereas not satisfied then else statement should execute. If condition having one string variable and in else statement is having another string variable how to achieve this

What I have tried:

List<BusinessObjectClass> ObjListStrVar = new List<BusinessObjectClass>();
List<BusinessObjectClass> ObjListStrVar1 = new List<BusinessObjectClass>();
string strVar, strVar1, selected, rbVal;

if (strVar == true) // <-- I am getting error here saying cannot convert string to bool                 
   ObjListStrVar.Add(new BusinessObjectClass() { strVar = strVar });                                
else
   ObjListStrVar1.Add(new BusinessObjectClass() { strVar1 = strVar1});

Before this if statement i am having the value of strVar and strVar1. There is one radio button in which checking the checked condition of that radio button. If radio button checked or not that value is stored in a string variable another the value of radio button. The values of strVar and strVar1 are as follows:

if(selected == rbVal)
{
   if (strVar == "1")
       strVar = "1";
   else
       strVar += strVar.Split(',');
   ObjBusinessObjectClass.strVar = strVar;
}
else
{
   if (strVar1 == "1")
       strVar1 = "1";
   else
       strVar1 += strVar1.Split(',');
   ObjBusinessObjectClass.strVar1 = strVar1;
}
Posted
Updated 2-Jan-19 21:19pm
Comments
BillWoodruff 3-Jan-19 1:07am    
There is so much confusion in this code it suggests you need to stop coding and get a good book on C# and study the basics. Examine the definitions of methods like 'Split.

Think about why you would use a string to store a boolean value: does that make sense ?
Member 8583441 3-Jan-19 1:16am    
thanks for your suggestions sir please provide me a nice book to study
David_Wimbley 3-Jan-19 2:08am    
Pretty much a google search for any book on c# is probably a best bet. You can also follow tutorials/google for tutorials.

One of the things about getting into development is taking time to figure things out for yourself before having someone else do the leg work for you.

My suggestion for you would be to do a google search, pick a few books you think may be good and then come back and ask based on what you've found...which book others may find most beneficial.

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:
C#
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
C#
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:
C#
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:
C#
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:
C#
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!
 
Share this answer
 
Instead of using any strings you could directly use checked property of radiobutton like-
C#
if(radioButton1.Checked == true){
//Your code here...
}else{
//Your code here...
}

I would also suggest you to first get a hand on good book and familiarize yourself with basics. 😊
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900