• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Arrays of Contants - is this possible?

I would like to do something like this pseudo code - Does anyone have any suggestions

Type OrderType
   OrderTypeID as integer
   name as String
   value as integer
end type

Enum MovementOption As OrderType
        CounterSale = 1, "Counter Sale", 10
        InternetSale =  2, "Internet Sale", 20
        MailOrder = 3, "MailOrder", 30
End Enum

I have a column in a data base table which is an OrderType field which stores 1, 2 or 3 ie counter, internet or mail order.  I would then like to be able to do something like this later

dim myOrderType as OrderType
myOrderType.OrderID = retrieved from database field

set somevalue = myOrderType.Name
set othervalue = myOrderType.value

I guess what I am after is some method of retrieving the Name and Value from the ID.  All these values I want hard coded into the program.

Regards


Paul
0
PaulHennessy
Asked:
PaulHennessy
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
Jeff CertainCommented:
Paul,

What about taking this approach?

Dim movementNames as String(3)
movementNames(1) = "Counter Sale"
movementNames(2) = "Internet Sale"
movementNames(3) = "Mail Order"

Dim movementValues as Integer(3)
movementValues (1) = 10
movementValues (2) = 20
movementValues (3) = 30

dim myOrderType as OrderType
myOrderType.OrderID = retrieved from database field

set somevalue = movementNames(myOrderType.OrderId)
set othervalue = movementValues (myOrderType.OrderId)

Hope this helps.

Jeff
0
 
Jeff CertainCommented:
Paul,

One refinement would be to use a two-dimensional array of objects, and use contants for the indexes.

Dim movements as Object(3,1)
Const movementNameIndex as integer = 0
Const movementValueIndex as integer = 1

Then you'd set the array, and retrieve values like this:

set somevalue = movements(myOrderType.OrderId,movementNameIndex)

Also, the array can be set something like this (you'd have to check the exact syntax):
movementValues ={,10,20,30}

The initial blank entry is required because the arrays are zero-based, and you're only using values of 1, 2 and 3.

Hope this helps.

Jeff
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It would make the most sense to create a new table in your database to store the this information so it can be retrieved and will be easily updateable and manageable without changing your code.

If you really want to hard code it into your application then here is another approach:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private OrderTypes As New Hashtable

    Private Structure OrderType
        Public OrderTypeID As Integer
        Public Name As String
        Public Value As Integer
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ot As OrderType

        ot = New OrderType
        ot.OrderTypeID = 1
        ot.Name = "Counter Sale"
        ot.Value = 10
        OrderTypes.Add(ot.OrderTypeID, ot)

        ot = New OrderType
        ot.OrderTypeID = 2
        ot.Name = "Internet Sale"
        ot.Value = 20
        OrderTypes.Add(ot.OrderTypeID, ot)

        ot = New OrderType
        ot.OrderTypeID = 3
        ot.Name = "MailOrder"
        ot.Value = 30
        OrderTypes.Add(ot.OrderTypeID, ot)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myOT As OrderType

        Dim myOrderType As Integer
        Dim someValue As String
        Dim otherValue As Integer

        myOrderType = 2 ' retrieved from database field
        myOT = CType(OrderTypes(myOrderType), OrderType)
        someValue = myOT.Name
        otherValue = myOT.Value
    End Sub

End Class
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PaulHennessyAuthor Commented:
Thanks for your comments so far guys. Idle Mind - I have already got this configured as an extra table on the SQL server - it just seems a bit of an overhead to keep retreiving just three never changing records.   I just can't believe there is no way of hard coding the data somehow as constants or even an XML file?...  I'll leave this open for a while longer!

Paul
0
 
Jeff CertainCommented:
Paul,

The approach I outlined above is hard-coded into the program. Did you want something that relied more on constants?

Jeff
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> I have already got this configured as an extra table on the SQL server - it just seems a bit of an overhead to keep retreiving just three never changing records.

Great.  So when your application loads, you can use the technique I have shown above to populate the OrderTypes HashTable with the values read from your SQL server.  You only need to do it once at program load and then you can retrieve these values from the HashTable which will be faster than querying the database.

>> just three never changing records.

Then, when you add another value to the table (you never know...) it will automatically be handled by the code since it isn't hardcoded.

~IM
0
 
ihenryCommented:

I like the idea from Idle_Mind, and to go more further I would have the HashTable as a private static variable in a singleton class so that when application start it retrieves OrderTypes data from database once and can only be accessed by name from a public static method.


Public Structure OrderType
    Dim OrderTypeID As Integer
    Dim Name As String
    Dim value As String
End Structure


Public Class OrderTypeHelper

    Private Shared ReadOnly OrderTypes As Hashtable = GetOrderTypes()
    Public Shared ReadOnly Current As Class1 = New OrderTypeHelper

    Private Sub New()
        MyBase.New()
    End Sub

    Public Function GetOrderType(ByVal name As String) As OrderType
        Return CType(OrderTypes(name), OrderType)
    End Function

    Private Shared Function GetOrderTypes() As Hashtable
        ' retrieve OrderType records from database
        ' make sure you initialize the hashtable with CaseInsensitiveHashCodeProvider object to avoid
        ' hashtable key case-sensitivity problem
    End Function

End Class
0
 
PaulHennessyAuthor Commented:
Thanks for your help on this guys.  Obviously it it not possible to do this neatly!  In the end I have used the following code which is a little crude but does work elegantly!

Class MovementType
    Enum MovementOption As Integer
        CounterSale = 1
        InternetSale = 2
        Order = 3
    End Enum

    Public MovementTypeID As MovementOption
    Public ReadOnly Property Descripion() As String
        Get
            Select Case MovementTypeID
                Case MovementOption.CounterSale
                    Return "Counter Sale"
                Case MovementOption.InternetSale
                    Return "InternetSale"
                Case MovementOption.Order
                    Return "Order"
             End Select
        End Get
    End Property

    Public ReadOnly Property EffectOnStock() As Integer
        Get
            Select Case MovementTypeID
                Case MovementOption.CounterSale
                    Return -1
                Case MovementOption.InternetSale
                    Return -1
                Case MovementOption.Order
                    Return 0
             End Select
        End Get

    End Property
End Class
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now