Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Code ??

Status
Not open for further replies.

need2progm

Programmer
Dec 13, 2002
92
US
I need this vb.net code to be in c#

If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then

Dim r As Label
r = e.Item.FindControl("Label2")
r.Text = &quot;<input type=radio name='myradiogroup' value=&quot; & e.Item.Cells(1).Text & &quot; id= e.item.c>&quot;

End If


I have .. with errors


//error can not implicitly convert to bool
if(e.Item.ItemType = ListItemType.AlternatingItem || e.Item.ItemType = ListItemType.Item)
{
System.Web.UI.WebControls.Label lblR = new System.Web.UI.WebControls.Label();

//Error can not implicitly convert control to a label control
lblR = e.Item.FindControl(&quot;Label2&quot;);

lblR.Text = &quot;<INPUT type='radio' name='radiogroup' value='&e.Item.Cells[1].Text'& id=e.item.c> &quot;;

}

Can anyone help me? Do you know of a tool on the web that I can download?


 
Lots of C# to VB tools, few VB to C# tools

Just do what the compiler says

(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)

double == !


System.Web.UI.WebControls.Label.control ?
Try to look in MSDN to see how it is done in C#. Most of the time its simular, so don't look to far.
 
Like Zinno says --

Hardest thing about making the switch is understanding that C# differentiates between assignment (single =) and comparison (double =).

Chip H.
 
I don't think that is the problem about (double =).
According to the error message .... that is about type convert.
And the FindControl method return &quot;Control&quot; type.
So, I think maybe you should write like
lblR = (Label)e.Item.FindControl(&quot;Label2&quot;);
to cast your Control back to a Label type.
 
VB is much more forgiving particularly as to resolving type. You need to be more careful with your parentheses as the expressions are evaluated and converted to boolean. Try

If ( (e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
{}

&quot;0952463671&quot; above is on track. In the statement, you are assigning a Control to a label control &quot;lblR&quot;.
 
I am still getting an error on the FindControl

Thank you 0952463671... I will try this when I get to work.
lblR = (Label)e.Item.FindControl(&quot;Label2&quot;);

Dinobrago.... I have gotten a clean compile on the (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)

unable to test yet .. would I still need the extra parentheses? I am assuming yes ..like you said it need to eval each expression. Looking forward getting to work today. I love this stuff :)
 
If it works, it works. Looking at it again (especially since you got a clean compile), the == expressions must have precedence and are evaluated to booleans which are then evaluated with the &quot;or&quot;.
 
// If it's an AlternatingItem type or it's an Item type
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) {

// Create a new label
// No need to initialize it with the 'new' keyword
// since we'll set it's value shortly
System.Web.UI.WebControls.Label r;

// Just in case it isn't a label control, don't drop your drawers
try {
// e.Item.FindControl will return any control
// cast it into a label control
r = ((Label)(e.Item.FindControl(&quot;Label2&quot;));
} catch {
// it wasn't a label
}

if ( r == null ) {
// Ok, we were wrong about &quot;Label2&quot;
// It either wasn't a label or it wasn't there

// Create me a new one
r = new System.Web.UI.WebControls.Label();
}

r.Text = &quot;<INPUT type='radio' name='radiogroup' value='&e.Item.Cells[1].Text'& id=e.item.c> &quot;;

}

/*
though the 'result as image' feature really bytes.
*/

robrich
 
need2progm -

What Dinobrago is saying is that while this simple example probably works for you (compiles clean, produces the expected outcome), eventually you'll run into a situation where you get a clean compile, but it *doesn't* act like you expected. You'll spend a lot of time troubleshooting what turns out to be an operator precendence problem that could have been prevented by liberal use of parentheses. So it's best to get into the habit of surrounding all your comparisons with parentheses to avoid any future heartache.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top