How to Resolve Empty Spaces in C#

Hi!
My empty space and null is destroying my code and I cannot seem to find a satisfying solution?
Can you help, please?

In addition I tried this link (https://stackoverflow.com/questions/7872633/most-advisable-way-of-checking-empty-strings-in-c-sharp), however it doesn’t work for me…

Here is my file: Larger_Than_Input.gh (7.4 KB)

This is happening due to string to number casting.
It will also fail if you connect panel with number of grasshopper, without C#.

That said, you wont be able to do this inside code if your data type is double, because it fails before.

I would do like this:
Make string A instead double A, and the same for B.

Then Convert.ToDouble(A) or parse
If this one fails return or continue with the code

Panel is stores text so what you are doing is string to number conversion.

As @Petras_Vestartas mentioned you need to change input type hints.
in addition to his suggestions you can also use GH_Convert.ToDouble like this:

private void RunScript(System.Object A, double B, ref object T, ref object F)
  {
    double a;
    if (GH_Convert.ToDouble(A, out a, GH_Conversion.Both) && a > B)
      T = a;
    else
      F = A;
}

Larger_Than_Input_re.gh (14.0 KB)

I would like to thank you very much, for paying attention to my post and replying to it.
Also, actually writing the code for me helped a great deal, as I wasn’t sure how to do it myself.
The link you gave is an amazing tool to learn more about how to code in C# in GH
Thank you very much, it is working marvellously! :grin:
You have helped me very much and I would like to humbly thank you for this.

I have a further question regarding the code, to make it even better, do you maybe have any ideas of how to change the empty item into a null?

One more thing, your “logo” or signature that you have saying Derang , is simply flabbergasting and breathtaking, would you mind if I asked what the secret to that is, I mean loading it to GH? Since I would like to try it out myself. I noticed that if I click on it I can load another figure from Rhino however how do you start?

private void RunScript(System.Object A, double B, ref object T, ref object F)
  {
    double a;
    if (GH_Convert.ToDouble(A, out a, GH_Conversion.Both) && a > B)
      T = a;
    else
      F = null;
  }

Larger_Than_Input_re2.gh (16.8 KB)

It’s a simple sketch object, you can find it on CanvasToolbar:
image
More info: The Grasshopper UI | The Grasshopper Primer Third Edition

Thanks, once again!
Here is what I managed with your help:

  private void RunScript(System.Object num, double min_num, ref object T, ref object F)
  {
    double a;

    if (GH_Convert.ToDouble(num, out a, GH_Conversion.Both) && a > min_num)
    {
      T = a;
    }

    else
    {
      if(a == 0)
        F = null;
      else if(a < min_num)
        F = a;
      else
        F = null;

    }
  }

Larger_Than_Input02.gh (58.5 KB)

3 Likes

Nice Logo :heart_eyes: