1

I have this code:

public float? InputCutOffFrequency { get; set; }//fc public float? InputF1 { get; set; } public float? InputF2 { get; set; } public float InputTransitionBand { get; set; } public float InputFS { get; set; } public float calcFc1(float f, float fs, float transition) { float _f = f + (transition / 2); _f /= fs; return _f; } float fc1; fc1 = calcFc1(InputCutOffFrequency, InputFS, InputTransitionBand); 

When running this code, I get this error:

cannot convert from 'float?' to 'float 

How can I fix this error?

7
  • 2
    What do you want the value to be if it’s null? Commented May 24, 2022 at 23:38
  • i want it to be equals 0 Commented May 24, 2022 at 23:39
  • you either supply default value like InputCutOffFrequency??0 or ignore it via InputCutOffFrequency.Value Commented May 24, 2022 at 23:39
  • 2
    @IłyaBursov - Value will throw if its null, it doesnt 'ignore it' Commented May 24, 2022 at 23:40
  • @pm100 I meant ignore possibility of NRE Commented May 24, 2022 at 23:45

3 Answers 3

6

It's a nullable float so you will need to specify you want the value.

fc1 = calcFc1(InputCutOffFrequency.Value, InputFS, InputTransitionBand); 

Or you can default it if it's null

fc1 = calcFc1(InputCutOffFrequency.GetValueOrDefault(0), InputFS, InputTransitionBand); 
Sign up to request clarification or add additional context in comments.

1 Comment

Good use of the GetValueOrDefault method. I always forget this exists
1

In C#, the ?, when used after a type, indicates that the variable associated with the type can be null or a concrete value. The function you wrote, calcFcl, has a signature that takes three floats and produces a float as output. However, when you call it, you're providing it with a float? as the first argument. Since floating-point operations with null values aren't valid, the compiler doesn't know what to do when this is the case.

There are three ways you can address this issue:

  1. Change InputCutOffFrequency so that it is of type float instead of type float?.
  2. Change how you call calcFc1 so that you send the value of InputCutOffFrequency instead of the nullable value:
 // Force a value (this will throw an InvalidOperationException if `InputCutOffFrequency is null) fc1 = calcFc1(InputCutOffFrequency.Value, InputFS, InputTransitionBand); // Coalesce the value (this will force a value, but you need to be sure it's the one you want) fcl = calcFcl(InputCutOffFrequency ?? 0, InputFS, InputTransitionBand); 
  1. Change the signature of calcFcl so that it accepts, for its first argument, a value of float? and then modify the function body so that it handles the case where the first argument is null.
public float calcFc1(float? f, float fs, float transition) { float _f = (f ?? 0) + (transition / 2); _f /= fs; return _f; } 

In this case, the f ?? 0 is coalescing f to a value. Essentially, if f is null, then 0 will be used. If f is not null, then the value in f will be used.

For more information on nullable types in C#, feel free to check out the documentation.

Comments

1

1 :

fc1 = calcFc1(InputCutOffFrequency.Value, InputFS, InputTransitionBand); 

or 2 :

public float calcFc1(float f?, float fs, float transition) { float _f = f + (transition / 2); _f /= fs; return _f; } 

You can change signature of nullable variable as float? in your function. [2]

Another way is getting Value of nullable variable like InputCutOffFrequency.Value [1]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.