2012-12-20 21 views
8

मैं एक परियोजना पर काम कर रहा हूं जो बच्चों को सांता को संदेश भेजने की अनुमति देता है। दुर्भाग्यवश, यदि वे एजीई फ़ील्ड में एक पूर्णांक की बजाय स्ट्रिंग दर्ज करते हैं, तो प्रोग्राम 'डबल' टाइप करने के लिए स्ट्रिंग "[exampleString]" से रूपांतरण क्रैश और रिटर्न वैध नहीं है। क्या यह जांचने का कोई तरीका है कि उन्होंने एक पूर्णांक दर्ज किया है या नहीं? यह कोड है।जांचें कि क्या एक स्ट्रिंग चर के पास एक पूर्णांक मान है

If childAge > 0 And childAge < 150 Then 
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! " 
Else 
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!" 
End If 

धन्यवाद, काई :)

उत्तर

30

एक बहुत ही सरल चाल try parse को एक पूर्णांक के रूप में स्ट्रिंग है। यदि यह सफल होता है, तो यह एक पूर्णांक (आश्चर्यचकित आश्चर्य) है।

Dim childAgeAsInt As Integer 
If Integer.TryParse(childAge, childAgeAsInt) Then 
    ' childAge successfully parsed as Integer 
Else 
    ' childAge is not an Integer 
End If 
+0

धन्यवाद! वह ** वास्तव में ** मदद की! :) –

1

आप इस का उपयोग कर सकते हैं।

Sub checkInt() 
    If IsNumeric(Range("A1")) And Not IsEmpty(Range("A1")) Then 

     If Round(Range("A1"), 0)/1 = Range("A1") Then 
      MsgBox "Integer: " & Range("A1") 
     Else 
      MsgBox "Not Integer: " & Range("A1") 
     End If 
    Else 
     MsgBox "Not numeric or empty" 
    End If 
End Sub 
+0

थोड़ा भ्रामक है, लेकिन ठीक। मदद के लिए धन्यवाद! :) –

-1

नेट में आप एक चर के डेटा प्रकार को निर्धारित करने के लिए GetType() का उपयोग कर सकते हैं।

Dim n1 As Integer = 12 
Dim n2 As Integer = 82 
Dim n3 As Long = 12 

Console.WriteLine("n1 and n2 are the same type: {0}", 
        Object.ReferenceEquals(n1.GetType(), n2.GetType())) 
Console.WriteLine("n1 and n3 are the same type: {0}", 
        Object.ReferenceEquals(n1.GetType(), n3.GetType())) 
' The example displays the following output: 
'  n1 and n2 are the same type: True 
'  n1 and n3 are the same type: False 

ऊपर नमूना आपको एक कोड स्निपेट लिख सकते हैं के आधार पर:

If childAge.GetType() = "Integer" then '-- also use childAge.GetType().Name = "Int32" 
    ' do something 
End if 
+2

यह सवाल का जवाब कैसे देता है? –

+2

@ मेटा-नाइट मैं परिवर्तनीय प्रकार का पता लगाने के लिए केवल 'GetType() 'के साथ नमूना कोड दिखा रहा हूं। ओपी इसका उपयोग यह परिभाषित करने के लिए कर सकता है कि यह 'पूर्णांक', 'लंबा', 'बूलियन' आदि प्रकार है या नहीं। सो जाओ कि आप मुझे नीचे छोड़ दें :( – bonCodigo

+4

यह प्रश्न यह निर्धारित करने के संबंध में था कि क्या स्ट्रिंग सफलतापूर्वक एक इंटीजर को पार्स कर सकती है। गेट टाइप का उपयोग करना यहां मदद नहीं करेगा। यह स्ट्रिंग प्रकार का होगा। –

1

IsNumeric वीबी में बनाया गया है, और वापसी एक सही/गलत

If IsNumeric(childAge) AndAlso (childAge > 0 And childAge < 150) Then 
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! " 
Else 
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!" 
End If 
+0

धन्यवाद, यह काफी उपयोगी है! –

+0

यदि बच्चा किसी संख्या और अक्षरों में रखता है तो यह अभी भी – Dman

4

आप यथोचित निश्चित है कि इनपुट आप हो रही है एक पूर्णांक है होना करने के लिए निम्न दो परीक्षणों प्रदर्शन कर सकता है जाएगा:

If IsNumeric(childAge) AndAlso (InStr(1, childAge, ".") <> 0) Then 
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! " 
    If childAge < 0 OrElse childAge > 150 Then 
     fmSecA2 = "I don't believe it's possible to be" & childAge & " years old..." 
    End If 
Else 
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!" 

InStr फ़ंक्शन शून्य यह स्ट्रिंग है के लिए देखा जा रहा है लगता है नहीं करता है, और इसलिए जब IsNumeric साथ कि परीक्षण के संयोजन, आप भी बाहर possibil शासन यह है कि कुछ फ़्लोटिंग पॉइंट डेटा प्रकार दर्ज किया गया था।

+0

को बग कर देगा।'एक दशमलव विभाजक के रूप में - जो ज्यादातर मामलों में ठीक हो सकता है। लेकिन वैश्वीकरण का उपयोग करना बेहतर हो सकता है। CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparato इसके बजाय – Poat

0

स्टाइक्सी के उत्तर से कार्य करना, यदि आप एक पूर्णांक की बजाय बाइट के रूप में पार्स करते हैं, तो यह नकारात्मक उम्र और 255 की अधिकतम आयु को भी एक ही बार में जांचता है।

Dim childAgeAsByte As Byte 
If Byte.TryParse(childAge, childAgeAsByte) Then 
    ' childAge successfully parsed as Byte 
Else 
    ' childAge is not a Byte 
End If 

क्रिस्टियन

0
dim as integer1 

textbox1.text = integer1 
if integer1 > 0 then 
    ............................ 
    ............................ 
else 
    textbox2.text = "please only enter a Positive integer" 
+2

जबकि यह कोड स्निपेट प्रश्न हल कर सकता है, [स्पष्टीकरण सहित] (http://meta.stackexchange.com/questions/114762/explaining-entirely- कोड-आधारित-उत्तर) वास्तव में आपकी पोस्ट की गुणवत्ता में सुधार करने में मदद करता है। याद रखें कि आप भविष्य में पाठकों के लिए प्रश्न का उत्तर दे रहे हैं, और वे लोग आपके कोड सुझाव के कारणों को नहीं जानते हैं। – secelite