1

I have the following code that has worked fine for months, but I forgot to create this class with Option Strict On so now I am going back to clean up my code correctly, however I haven't been able to figure out a way around the following issue.

I have a local variable declared like this:

Private _manageComplexProperties

Now with option strict, this isn't allowed due to no As clause which I understand, however the reason that it is like this is because the instance of the class that will be assigned to it takes a type parameter which isn't known until run time. This is solved by the following code:

Private _type As Type
*SNIP OTHER IRRELEVANT VARIABLES*

Public Sub Show()

    Dim requiredType As Type = _
        GetType(ManageComplexProperties(Of )).MakeGenericType(_type)

    _manageComplexProperties = Activator.CreateInstance(requiredType, _
         New Object() {_value, _valueIsList, _parentObject, _unitOfWork})

    _result = _manageComplexProperties.ShowDialog(_parentForm)
    If _result = DialogResult.OK Then
        _resultValue = _manageComplexProperties.GetResult()
    End If

End Sub

Again option strict throws a few errors due to late binding, but they should be cleared up with a cast once I can successfully declare the _manageComplexProperties variable correctly, but I can't seem to get a solution that works due to the type parameter not known until run time. Any help would be appreciated.

XN16
  • 5,679
  • 15
  • 48
  • 72

2 Answers2

1

Declare your variable as Object

Private _manageComplexProperties as Object

And then you will have to persist with reflection, e.g. to call ShowDialog method:

Dim method As System.Reflection.MethodInfo = _type.GetMethod("ShowDialog")
_result = method.Invoke(_manageComplexProperties, New Object() {_parentForm})
Szymon
  • 42,577
  • 16
  • 96
  • 114
  • If I do that, I still can't cast `_manageComplexProperties` variable in the `Show()` method to anything so I can use the `.ShowDialog()` or `.GetResult()` methods. I am beginning to think that you can't use `Option Strict On` in this situation! – XN16 Oct 17 '13 at 11:34
  • Can't you use `DirectCast(_manageComplexProperties, YourType)`? OK. you can't. A sec... – Szymon Oct 17 '13 at 11:38
  • 1
    You will need to use reflection until the end, I'm afraid. See my edit for an example. – Szymon Oct 17 '13 at 11:42
  • 1
    With a little extra work regarding properties rather than methods, getting the correct overload and casting, my code now works as it did before, thanks very much! – XN16 Oct 17 '13 at 13:14
0

You have to use option infer on on the top of your vb file. It enables local type inference.

Using this option allows you to use Dim without the "As" clausule, it is like var in C#.

IntelliSense when Option Infer and Option Strict are off

enter image description here

IntelliSense when Option Infer is on (as you can see it has type inference)

enter image description here

If you do not want to use option infer on, you will have to declare the variable matching the type of the one returned by Activator.CreateInstance

Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • I don't think this is working for me, either that or I am misunderstanding something which is entirely possible! In effect I need to declare the variable like this: `Private _manageComplexProperties = ManageComplexProperties(Of )`, however it needs a type in the declaration, which I don't know at that point. – XN16 Oct 17 '13 at 11:26