2010-01-18 78 views
1

创建可空字符串参数请告诉我如何在vb.net中实现字符串类型的可空参数请帮我在Vb.NET

功能:

sub savedetail(byval name as string, byval age as integer) 
if name isnot nothing then 
some work 
end if 
end sub 

我打电话也似的东西

savedetail(nothing,34) //here is giving me format exception 

异常: System.FormatException:输入字符串的格式不正确。

现在我把exect功能..请告诉我,我在哪里犯错:

Function savedetails(ByVal db As DSLibrary.DataProviders.SqlProvider, ByVal name As String, ByVal cityId As Integer, ByVal addr1 As String, ByVal addr2 As String, ByVal phone As String, ByVal fax As String, ByVal zip As String, ByVal contactfname As String, ByVal contactlname As String, ByVal randomcode As String, ByVal reservationEmail As String, ByVal email As String, ByVal url As String, ByVal description As String, ByVal locationInfo As String, ByVal amenities As String, ByVal roomDetails As String, ByVal latitude As Double, ByVal longitude As Double, ByVal enabled As Integer, Optional ByVal hotelId As Integer = Nothing, Optional ByVal checkDuplicate As Boolean = True, Optional ByVal DSPartner As Integer = Nothing, Optional ByVal category As String = Nothing, Optional ByVal rating As Integer = Nothing) As Integer 

电话:

savedetails(db, hotelname, 0, address, Nothing, Nothing, Nothing, postal, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, hotelinfo, Nothing, service, Nothing, Convert.ToDouble(lat), Convert.ToDouble(longi), 1, Convert.ToInt32(hotelid), False, dspartnerid) 
+0

请例外的确切文本。 – Bobby 2010-01-18 13:07:49

+2

你的例子工作正常。必须有其他事情正在进行。 – 2010-01-18 13:08:01

+0

你确定FormatException不是来自你的'一些工作'例程吗? – 2010-01-18 13:23:14

回答

5

Errrhm。一个字符串默认为空,因为它是一个引用类型。 其中Convert.ToDouble()Convert.ToInt32()都可能抛出此异常:以后您提供更多信息

编辑。使用调试器并在呼叫中设置断点并检查这些转换的参数或将它们移到呼叫之外。

+0

,但它给了我错误....让我编辑我的问题,你请再看看 – 2010-01-18 13:03:30

+0

是的,你是对的... convet.todouble抛出异常... – 2010-01-18 13:30:24