Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo

  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








 
Previous Posts
    - Webbrowser Control Question
    - How to unload a COM DLL?
    - How to convert Richtextbox to Textbox in .NET
    - How to detect if application is already running in...
    - GetForeGroundWindow
    - Reflection: Determining a Method's Attributes from...
    - Open a form from menu
    - Adding a reference in VS.NET
    - Converting String Value to a Formula for calculati...
    - Microsoft Office Document Image Writer & Vb.Net

Archives
    - 08/01/2004 - 08/08/2004
    - 08/08/2004 - 08/15/2004
    - 08/15/2004 - 08/22/2004
    - 08/22/2004 - 08/29/2004
    - 08/29/2004 - 09/05/2004
    - 09/05/2004 - 09/12/2004
    - 09/12/2004 - 09/19/2004
    - 09/19/2004 - 09/26/2004
    - 09/26/2004 - 10/03/2004
    - 10/03/2004 - 10/10/2004
    - 01/02/2005 - 01/09/2005
    - 01/09/2005 - 01/16/2005
    - 01/30/2005 - 02/06/2005
    - 01/01/2006 - 01/08/2006


Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland