Reducing some array lines of code with .NET
(Friday, September 03, 2004)
Found the following interesting discussion in the Newsgroups:
reducing some array lines of code by:Eric Sabine
| Below is a sub I have which contains 4 lines of code. I would like to reduce it to 1. My problem is that it takes 3 lines to create the array to hold the sql parameter and since there's only one dimension in the array, I'd like to do something more like what's at the bottom of this post, but a lack of understanding prevents me from doing it properly. Any suggestions?
Sub getUserVendorAssignment(ByVal UserName As String) ' fill the table UserVendor in the local dataset with the ' results from the usp_dr_userVendorReceivingAssignments stored procedure ' Pass in one parameter item which is received as a string
' 3 lines to create our parameter? Dim arParms() As System.Data.SqlClient.SqlParameter = _ New System.Data.SqlClient.SqlParameter(0) {} arParms(0) = New SqlParameter("@user_name", SqlDbType.VarChar, 8) arParms(0).Value = UserName .ToUpper
localFillDataset( _ "dbo.usp_dr_userVendorReceivingAssignments", _ "UserVendor", _ arParms) End Sub
-------------------- what I'd _like_ to do (I know it's wrong, it's just the "guess") localFillDataset( _ "dbo.usp_dr_userVendorReceivingAssignments", _ "UserVendor", _ New SqlParameter() {CType(UserName , SqlParameter)}) -------------------
thanks Eric
| | | Reply: by:Armin Zingler
| | | You could write a function returning the array. -- Armin
| | | Reply: by:Eric Sabine
| | | Thanks Armin, I saw your response and I kept hammering at it. I did get to achieve the 1 line of code that I wanted :-)
This is what I ended up doing
localFillDataset( _ "dbo.usp_dr_userVendorReceivingAssignments", _ "UserVendor", _ New SqlParameter("@user_name", UserName))
and I changed the signature of localFillDataset to the following (I added the "ParamArray")
Sub localFillDataset(ByVal ProcedureName As String, _ ByVal TableName As String, _ ByVal ParamArray prms() As System.Data.SqlClient.SqlParameter)
My question to you is, was the absence of ParamArray in the first place incorrect? The IDE didn't seem to complain when I left it out. Actually, everything worked fine with the earlier code.
Eric
|

|
0 Comments:
Post a Comment
<< Home