Results 1 to 8 of 8

Thread: constant

  1. #1

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    constant

    How do I create a constant in a vb.net class?

    All i want to do is something like:

    Public Const strPath as String = "\\machine\blah\foo"

    Is that right ???

    I want to use strPath in several functions and dont want to keep hardcoding that path name.

    Thanks

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    That syntax is correct, as far as I can tell. To be honest, I dont quite get the advantage of constants over just a plain variable, especially if it's exposed as a property. I guess it makes sense for something like PI, which will never change, but paths well could change.

  3. #3

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by Mike Hildner
    That syntax is correct, as far as I can tell. To be honest, I dont quite get the advantage of constants over just a plain variable, especially if it's exposed as a property. I guess it makes sense for something like PI, which will never change, but paths well could change.
    Exactly...so if they do change why should i recode them in every place rather than one constant ???

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Something like this could go into the web.config file, so that you can keep changing it without having to recompile each time.

  5. #5

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by mendhak
    Something like this could go into the web.config file, so that you can keep changing it without having to recompile each time.
    Hmm...

    Where in the web config...
    can you provide examples....

    As well as how to read it back in ??

    Is it much like a connection string ?

    Web Config does sound tempting...
    I take it you just build the tag kinda like the conn string?

  6. #6
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449
    Hi,

    You really need to search your MSDN or the online version a bit for constant declaration and web.config usage. You'll be amazed by the number of examples you'll find there.

    Cheers,
    NTG
    Last edited by ntg; Oct 17th, 2004 at 05:11 PM.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Exactly...so if they do change why should i recode them in every place rather than one constant ???
    I don't mean that a variable needs to be defined everywhere, just that a constant is just that. Maybe you really mean a "global" variable and not a constant per se. I don't know.

    Your syntax in your original post is correct, and you'll get the constant you need. You could do as suggested (via app.config), and make your "constant" well, "variable" .

    Really depends on what you need. Seems I remember that defining constants somehow saves memory space.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width