none
How to define constant based on criteria? RRS feed

  • Question

  • Need to define a constant for the APIs in class, but the constant is different base on the OS. Cannot define variable because the APIs need constant. The C# code is below:

    #if WIN64
            public const int PACK_SIZE = 8;
    #else
            public const int PACK_SIZE = 1;
    #end

    How to realize it in VB.NET? thank you.

    Saturday, December 23, 2017 10:47 AM

All replies

  • There is an #If in VB too:

    #If WIN64 Then

        Public Const PACK_SIZE As Integer = 8

    #Else

        Public Const PACK_SIZE As Integer = 1

    #End If

     

    The WIN64 definition can be provided in the “Advanced Compile Options” of Project Properties for x64 configuration. (Or using the #Const directive in some cases).

    For other usages, see also:

    ReadOnly PACK_SIZE As Integer = If(Environment.Is64BitProcess, 8, 1)

    • Edited by Viorel_MVP Saturday, December 23, 2017 11:24 AM
    Saturday, December 23, 2017 11:17 AM
  • Since the mechanical machine when there was real something that gave always constantly a constant number there are no constants anymore. 

    Currently a constant is just a name used to make a program more readable for the programmer. 

    Therefore what you need is probably an Int32 (if that C#) code is correct. If it is a very old Api, it can also be an Int16 but I doubt it. 


    Success
    Cor

    Saturday, December 23, 2017 11:33 AM
  • gax,

    If it changes then obviously it can't be a constant.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, December 23, 2017 12:07 PM
  • Sorry, i do not understand how to use the "ReadOnly PACK_SIZE As Integer = If(Environment.Is64BitProcess, 8, 1)", my code is below, could you please check it? thank you.

    ///////////////////////////////////////////////////////////////////////////////////////

    Imports System.Runtime.InteropServices

    Public Class test

        'Public Const PACK_SIZE As Integer = 8 'The Value is 8 if OS is x64
        'Public Const PACK_SIZE As Integer = 1 'The Value is 1 if OS is x86

        Public Const LINE_LEN As Integer = 256
        Public Const MAX_PATH As Integer = 260
        Public Const ANYSIZE_ARRAY As Integer = 1

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode, Pack:=PACK_SIZE)>
        Public Structure SP_DEVINFO_DATA
            Public cbSize As Integer
            Public ClassGuid As Guid
            Public DevInst As Integer
            Public Reserved As IntPtr
        End Structure

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode, Pack:=PACK_SIZE)>
        Public Structure SP_DRVINFO_DATA_V2_W

            Public cbSize As Integer

            Public DriverType As Integer

            Public Reserved As IntPtr

            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=LINE_LEN)>
            Public Description As String

            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=LINE_LEN)>
            Public MfgName As String

            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=LINE_LEN)>
            Public ProviderName As String

            Private DriverDate As System.Runtime.InteropServices.ComTypes.FILETIME

            Public DriverVersion As ULong
        End Structure

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode, Pack:=PACK_SIZE)>
        Public Structure SP_DRVINFO_DETAIL_DATA_W

            Public cbSize As Integer

            Private InfDate As System.Runtime.InteropServices.ComTypes.FILETIME

            Public CompatIDsOffset As Integer

            Public CompatIDsLength As Integer

            Public Reserved As IntPtr

            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=LINE_LEN)>
            Public SectionName As String

            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)>
            Public InfFileName As String

            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=LINE_LEN)>
            Public DrvDescription As String

            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=ANYSIZE_ARRAY)>
            Public HardwareID As String
        End Structure

    End Class

    Saturday, December 23, 2017 12:44 PM