GetValue for Field contains too many arguments

25 Views Asked by At

I have a field that I'm trying to extract it's value. I'm trying to make this method generic as the field could contain a Double or a Color as it's value. I can get the type of the field easily enough, but I get an error whenever I try to call GetValue on the field saying

"Too many arguments to Public Overloads Function GetValue(Of T)() as T"

Here is my code

Try
  Dim field = renderMaterial?.Fields?.GetField(slotName)

  If field IsNot Nothing Then
    Dim fieldType As Type = field.GetType()
    Dim value = field.GetValue(renderMaterial)

    If fieldType = GetType(DoubleField) AndAlso DirectCast(value, DoubleField).HasValue Then
      _Attributes.Number = DirectCast(value, DoubleField).Value
    End If

    If fieldType = GetType(Color4fField) AndAlso DirectCast(value, Color4fField).HasValue Then
      _Attributes.Color = DirectCast(value, Color4fField).SystemColorValue
    End If
  End If
Catch ex As Exception
End Try

** Update ** Well, I thought I made some progress because I was able to get it to compile correctly. The GetValue method needs to know the type of data it's trying to retrieve (I think) and so I looked at the base class of DoubleField and Color4Field which is a class simply called Field. So, I used that and now it compiles just fine. The problem I've run into now is that the value that is returned is always null (or Nothing in VB). Here's the latest code:

Try
  Dim field = renderMaterial?.Fields?.GetField(slotName)

  If field IsNot Nothing Then
    Dim fieldType As Type = field.GetType()
    Dim value = field.GetValue(Of Field)

    If fieldType = GetType(DoubleField) Then
      _Attributes.Number = DirectCast(value, DoubleField)?.Value
    End If

    If fieldType = GetType(Color4fField) Then
      _Attributes.Color = DirectCast(value, Color4fField)?.SystemColorValue
    End If
  End If
Catch ex As Exception
  ' Handle exception appropriately
End Try
2

There are 2 best solutions below

0
Olivier Jacot-Descombes On

You must do the test like this, assuming that the different field types inherit from a base field class:

If TypeOf field Is DoubleField Then
1
andyopayne On

Thanks to @Olivier's suggestions above. They did help me move in a different direction. Ultimately, I was able to solve it by using the following code.

Try
  Dim field = renderMaterial?.Fields?.GetField(slotName)
  If field IsNot Nothing Then
    If TypeOf field Is DoubleField Then
      Dim value = field.GetValue(Of Double)
      _Attributes.Number = value
    ElseIf TypeOf field Is Color4fField Then
      Dim value = field.GetValue(Of Color4f)
      _Attributes.Color = value.AsSystemColor()
    End If
  End If
Catch ex As Exception
End Try