我还是C#的新手,对我的代码有任何帮助。
我正在创建一个用户个人资料页面,并在“photo =(byte)user.Photo”上收到错误“Nullable object必须有值”在以下代码中。我认为这是因为我声明“photo = 0;”如何为其添加值?
更新
这是整个方法
public static bool UserProfile(string username, out string userID, out string email, out byte photo)
{
using (MyDBContainer db = new MyDBContainer())
{
userID = "";
photo = 0;
email = "";
User user = (from u in db.Users
where u.UserID.Equals(username)
select u).FirstOrDefault();
if (user != null)
{
photo = (byte)user.Photo;
email = user.Email;
userID = user.UserID;
return true; // success!
}
else
{
return false;
}
}
}
答案 0 :(得分:0)
我假设你为这个犯错误...
if (user != null)
{
photo = (byte)user.Photo;
email = user.Email;
userID = user.UserID;
return true; // success!
}
else
{
return false;
}
如果是,则只需将其替换为......
if (user != null)
{
photo = user.Photo== null ? null : (byte)user.Photo;
email = user.Email;
userID = user.UserID;
return true; // success!
}
else
{
return false;
}