A bug caused by a const

Source: Internet
Author: User

Today, there is a problem in the daytime, hidden deep, here to record.

The original intention is simple, is to change the link name of a database, the link name is placed in the data Layer Public const string 17uwangdb= "17uwangdian", that is, under the DataAccess project, Then I servcie layer Project also use this link name to make, String db=databasefactory.getdatabase (DBHELPER.17UWANGDB); now I have changed the value of this variable name because only the data layer has changed Then compile the data-layer DLL, self-test always prompt not to find the corresponding link name, and then directly compile the solution, OK, no problem. And then because I only moved the data layer to the data layer of this DLL to the line, and then run, Yellow Pages, baffled its solution. Then suddenly think of the const modifier is static constant that is, after the compilation is determined, that is, the service DLL on the line to use the DB variable or I modified the value before, then I put the local compiled service DLL is also uploaded to the line on the line, sure enough OK.

Now let's analyze the problems that arise here.

The problem is that it appears on the const modifier, and the const-modified 17uwangDB is a static constant, and the code is replaced with a value of 17uwangdian after it has been compiled in place of that variable.

Look directly at the code, in order to see the difference added readonly and no modifiers to do the comparison:

DataAccess Layer Declaration Variable Dbblog

namespace simple.dataaccess{public   class Dbhepler   {public       const string CONSTDB = "17uwangdian";       public static readonly String readonlydb = "17uwangdian";}   }

  

Service Layer Call Dbhepler.dbblog variable

namespace simple.service{public    class Example    {public        void GetInfo ()        {            int constl = DbHepler.constDB.Length;            int readonlyl = DbHepler.readonlyDB.Length;}}}    

Anti-compilation See source code

namespace Simple.service{public class example{public void GetInfo () {int length = "17uwangdian". Length;int length2 = DbHepler.readonlyDB.Length;}}}

Here you can see that the const-modified CONSTDB variable has been replaced with the original defined value after compilation, and Readonlydb has not changed, READONLYDB will replace the value when the code executes here.

Back to the top of the problem, I modified the dataaccess layer of the variable value, but did not recompile the service layer, then the SERVCE layer to use the value is still the original value, so it caused a problem.

 

A bug caused by a const

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.