When exactly is a constant better than a variable?
99.9% of the time, neither do I.PS. I still don't see the need for constants, compared to variables.
So you can declare as many as you like. Once they have been set can you change them?Depends on your level of caution and/or confidence in your typing. For instance, in some of my hobbyist writing I have fantasy novels. In them I am doing some totally unnecessary - but fun - computations of lunar orbits for a fantasy world with multiple moons of differing periods. In that computation I often need the value of PI. If I declare a global value of
PUBLIC CONST PI AS DOUBLE = 3.141592653589793
it is a lot easier to type 2 letters rather than 16 digits and a decimal point.
If you needed Euler's constant or Avogadro's number or Planck's constant or the speed of light, would you rather type a short name or the long numbers?
It is called a Constant for a reason, that is the whole purpose. A constant is a safety net so that you our someone else does not try to reassign a value to it. If they do the compiler will throw an error at compile time. So the question is not can you use a variable instead, the question is why is a constant better than a variable for a non changing value.Once they have been set can you change them?
So you can declare as many as you like. Once they have been set can you change them?
Const ONE As String = "1" ' 1 Const ELEVEN As String = "1" & ONE ' 11 Const APP_PATH As String = CurrentProject.Path ' Fails since it gets its value from a volatile function
?vbGreeninto the immediate window and it displays the value of 65280. Now try to assign a different number to it and it will fail for good reason (Assignment to constant not permitted). How would you like it if someone's code changed all the values of those color constants around?
Devolving or otherwise, I replaced the constant with a public variable.No - but if they are truly constant, you SHOULD NOT change them. And as we have seen in passing, if you attempt to use a constant incorrectly, you get warned. This is beginning to devolve into a similar thread as the "language has strong typing"/"language has weak typing" debate.
Like any other tool in the tool box, a constant has a purpose and works pretty well to that end. Admittedly, the tools in your tool box do no good if you never need them. But if you ever do need to protectedly declare a constant, you can.
Little Access Program? This little ERP Program, plans and runs the entire operations for a Chemical Plant, manages all Lab Samples, Project Manages their NCRs, and schedules rail cars and trailers arrivals and departures, We have a few hundred public variables, and no constants. And after 6 years of all this we never had single incident that you describe.You may say that you are never going to mistakenly try to reassign a constant, but little Access programs are not the yardstick for designing a language. You could have a program with 100s of code modules and many people working on portions.
Dude, lighten up. Sorry if you took it personally, but I was making a general statement about Access coding and not a specific comment on your application. I have no idea on the complexity and quality of your code, but it sounds like you put in a lot of work and should be proud. In general Access is a Rapid Development Application and the code is small, that was my point. If you are using it to run a Chemical Plant, that is pretty impressive and definitely pushing the limits of Access. As for winning friends on this site, that is not really my concern. However, after thousands of post on this site and others I have many people who follow me, many appreciative comments, and many people that PM me looking for assistance. So yeah I think I figured out how to make Access friends and "influence" the Access community. You seem very defensive about any even slight criticism or disagreement.Little Access Program? This little ERP Program, plans and runs the entire operations for a Chemical Plant, manages all Lab Samples, Project Manages their NCRs, and schedules rail cars and trailers arrivals and departures, We have a few hundred public variables, and no constants. And after 6 years of all this we never had single incident that you describe.
Have you ever thought about reading How to Win Friends and Influencing People?