0

Could I create dynamically variable names for already declared variables using vb.net. A little, but not quite, the way I would do the 'echo' of PHP?

For example if I had a code looking like that:

Module Module1

    Public Class my_Class
        <VBFixedString(10)> Public item1 = ""
        <VBFixedString(10)> Public item2 = ""
        <VBFixedString(10)> Public item3 = ""
        <VBFixedString(10)> Public item4 = ""
    End Class

    Public Selected_Item As New my_Class

    Public Sub showItem(theItem As String)
        Select Case theItem
            Case "1"
                Selected_Item.item1 = "Item 1 selected"
            Case "2"
                Selected_Item.item2 = "Item 2 selected"
            Case "3"
                Selected_Item.item3 = "Item 3 selected"
            Case "4"
                Selected_Item.item4 = "Item 4 selected"
        End Select
    End Sub

End Module

Would there be a way; instead of going through a select case to construct something like:

Dim SelItem = "Item" + theItem 

And somehow be able to construct something like that: Selected_Item.SelItem

In practice; what I try to do is:

I have a form with a NumericUpDown and a DateTimePicker. I would like, without using a table, to assign the DateTimePicker.Value to a different variable depending of the NumericUpDown.Value

Is there a possibility to achieve this?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
NorKayak
  • 41
  • 7
  • You would have to use Reflection for that. – jmcilhinney May 02 '21 at 05:09
  • @jmcilhinney What is Reflection? – NorKayak May 02 '21 at 05:10
  • Sounds like the job of a `Dictionary(Of String, String)` rather than a class object + switch selector. – Jimi May 02 '21 at 05:14
  • You typically use collections (arrays, dictionaries) for such a purpose. However, you specify that you want to assign the datepicker value to "different variables", but "without using a table". I am always intrigued when people want to limit their solution scope with criteria that don't make sense to me. Sometimes it's actually clever, but most of the time it indicates an XY-problem and more effective solutions already exist. Can you explain why you explicitly want to avoid certain solutions beforehand to get the job done? – Bart Hofland May 02 '21 at 05:16
  • 1
    @Jimi . . . Looking at the issue, I agree that using a dictionary might be a good choice. However, since it's intended to store dates, it probably should be a `Dictionary(Of String, Date)`. – Bart Hofland May 02 '21 at 05:24
  • @Bart Hofland Actually I wanted to use an array in my class but this array must have a class too and I don't know how to do that. So I went for the easy way which I admit is not elegant. Or maybe you can point me to how I assign a class to an array that is inside a class? – NorKayak May 02 '21 at 05:24
  • @Bart Hofland How do I use a dictionary? – NorKayak May 02 '21 at 05:25
  • @Jimi How do I use a Dictionary? – NorKayak May 02 '21 at 05:26
  • @Bart Hofland Reading the question to the end (which I didn't before :), it's then probably `Dictionary(Of Integer, Date)` or `Dictionary(Of Double, Date)`. @NorKayak A [Dictionary](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2) is an object that correlates unique Keys to a Value. -- You also have [ValueTuples](https://docs.microsoft.com/en-us/dotnet/api/system.valuetuple) and Named ValueTuples, or a collection of [KeyValuePair](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.keyvaluepair-2) elements (as other simple examples). – Jimi May 02 '21 at 05:34
  • @Jimi . . . Correct. My apologies. :) – Bart Hofland May 02 '21 at 05:34
  • I'm not sure what to do with your code snippet. It does not seem to directly relate to your actual question. – Bart Hofland May 02 '21 at 06:43
  • You are using the [`VBFixedString`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.vbfixedstringattribute) attribute on your class' string properties. Do you have a very good specific reason for that? (If so, I would advise you to document so in your code, since it's not a common way to use strings. If not, I would advise to just use default dynamic length strings.) In your code, this attribute probably has no special effects. – Bart Hofland May 02 '21 at 06:46

1 Answers1

1

Looking at the provided code, I think that you are still struggling with its design. Currently, you have a class that contains four items. And you create a variable of that class that should hold a selected item. Doing so, you create a selected item that contains four items...

Furthermore, your code only uses Strings. I don't see any code related to dates or your NumericUpDown and DateTimePicker controls. It's difficult to reason about the logic with such differences. I'll try to focus on the actual question about a numeric up/down control and the collection that should store dates depending on the up/down control's selected value.

Let me assume the following requirements:

  • It should be possible to store up to four values (dates) in a collection (like an array or a dictionary).
  • The user should be able to select a specific entry in the collection (using a NumericUpDown) and modify its value (using a DateTimePicker).

You probably do not need a specific class for your items (yet), so you could actually drop your my_Class class.

I would choose a Dictionary here to store the date values:

Dim items As New Dictionary(Of Integer, Date)

You can then get the date from the dictionary like this:

Public Sub showItem(key As Integer)
    If items.ContainsKey(key) Then
        Dim dateValue As Date = items(key)

        If DateTimePicker1.ShowCheckBox Then
            DateTimePicker1.Checked = True
        End If

        DateTimePicker1.Value = dateValue
    Else
        If DateTimePicker1.ShowCheckBox Then
            DateTimePicker1.Checked = False
        End If

        DateTimePicker1.Value = Nothing  '"Nothing" actually represents a default value, so it "works" for value types and structures (like dates) as well. :)
    End If
End Sub

Note that I changed showItem's parameter's type to Integer. The Value of a NumericUpDown control is of type Decimal, and since you will probably use it for integer values 1 to 4, you could cast that Value to an Integer and pass it to showItem. (I assume you want to call the showItem method inside your NumericUpDown control's ValueChanged event.)

Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    Dim key as Integer = CInt(NumericDropDown1.Value)
    showItem(key)
End Sub

This example only shows you how to read values from your dictionary. Depending on what you actually want to do, you need to shape the logic in the showItem method. And several other methods as well. I would at least expect that you want to implement an event handler that is triggered when your DateTimePicker's value is changed; it should store the selected date in the dictionary. Something like this:

Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
    Dim key As Integer = CInt(NumericUpDown1.Value)

    If DateTimePicker1.Checked Then
        items(key) = DateTimePicker1.Value
    Else
        items.Remove(key);
    End If

    showItem(key)
End Sub
Bart Hofland
  • 3,700
  • 1
  • 13
  • 22