I must be blind; diy constants?

Discussion in 'Questions (Windows Mobile)' started by enonod, Jul 11, 2009.

  1. enonod

    enonod Well-Known Member Licensed User

    I cannot find how to create a constant and be sure I am not creating a variable.
    I have searched threads but not found, and the Help file seems only to show pre-defined constants.
    Help please?
     
  2. Cableguy

    Cableguy Expert Licensed User

    The way I see it, in b4ppc you cannot create a cnstant as such, but you can use a variable as a placeholder for a value, and if never changed, it becomes a constant...

    ie: if we place this code under globals:
    myconst="whateverIwant"

    myconst will be available troughout all the module, keeping the same value throughout the module.
     
  3. enonod

    enonod Well-Known Member Licensed User

    I thank you for that Cableguy, but my issue is trying not to use variables because they consumes memory. I prefer to use constants in my code to avoid hundreds of changes later if I decide to alter a value. I realise that using a variable achieves that admirably, but with many such variables/constants goes memory use.
    Constants use none.
     
  4. agraham

    agraham Expert Licensed User

    Cableguy is correct. Use a global variable. There are oops! is no such thing as a constant in Basic4ppc.
     
    Last edited: Jul 11, 2009
  5. enonod

    enonod Well-Known Member Licensed User

    I wasn't disputing Cableguy. Can you say how expensive it is in memory to use all these global variables, for integer constants? After all a constant is simply a word substitution.
     
  6. agraham

    agraham Expert Licensed User

    It doesn't really matter as there is no alternative and in the IDE and legacy compiler, which are interpreted, a named constant will be treated much the same as a variable anyway.

    For an optimised compiled application he amount of memory used will depend upon how smart the C# compiler is when it generates the IL (Intermediate Language) code and what optimisations the JIT (Just In Time) compiler does at runtime when it generates the native code. In Basic4ppc variable values are held as strings and converted when required so the amount of storage will depend upon the length of the string. Additionally the C# compiler "pools" literal strings that are the same so there is only one instance of every unique string (initially!) in an application so that variables that are initialised with the same value will all reference a single instance of a string.

    I'd stop worrying about memory. Even a small 50 x 50 Bitmap will take up more memory than a couple of hundred string variables.
     
  7. enonod

    enonod Well-Known Member Licensed User

    I do hear what is being said and I am not arguing; I was obviously under a misapprehension that constants were substituted with their values by a pre-processor when the source code is initially parsed, before anything is compiled or anything else. I mean, as a sort of service to the coder, which basically would be nothing to do with any particular language or compiler.
    i.e. it might have been provided by anybody as a piece of code that is run on a source and simply does a substitution. Its purpose simply being to aid the coder when he changes his/her mind about a number that has been used repeatedly.
     
  8. agraham

    agraham Expert Licensed User

    There are constants in .NET but Basic4ppc doesn't implement them. The C# compiler does do constant substitution but also emits the constants' name and value as metadata in the assembly (exe or dll) so that a constant defined in an pre-compiled assembly, like a library, is available to an application that is compiled to use it. However constants, because of compile time substitution, can cause cross-versioning problems and are best avoided when they might change - which is often the reason for using them in first place as they make change convenient. So MaxInt = 32767 is a good candidate for a true constant but MilesPerGallon = 32 is not. I personally have never used a constant in .NET code and probably never will though I do uppercase name such variables in old C style.

    The .NET architecture is significantly different to the older "multiple pass compilation over source code direct to final native code execution image (perhaps via a separate assembler)" so you can't map previous experience of "how stuff works" onto .NET and the CLR.
     
  9. enonod

    enonod Well-Known Member Licensed User

    Thank you for putting that to bed.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice