मैं कथन के बाहर insuranceCost
कैसे उपलब्ध कर सकता हूं?'if' कथन के बाहर एक चर का उपयोग
if (this.comboBox5.Text == "Third Party Fire and Theft")
{
double insuranceCost = 1;
}
मैं कथन के बाहर insuranceCost
कैसे उपलब्ध कर सकता हूं?'if' कथन के बाहर एक चर का उपयोग
if (this.comboBox5.Text == "Third Party Fire and Theft")
{
double insuranceCost = 1;
}
इसे कथन के बाहर परिभाषित करें।
double insuranceCost;
if (this.comboBox5.Text == "Third Party Fire and Theft")
{
insuranceCost = 1;
}
आप विधि से लौट रहे हैं, तो आप यह एक डिफ़ॉल्ट मान या 0 "अ-निरुपित चर का प्रयोग करें" असाइन कर सकते हैं, अन्यथा आप त्रुटि आ सकती है,;
double insuranceCost = 0;
या
double insuranceCost = default(double); // which is 0.0
double insuranceCost = 0;
if (this.comboBox5.Text == "Third Party Fire and Theft")
{
insuranceCost = 1;
}
अगर बयान से पहले ही घोषित होने पर डिफ़ॉल्ट मान दे रही है। अगर मूल्य के अंदर मूल्य सेट करें। यदि आप डबल पर डिफ़ॉल्ट मान नहीं देते हैं, तो आपको संकलन समय पर एक त्रुटि मिलेगी। उदाहरण
double GetInsuranceCost()
{
double insuranceCost = 0;
if (this.comboBox5.Text == "Third Party Fire and Theft")
{
insuranceCost = 1;
}
// Without the initialization before the IF this code will not compile
return insuranceCost;
}
अन्य उत्तर के अलावा, आप सिर्फ इस मामले में if
इनलाइन सकता है के लिए (कोष्ठक केवल स्पष्टता के लिए जोड़ा):
double insuranceCost = (this.comboBox5.Text == "Third Party Fire and Theft") ? 1 : 0;
आप प्रारंभ करना चाहते हैं जो कुछ भी मूल्य के साथ बदलें 0
insuranceCost
, यदि स्थिति मेल नहीं खाती है।
बीमा को क्या मूल्य होना चाहिए यदि कॉम्बोबॉक्स 5 का टेक्स्ट अलग है? – Heinzi