Converting C+ to VB and Back Again

In the continuing series of articles that I have been writing in support of “Issues when trying to use multipart-form data with Jquery AJAX forms”, I wanted to address the challenges of using online code samples. I am a strong proponent of not typing code if I don’t have to. There are a lot of great sites out there that provide code snippets that can be used over and over again. Sites like this one or this one can be tremendous asset to any developer.

Inevitably you will come across a piece of code that you really want to use but it’s in a different language. Specifically I am talking about VB.net and C+ code. Most of us know that although these languages are similar, sometimes there are little (and large) nuances that can cause some pain. You could print out the C+ code or the VB.net code and manually convert it or you try several online conversion tools. If a simple- copy, paste and process click can save me time, than why not? At the end of the day you still have to understand the code because as good as they all might, there is always little changes that must be resolved manually. Take for example this block of code that I used for this project.

codeconvert1

I could retype it into VB but why should I do this when I can convert it online. Each one has a little different type of conversion process and I have used all three at some point.

Translator #1
http://www.carlosag.net/Tools/CodeTranslator/
translate1

Translator #2
http://www.developerfusion.com/tools/convert/csharp-to-vb
translate2

Translator #3
http://converter.telerik.com/
translate3

In the end you will have to determine which one translated better and then make your changes from there-to get this.

<HttpPost> _
    Public Function PostValue() As Task(Of HttpResponseMessage)

        ' Check if the request contains multipart/form-data.
        If Not Request.Content.IsMimeMultipartContent() Then
            Throw New HttpResponseException(HttpStatusCode.UnsupportedMediaType)
        End If

        Dim provider = New InMemoryMultipartFormDataStreamProvider()
        Dim test As System.IO.Stream
        Dim task1 = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(Of HttpResponseMessage) _
                  (Function(t)
                       Try
                           'access form data
                           Dim formdata As NameValueCollection = provider.FormData

                           'access files
                           Dim files As IList(Of HttpContent) = provider.Files

                           'Example: reading a file's stream like below
                           Dim file1 As HttpContent = files(0)
                           Dim file1Stream As Stream = file1.ReadAsStreamAsync().Result

                       Return resp
      		   Catch ex As Exception

                           Dim tr As String = ex.Message

                       End Try

                   End Function)

     Return task1
    End Function

It general these conversion tools are good at giving you a base start but in the end you still have to take the time to understand what you are doing.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under 3rd Party Software, ASP .NET 4.0, Internet, Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + 8 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>