gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:52:00 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com