• static 使用要注意的地方


      

     protected static string headimg = string.Empty;
    

    这里用到 static ,下面如果这样写
       object himg = DBUtility.DbHelperSQL.GetSingle(hstr);
    
            if (himg != null)
             {
                 headimg = "http://www.16njl.com" + Convert.ToString(himg);
             }
    给headimg 赋值,存在则赋值,不存在数据 则 没处理 headimg
    这样会出现错误 , 当你 不重新给赋值 headimg 赋值时,headimg 的值会一直保存下去, 即使 重新换个 账号 登录, 这个值只要没有被重新赋值 就一直存在(可能有时间限制,但时间很长), 当下一个 账号 没有 改变 headimg 的值时, 就会使用 上一个账户 存在的 headimg ,从而出现错误。

      protected static string headimg = string.Empty;
            protected void Page_Load(object sender, EventArgs e)
            {
    }
    这种方式的 static 不会因为 换了账户 headimg==string.Empty; headimg 的值不会变,除非在函数内重新赋值。

    所以一定要写成这种方式 :
      object himg = DBUtility.DbHelperSQL.GetSingle(hstr);
                    if (himg != null)
                    {
                        headimg = "http://www.16njl.com" + Convert.ToString(himg);
                    }
                    else
                    {
                        headimg = "";  // 记得重新赋值
                    }

    或者 去掉 static .
  • 相关阅读:
    Converting PDF to Text in C#
    Working with PDF files in C# using PdfBox and IKVM
    Visualize Code with Visual Studio
    Azure Machine Learning
    Building Forms with PowerShell – Part 1 (The Form)
    ML.NET is an open source and cross-platform machine learning framework
    Microsoft Visual Studio Tools for AI
    Debugging Beyond Visual Studio – WinDbg
    Platform.Uno介绍
    Hawk-数据抓取工具
  • 原文地址:https://www.cnblogs.com/awake-insist/p/5182185.html
Copyright © 2020-2023  润新知