0
double usd, usd2;

for (int x = 0; x < 14; x++)
            {

            if (usd >= principle[x * 2] && usd <= principle[x * 2 + 1])
            {
                usd2 = usd + fee[x];
                break;
            }
        }


peso = usd2 * 50.26;

principle[] is an array of price ranges principle[0] - principle[1]. usd2 is supposed to be the usd with an added fee. I wanted to use the usd2 after but it errors at the `peso = usd2 * 50.26 and it says "use of unassigned local variable usd2". Should I use return? I am using an array to check the price usd in the principle(price ranges). I am using c#

Kylie
  • 1
  • 2
  • 1
    Looks like C++ translated to C# ... – Stefan Steinegger Apr 04 '17 at 08:24
  • usd, usd2 just needs assignment for compiler to be 100% sure that your program doesn't use uninitialized value / memory – Mrinal Kamboj Apr 04 '17 at 08:26
  • I **woudln't assign a value** just to make the compiler happy! It would hide a serious issue when usd2 is actually not assigned a proper value. When you expect the condition to match for any of the items, throw an exception when this is not the case. (Unfortunately the question was closed before I could post my answer.) – Stefan Steinegger Apr 04 '17 at 08:29
  • 1
    @StefanSteinegger, this is compiler's requirement not run-time, there has to be value assignment fixed or variable, else it will not proceed to the level of throwing exceptions – Mrinal Kamboj Apr 04 '17 at 08:30
  • It is already answered :) – Kylie Apr 04 '17 at 08:48
  • @StefanSteinegger: If you have another solution you can still post it as a comment. Telling us you have a better answer doesn't mean much if you don't share that answer. – David Apr 04 '17 at 09:14
  • @MrinalKamboj: The compiler tells us that there is a case where not all required information is available. We shouldn't "fix" it by taking "something" (e.g. 0.0 instead of a value that should always be found). If there would be a valid default, I guess the OP wouldn't have been run into that error. Business rules should probably be covered somewhere else. But programming errors should always throw whenever discovered. When you *expect* valid data in this method, you shouldn't ignore it when it's not. You may have a hell of a time to find out why the calculation is wrong. – Stefan Steinegger Apr 04 '17 at 13:33
  • @David: Comment do not allow useful source code formatting. I try anyway: `for (...) { if (found) {return peso} } /* after for */ throw new InvalidOperationException()` – Stefan Steinegger Apr 04 '17 at 13:37
  • @StefanSteinegger: That doesn't address the compiler error though. There may be business logic to apply and exceptions to throw, but none of it will execute if the code doesn't compile. It's *possible* that `0.0` is a perfectly valid resulting value. (If the value was being used in division, that would be another story entirely. But it's not.) – David Apr 04 '17 at 13:38
  • @David: Sure it solves the compiler error. Only when you try to access the value *outside* of the condition, the compiler complains that it might not have been initilaized. When you only use the value *inside* the condition, it's all fine. Sure it is *possible* that 0.0 is a valid default. When you look at this code, it most probably is not. There is a mapping from principle to fee, why should the sum with "usd" default to 0.0? "When I don't find the price, it's for free"? – Stefan Steinegger Apr 04 '17 at 13:53
  • @StefanSteinegger: Try it for yourself, it doesn't solve the compiler error. The compiler needs to guarantee that a value will be assigned at runtime. No matter how much business logic you implement to assure yourself that a value will be assigned, it doesn't mean anything to the compiler. You're advocating that additional checks and logic be added, and that *might* indeed be correct. But you're also advocating that an initial value *not* be set, which is exactly what's causing the compiler error and is entirely incorrect. – David Apr 04 '17 at 13:56
  • @David: Please look at my proposal again. The variables can be put all within the condition and initialized immediately. There is no business logic check. Only programming error. It **needs** to throw an exception, because data is missing. Like a dictionary throws an exception when you try to access a key that doesn't exist. It has nothing to do with business logic. – Stefan Steinegger Apr 04 '17 at 14:01
  • @StefanSteinegger: Ah, I see what you're suggesting now. Put the `return` in the loop. Then always throw after the loop to address all code paths returning a value. Ok, that does make sense to me now. (You're right, comments aren't great for code...) Depends on what the OP needs to implement I guess. We're debating business logic at this point. – David Apr 04 '17 at 14:03
  • @StefanSteinegger: Although, using `return` is also an assumption based on the original code. We don't know what this method returns *now*, if anything at all. So it's still assuming a lot. – David Apr 04 '17 at 14:05

1 Answers1

3

The compiler can't guarantee that a value is ever assigned. You need to provide a default value:

double usd = 0.0D;
double usd2 = 0.0D;

The way the compiler can at least guarantee that if the loop or the if are never entered then something is still assigned to that variable.

David
  • 208,112
  • 36
  • 198
  • 279
  • It worked! Thank you very much :) – Kylie Apr 04 '17 at 08:27
  • No! You hide errors! Please see my comment on the question. – Stefan Steinegger Apr 04 '17 at 08:31
  • @StefanSteinegger: Why assume that this is an error? If there is business logic to be enforced, that can certainly be enforced as well. It's not mentioned in the question though. Do you have another suggestion to correct the compiler error? – David Apr 04 '17 at 08:32
  • You're right. I assumed that it should never be the case that the value is not set, because the OP didn't think about handling it. When the value is found, it can calculate peso and return it. When it reaches the end of the loop without finding, it should throw. – Stefan Steinegger Apr 04 '17 at 13:21