Grasshopper

algorithmic modeling for Rhino

My class was derived from IGH_Goo.

public bool CastTo<T>(out T target)
{
            if (typeof(T).IsAssignableFrom(typeof(int))) {
                object ptr = this.data;
                target = (T)ptr;
                return true;
            }
           return false; // <- error
}

Throws this error.

Error : The Out Parameter must be assigned before control leaves the current method.

Older IGH_Goo interface has CastTo<T>(ref T target) instead of out.

Views: 464

Replies to This Discussion

If the method doesn't end up in your if-statement the variable "target" will never be assigned. This is a requirement when using out-parameters. Setting "target" to null in an else-scope might be working. Something like this:

public bool CastTo<T>(out T target)
{
            if (typeof(T).IsAssignableFrom(typeof(int))) { 
                object ptr = this.data;
                target = (T)ptr;
                return true;
            }

            else{

               target = null;

               return false; // <- error

            }

}

target = null;

won't work because T may be a value type. You should use:

target = default(T);

--

David Rutten

david@mcneel.com

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service