c# 为什么这一行需要强制转换?

标签 c# casting

// A single blog entry
public class BlogEntry
{
    public string ID;
    public string Title;
    public string Entry;
    public DateTime Date;
    public int UserID;

    // Load an entry based on ID
    public BlogEntry(int EntryID)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var q = (from Blog in db.tblBlogEntries where Blog.ID == EntryID select Blog).Single();

        // Fill data
        this.ID = EntryID;
        this.Title = q.title;
        this.Entry = q.entry;
        this.Date = (DateTime)q.date;
        this.UserID = (int)q.userID;
    }
}

this.ID = EntryID 抛出错误:

Cannot implicity turn type 'string' to 'int'

我很困惑,因为它们都被定义为整数,非常明确!

最佳答案

在您的类定义中,ID 被定义为字符串

 public string ID;

并且您正在尝试分配一个 int。 这就是为什么。

关于c# 为什么这一行需要强制转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5210882/

相关文章:

c# - 让 gtk# 应用程序在 Windows 上运行的最简单方法

c# - 更改 iTextSharp 的默认桌面保存位置

c++ - 在这种情况下,qobject_cast 不应该默默地失败吗?

c++ - 将 Boost FileSystem3 迭代器转换为 const char*

java - 如果没有强制转换,将通用数据库方法封装在 Java 父类(super class)中然后从子类调用是行不通的。帮助

c# - 在没有安装Word的情况下以编程方式将DOCX转换为PDF?

c# - 如何以编程方式分析以了解两个表之间存在多对多关系?

c# - 如何从原始心电图数据写入 DICOM 文件

c - malloc 中是否需要类型转换?

java - 这个 Actor 阵容实际上是如何运作的?