2

我对 C# 还是很陌生,如果对我的代码有任何帮助,我将不胜感激。

我正在创建一个用户个人资料页面,并且在“photo = (byte)user.Photo;”上收到错误“Nullable object must have a value” 在下面的代码中。我认为这是因为我声明了“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;
            }
        }
    }
4

1 回答 1

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;
        }
于 2012-09-12T11:31:57.500 回答