Grasshopper

algorithmic modeling for Rhino

Hi,

I suppose this is a basic one - I keep running into problems with this though...

I have a VB component with 2 inputs: a double and a list of doubles. I want to check if the elements in the list are greater or less than the one double.

In the following code I put in an extra input with the length of the list ("intLength") but I guess this can be done without this...

 

    For counter = 0 To intLength
      If dblDistClosest(counter) > dblLengthHelpLine Then
        A = -1
      Else
        A = 1
      End If
    Next

The error I get:

Error: Overload resolutino failed because no accessible '>' can be called with these arguments:
'Public Shared Operator >(Left AS Double, right As Double) As Boolean': Value of type
'System.Collections.Generic.List(Of Double)' cannot be converted to 'Double'.

 

I have tried variations and the error changes but it's all in the same line

(e.g. Error: Operator '>' is not defined for types 'Object' and 'System.Collections.Generic.List(Of Double)'.)

 

thanks for any pointers.

wim

Views: 569

Replies to This Discussion

Hi Wim,

 

this function only checks the last value in your list. Or rather, it check them all, but the last value overrides the return value so it might as well only test the last value.

 

Assuming dblDistClosest is a List(Of Double) and dblLengthHelpLine is a Double, then the following code will work:

 

Dim rc As Int32 = +1

For i As Int32 = 0 To dblDistClosest.Count - 1

  If (dblDistClosest(i) > dblLengthHelpLine) Then rc = -1

Next

A = rc

 

You are correct in assuming you do not need the intLength input, a List(Of Whatever) knows how many items it contains.

 

Based on the errors you reported, two things are going wrong here:

 

1) Overload resolution failed blah blah blah, is because you're using the > operator to compare a List(Of Double) with a Double. The larger than operator is only defined for individual numbers.

 

2) Operator '>' is not defined blah blah blah, is because you are treating your Double as a System.Object. Usually this is because you did not set a Type Hint in the input parameter. By default all data that goes into a Script component is of type Object because Object is the most basic type of data possible in .NET. Everything can be represented as a System.Object. However, the larger than operator doesn't know how to handle Objects and it is not forgiving enough to try and convert the data into a type it can handle. It will just cause a compiler error.

 

Are you absolutely sure dblDistClosest is a List(Of Double)? Can you post the entire code, including the readonly line containing the RunScript() method declaration?

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Hi David,

dank je!

Silly mistake on my part - I had originally check for list access to the dblLengthHelpLine input when it was just that single double going in there. Checking for item access solved this.

 

But yes, as you say, it only outputs the last result, which is not what I wanted. The way I tend to solve that is by moving the rc = +1 inside the for - next loop and using print(rc) to stream the consecutive results into a list through the out-port. How 'should' this be done?

 

cheers,

wim

Are you trying to create a list of integers with -1 for every double that was smaller than the threshold and +1 for every double that is larger than or equal to the threshold?

 

Or are you trying to figure out whether any of the numbers in the list is larger than the threshold?

 

If 1)

 

Dim rc As New List(Of Integer)

For Each val As Double in dblDistClosest

  If (val > dblLengthHelpLine) Then rc.Add(+1) Else rc.Add(-1)

Next

A = rc

 

 

 

If 2)

 

Dim rc As Integer = -1

For Each val As Double in dblDistClosest

  If (val > dblLengthHelpLine) Then

    rc = +1

    Exit For

  End If

Next

A = rc

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia


David,

it was the 1st one. I should be able to remember that construction for the future...

Thanks!

 

groetjes,

wim

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service