listitem如何投射或以其他方式

编程入门 行业动态 更新时间:2024-10-26 21:20:50
本文介绍了listitem如何投射或以其他方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

错误:无法将类型"System.Web.UI.WebControls.ListItem"转换为详细信息"

Error:Cannot convert type ''System.Web.UI.WebControls.ListItem'' to ''detail''

public class Detail { public int detailid; public string detailName; public override string ToString() { return detailName; } public Detail() { detailid = 0; } } Detail detail=null; detail = (Detail)ChBoxList.Items[i];

我尝试过的事情:

What I have tried:

public class Detail : ListItem { public string detailid; public string detailName; }

问题继续!

problems continue!

推荐答案

ListItem 不能是inherited,因为它是密封类 [ ^ ] 您可以使用Extension Methods 尝试类似的方法 ListItem cannot be inherited, since it is a Sealed Class [^] you can try something like this, using Extension Methods namespace WebApplication1 { public class Detail { public int detailid { get; set; } public string detailName { get; set; } } public static class DetailExtension { public static Detail ToDetail(this ListItem li) { Detail detail = new Detail(); if (null != li) { detail.detailid = int.Parse(li.Value); detail.detailName = li.Text; } return detail; } } public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) return; List<Detail> lst = new List<Detail>(); lst.Add(new Detail() { detailid = 1, detailName = "Detail 1" }); lst.Add(new Detail() { detailid = 2, detailName = "Detail 2" }); lst.Add(new Detail() { detailid = 3, detailName = "Detail 3" }); ChBoxList.DataTextField = "detailName"; ChBoxList.DataValueField = "detailid"; ChBoxList.DataSource = lst; ChBoxList.DataBind(); Detail item = ChBoxList.Items[1].ToDetail(); } } }

基本上,我已经在评论中回答了您的问题. 可以转换的想法非常怪异,这意味着总体上对编程有完全的误解,但也有纯粹的逻辑缺陷,这是非常基本的缺陷.试着想一想,.NET FCL类System.Web.UI.WebControls.ListItem如何可能知道"您的类型Detail甚至存在? :-) 这是您可以使用的: Essentially, I already answered your question in my comment to it. The idea that you can cast is extremely weird it means total misunderstanding of programming in general, but also pure logical flaw, very elementary one. Just try to think, how the .NET FCL class System.Web.UI.WebControls.ListItem could possibly "know" that your type Detail even exists? :-) This is what you can use: Detail detail = //... int value; if (int.Parse(ChBoxList.Items[i].Value, out value)) { DoSomethingWithDetail(detain, value); // ... }

在此代码示例中,我没有使用您对类Detai的定义,因为它的定义不够好.使用public字段是不好的,属性通常用于非私有数据成员.为什么一切都是public为什么不是internal?您是否真的将那些成员暴露给其他程序集?另外,请避免使用太短的名称,例如i.名称应为有效的英语单词,变量的名词和一些非方法成员,方法成员应为动词.

—SA

In this code sample, I did not use your definition of the class Detai, because it is not good enough. Using public fields is bad, properties are usually used for non-private data members. And why everything is public why not internal? Do you really expose those members to other assemblies. Also, avoid too short names like i. Names should be valid English words, nouns for variables and some non-method members, and method members should be verbs.

—SA

更多推荐

listitem如何投射或以其他方式

本文发布于:2023-10-31 17:21:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:或以   方式   listitem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!