Results 1 to 7 of 7

Thread: VS2008 and higher:: Curiosity

  1. #1

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Exclamation VS2008 and higher:: Curiosity

    I was wondering for those who are developing in VS2008 or higher are taking advantage of LINQ and language extensions not because they are something different but because they make development easier with consistency in coding and maintenance?

    Simple everyday example makes string to integer method based
    Code:
    <System.Diagnostics.DebuggerStepThrough()> _
    <Runtime.CompilerServices.Extension()> _
    Public Function ToInteger(ByVal value As String) As Integer
        Return Convert.ToInt32(value)
    End Function
    Keeping functional, reusable code behind away from business logic and novice developers and allows for easy maintenance similar to the next example
    Code:
    <System.Diagnostics.DebuggerStepThrough()> _
    <Runtime.CompilerServices.Extension()> _
    Function CurrentRowCellValue(ByVal GridView As DataGridView, ByVal ColumnName As String) As String
        Return GridView.Rows(GridView.CurrentRow.Index).Cells(ColumnName).Value.ToString
    End Function
    Lastly anyone using LINQ to quickly test queries as shown below?
    (Code below is to show a concept, I code with database classes)

    Code:
    Using cn As New OleDb.OleDbConnection( _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data\demo.mdb;")
        Dim da As New OleDb.OleDbDataAdapter( _
        "SELECT PersonName, ContactPosition FROM People;", cn)
        Dim ds As New DataSet
    
        da.Fill(ds)
    
        Dim dv As DataView
    
        dv = ds.Tables(0).DefaultView
    
        dv.Sort = "PersonName"
    
        ' The following perhaps come from a textbox etc.
        ' in a real application
        Dim InsertName As String = "Jane Smith"
        Dim InsertTitle As String = "Manager"
    
        Dim result As Integer = dv.Find(InsertName)
        If result = -1 Then
            Console.WriteLine("Adding")
            cn.Open()
            Dim cmd As New OleDb.OleDbCommand With _
                {.CommandText = _
                  <SQL>
                     INSERT INTO People 
                            (PersonName, ContactPosition) 
                             VALUES (<%= InsertName.SingleQuotes %>, 
                                          <%= InsertTitle.SingleQuotes %>)
                    </SQL>.Value, _
                     .Connection = cn}
                cmd.ExecuteNonQuery()
        End If
    End Using

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VS2008 and higher:: Curiosity

    Your first extension has a flaw. Won't work if the string isn't a number. Will throw an error. Would be better to use the TryParse method instead, then return a default value if the string fails conversion.

    No comment on the second one as I normally get my data from the underlying data rather than the grid itself.

    Lastly, I use a lot of LINQ. It's a real time saver. I've converted previous loops that used the .Select method of the datatable, rolled them over into their LINQ equivalents, and shaved off considerable amounts of time. Mostly I use LINQ to Object. Not 100&#37; sold on LINQ to SQL directly. Two things in LINQ that I take the most advantage of: 1) Lambdas... I use a lot of Lambdas to select, transform, and perform calculations on my data. 2) LINQ to XML - being able to transform my datatable into an XML document... along with using Lambdas to control what data is selected... Now I can transform my data into an XML document, and send it all over to my sproc all in one chunk, rather than making repeated calls to the sproc for each row. Yet another performance gainer.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: VS2008 and higher:: Curiosity

    Thanks for your reply.

    In the first extension you are right if you do not know what you are consuming. I am using mine without any issues as I know what I am asking for and why there is no exception or assertion. usually if I am up against unknown types I will not use an extension and will use logic with TryCast but not within an extension as I like extensions to be fairly open and not constrained.

    Lastly I was not looking for comments on the extensions shown but instead extensions in general along with LINQ.

    Quote Originally Posted by techgnome View Post
    Your first extension has a flaw. Won't work if the string isn't a number. Will throw an error. Would be better to use the TryParse method instead, then return a default value if the string fails conversion.

    No comment on the second one as I normally get my data from the underlying data rather than the grid itself.

    Lastly, I use a lot of LINQ. It's a real time saver. I've converted previous loops that used the .Select method of the datatable, rolled them over into their LINQ equivalents, and shaved off considerable amounts of time. Mostly I use LINQ to Object. Not 100% sold on LINQ to SQL directly. Two things in LINQ that I take the most advantage of: 1) Lambdas... I use a lot of Lambdas to select, transform, and perform calculations on my data. 2) LINQ to XML - being able to transform my datatable into an XML document... along with using Lambdas to control what data is selected... Now I can transform my data into an XML document, and send it all over to my sproc all in one chunk, rather than making repeated calls to the sproc for each row. Yet another performance gainer.

    -tg

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: VS2008 and higher:: Curiosity

    I'm very much with techgnome. I use a lot of LINQ to Object, and don't see LINQ to SQL as worthwhile at all; primarily because only MSSQL supports it; and it's typically the last database I'm programming for.

    I use it to sort and aggregate a lot of data though. The aggregation is where it saves me the most time as I can select and aggregate a collection of objects in one statement. I also use a lot of lambdas too in my code.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: VS2008 and higher:: Curiosity

    Linq to 'stuff' and lambdas, yes. It's a great addition to the language.

    Linq to Sql/Entity Framework, yes, but it's not a joy - in the end, they're crap for performance-intensive apps.

    Extension methods, only sometimes. It's easy to get carried away with extension methods but other developers will lose visibility of what's going on. I don't find that extension methods make maintenance easier.

  6. #6

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: VS2008 and higher:: Curiosity

    Quote Originally Posted by Jenner View Post
    I'm very much with techgnome. I use a lot of LINQ to Object, and don't see LINQ to SQL as worthwhile at all; primarily because only MSSQL supports it; and it's typically the last database I'm programming for.

    I use it to sort and aggregate a lot of data though. The aggregation is where it saves me the most time as I can select and aggregate a collection of objects in one statement. I also use a lot of lambdas too in my code.
    Thanks for your comments. I am with you on LINQ to SQL as my environment is mainly IBM-DB2. Also agree with the use of Lambda but have found it to be a struggle to get other developers (many are short timers) to use Lambda even after they have seen how it can make life easier.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VS2008 and higher:: Curiosity

    I resisted Lambas at first because I didn't see the point... and I didn't understand them. Fortunately I found myself in a position where I finally understand how they work and make my life easier, I get the point. Now I'm not sure how I ever got along w/o them.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width