// 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/