Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

when I am trying to convert the below line of code from vb.net to c# showing an error.

VB.NET Code: lt.Text = IIf(CType(dg.Controls(i), CheckBox).Checked, True, False)

Converted c# code:
lt.Text == (((CheckBox)dg.Controls(i)).Checked ? true : false)

Please help me for resolving this.
Posted
Comments
CPallini 8-Oct-15 8:02am    
What are you trying to do? Do you really want to test for equality It.Text with the expression on the right side of the == operator?
Abdul Samad KP 8-Oct-15 8:03am    
use Controls[i] instead of Controls(i)
Krunal Rohit 8-Oct-15 8:04am    
[i] instead of (i), rest of it looks good.
Can you share the error details ?

-KR
Baroor 8-Oct-15 8:44am    
when Iam tring this: (((CheckBox)dg.Controls[i]).Checked ? true : false);

Showing error like, cannot imppicityly convert type bool to string.
Abdul Samad KP 8-Oct-15 9:40am    
You need to convert it to string
((CheckBox)dg.Controls[i]).Checked.ToString();
this will work.

You don't need a conditional operator (?:) operator because ((CheckBox)dg.Controls[i]).Checked return true or false only.

Assuming that you want to assign, not compare. Also assuming that you mean to perform an array index operation:

C#
lt.Text = ((CheckBox)dg.Controls[i]).Checked


Or, if the variable dg is statically typed:

C#
lt.Text = dg.Controls[i].Checked
 
Share this answer
 
v3
Try it like this :

C#
lt.Text = Convert.ToString(((CheckBox)dg.Controls(i)).Checked ? true : false);
 
Share this answer
 
Comments
Maciej Los 9-Oct-15 1:57am    
Ralf, have a look at your solution. In my opinion it it completely wrong. Think of it: Checked returns true/false, so you don't need to use lambda expression. Secondly: why - ...!!! - do you convert it to string?
Ralf Meier 9-Oct-15 3:36am    
Hello Maciej,
Thanks for your reply.
I don't checked if this code makes sense or not - I only completed the code from the inquirer that the code would be accepted by the development-system.
I think also that Solution 1 is much better.

2nd: the control return a boolean and the assignment goes to a text-Property (lt.text) which is a string - so in my opinion it must be converted to string.
Baroor 9-Oct-15 4:01am    
@Ralf, thanks, above code works for me.
Ralf Meier 9-Oct-15 6:37am    
Thanks for your reply.
Nevertheless - I would prefer the first Suggestion from Solution 1 - combined with the Convert.ToString-method ...

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