简体   繁体   中英

What is equalivent C# code from this VB.Net code?

I have the following snippet in VB.Net (web application):

Dim lnkTemp as HyperLink = dvFileInfo.FindControl("lnkPlan")

dfFileInfo is a DetailsView control from System.Web.UI.WebControls

When I try to convert this to C# like this:

HyperLink lnkTemp = dvFileInfo.FindControl("lnkPlan");

I get an error after the "=" that states:

Cannot implicitly convert type 'System.Web.UI.Control' to System.Web.UI.WebControls.HyperLink'. An explicit conversion exists (are you missing a cast?)

What do I need to do to correct this issue?

Thanks!

您只需要添加一个显式转换:

HyperLink lnkTemp = (HyperLink)dvFileInfo.FindControl("lnkPlan");

Just cast it:

HyperLink lnkTemp = (HyperLink)dvFileInfo.FindControl("lnkPlan");

Or be a little safer:

HyperLink lnkTemp = dvFileInfo.FindControl("lnkPlan") as HyperLink;
// check for lnkTemp == null

The writer of the VB code should probably have been doing the following for clarity:

Dim lnkTemp As HyperLink = DirectCast(dvFileInfo.FindControl("lnkPlan"), HyperLink)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM