locked
VB To C Conversion RRS feed

  • Question

  • Can anyone convert this to C??:

    Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    Dim targetURL As String

    'Build URL with query string of name/value pairs. UrlEncode for proper

    'URL characters (space and ampersand).

    targetURL = "Target1.aspx?"

    targetURL &= "CustID=" & Server.UrlEncode(txtCustomerID.Text.Trim())

    targetURL &= "&CustName=" & Server.UrlEncode(txtCustomerName.Text.Trim())

    targetURL &= "&ProdID=" & Server.UrlEncode(txtProductID.Text.Trim())

    targetURL &= "&ProdDesc=" & Server.UrlEncode(txtProductDescription.Text.Trim())

    Response.Redirect(targetURL)

    End Sub

    End Class

    • Moved by Peter Ritchie Monday, October 6, 2008 7:35 PM ASP.NET question (Moved from Visual C# Language to Off-Topic Posts (Do Not Post Here))
    Monday, October 6, 2008 10:10 AM

Answers

  • hi

    partial class _Default : System.Web.UI.Page

    {

     

    private void btnSubmit_Click(object sender, System.EventArgs e)

        {

           

            string targetURL = null;

           

           //Build URL with query string of name/value pairs. UrlEncode for proper

           

            //URL characters (space and ampersand).

           

            targetURL = "Target1.aspx?";

           

            targetURL += "CustID=" + Server.UrlEncode(txtCustomerID.Text.Trim());

           

            targetURL += "&CustName=" + Server.UrlEncode(txtCustomerName.Text.Trim());

           

            targetURL += "&ProdID=" + Server.UrlEncode(txtProductID.Text.Trim());

           

            targetURL += "&ProdDesc=" + Server.UrlEncode(txtProductDescription.Text.Trim());

           

            Response.Redirect(targetURL);

           

        }


    Cheers By Anand Ranjan   

    }


    Developer
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 12:14 PM
    • Unmarked as answer by GuyCre8ive Monday, October 6, 2008 12:26 PM
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 2:24 PM
    Monday, October 6, 2008 10:47 AM
  • Hey Anand,
    I'm guessing its not working because you didn't "translate" Handles btnSubmit.Click.

    partial class _Default : System.Web.UI.Page

    {

     public _Default()

    {
        this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
    }

    private
    void btnSubmit_Click(object sender, System.EventArgs e)

        {

           

            string targetURL = null;

           

           //Build URL with query string of name/value pairs. UrlEncode for proper

           

            //URL characters (space and ampersand).

           

            targetURL = "Target1.aspx?";

           

            targetURL += "CustID=" + Server.UrlEncode(txtCustomerID.Text.Trim());

           

            targetURL += "&CustName=" + Server.UrlEncode(txtCustomerName.Text.Trim());

           

            targetURL += "&ProdID=" + Server.UrlEncode(txtProductID.Text.Trim());

           

            targetURL += "&ProdDesc=" + Server.UrlEncode(txtProductDescription.Text.Trim());

           

            Response.Redirect(targetURL);

           

        }


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 12:15 PM
    • Unmarked as answer by GuyCre8ive Monday, October 6, 2008 12:26 PM
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 2:25 PM
    Monday, October 6, 2008 11:38 AM
  • Load the VB project in SharpDevelop, verify that it runs correctly and convert to C#.  Load the C# project in Visual Studio and correct the errors until it runs correctly.
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 12:15 PM
    • Unmarked as answer by GuyCre8ive Monday, October 6, 2008 12:26 PM
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 2:25 PM
    Monday, October 6, 2008 11:49 AM

All replies

  • hi

    partial class _Default : System.Web.UI.Page

    {

     

    private void btnSubmit_Click(object sender, System.EventArgs e)

        {

           

            string targetURL = null;

           

           //Build URL with query string of name/value pairs. UrlEncode for proper

           

            //URL characters (space and ampersand).

           

            targetURL = "Target1.aspx?";

           

            targetURL += "CustID=" + Server.UrlEncode(txtCustomerID.Text.Trim());

           

            targetURL += "&CustName=" + Server.UrlEncode(txtCustomerName.Text.Trim());

           

            targetURL += "&ProdID=" + Server.UrlEncode(txtProductID.Text.Trim());

           

            targetURL += "&ProdDesc=" + Server.UrlEncode(txtProductDescription.Text.Trim());

           

            Response.Redirect(targetURL);

           

        }


    Cheers By Anand Ranjan   

    }


    Developer
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 12:14 PM
    • Unmarked as answer by GuyCre8ive Monday, October 6, 2008 12:26 PM
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 2:24 PM
    Monday, October 6, 2008 10:47 AM
  • Hmm, that's not working.  Here's the whole VB Script:
    http://www.getcre8ive.com/StringVB.zip

      I'm trying to convert all of this to C#..
    Monday, October 6, 2008 11:23 AM
  • Sorry but right now I am not able to run the code....

    So can you please provide the problem or error message.

    Thanks and Regards Anand Ranjan
    Developer
    Monday, October 6, 2008 11:31 AM
  • Hey Anand,
    I'm guessing its not working because you didn't "translate" Handles btnSubmit.Click.

    partial class _Default : System.Web.UI.Page

    {

     public _Default()

    {
        this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
    }

    private
    void btnSubmit_Click(object sender, System.EventArgs e)

        {

           

            string targetURL = null;

           

           //Build URL with query string of name/value pairs. UrlEncode for proper

           

            //URL characters (space and ampersand).

           

            targetURL = "Target1.aspx?";

           

            targetURL += "CustID=" + Server.UrlEncode(txtCustomerID.Text.Trim());

           

            targetURL += "&CustName=" + Server.UrlEncode(txtCustomerName.Text.Trim());

           

            targetURL += "&ProdID=" + Server.UrlEncode(txtProductID.Text.Trim());

           

            targetURL += "&ProdDesc=" + Server.UrlEncode(txtProductDescription.Text.Trim());

           

            Response.Redirect(targetURL);

           

        }


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 12:15 PM
    • Unmarked as answer by GuyCre8ive Monday, October 6, 2008 12:26 PM
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 2:25 PM
    Monday, October 6, 2008 11:38 AM
  • It's not doing anything when I click the submit button using that code.  Here's the working code in VB:

    default.aspx
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head runat="server">  
      <title>Source Page</title> 
    </head> 
    <body> 
      <form id="form1" runat="server">  
      <div> 
        <p> 
          <asp:Label ID="labCustID" runat="server" Text="Customer ID:" /> 
          <asp:TextBox ID="txtCustomerID" runat="server"></asp:TextBox> 
        </p> 
        <p> 
          <asp:Label ID="labCustName" runat="server" Text="Customer Name:" /> 
          <asp:TextBox ID="txtCustomerName" runat="server"></asp:TextBox> 
        </p> 
        <p> 
          <asp:Label ID="labProdID" runat="server" Text="Product ID:" /> 
          <asp:TextBox ID="txtProductID" runat="server"></asp:TextBox> 
        </p> 
        <p> 
          <asp:Label ID="labProdDesc" runat="server" Text="Product Description:" /> 
          <asp:TextBox ID="txtProductDescription" runat="server"></asp:TextBox> 
        </p> 
        <p> 
          <asp:Button ID="btnSubmit" runat="server" Text="Submit Data" /></p>  
      </div> 
      </form> 
    </body> 
    </html> 
     

    default.aspx.vb
     
    Partial Class _Default  
        Inherits System.Web.UI.Page  
     
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click  
     
      Dim targetURL As String  
     
      'Build URL with query string of name/value pairs. UrlEncode for proper  
      'URL characters (space and ampersand).  
      targetURL = "Target1.aspx?" 
      targetURL &= "CustID=" & Server.UrlEncode(txtCustomerID.Text.Trim())  
      targetURL &= "&CustName=" & Server.UrlEncode(txtCustomerName.Text.Trim())  
      targetURL &= "&ProdID=" & Server.UrlEncode(txtProductID.Text.Trim())  
      targetURL &= "&ProdDesc=" & Server.UrlEncode(txtProductDescription.Text.Trim())  
     
      Response.Redirect(targetURL)  
     
    End Sub  
     
    End Class  
     

    Target1.aspx
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Target1.aspx.vb" Inherits="Target1" %> 
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head runat="server">  
      <title>Target Page</title> 
    </head> 
    <body> 
      <form id="form1" runat="server">  
      <div> 
        <p> 
          <asp:Label ID="labCustID" runat="server" /> 
        </p> 
        <p> 
          <asp:Label ID="labCustName" runat="server" /> 
        </p> 
        <p> 
          <asp:Label ID="labProdID" runat="server" /> 
        </p> 
        <p> 
          <asp:Label ID="labProdDesc" runat="server" /> 
        </p> 
      </div> 
      </form> 
    </body> 
    </html> 
     

    Target1.aspx.vb
     
    Partial Class Target1  
        Inherits System.Web.UI.Page  
     
      Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender  
     
        'Extract each query string value using variable name after  
        'testing for existence of the variable.  
        If (Request.QueryString("CustID") <> Nothing) Then  
          labCustID.Text = Request.QueryString("CustID").ToString()  
        End If  
        If (Request.QueryString("CustName") <> Nothing) Then  
          labCustName.Text = Request.QueryString(1).ToString()  
        End If  
        If (Request.QueryString("ProdID") <> Nothing) Then  
          labProdID.Text = Request.QueryString("ProdID").ToString()  
        End If  
        If (Request.QueryString("ProdDesc") <> Nothing) Then  
          labProdDesc.Text = Request.QueryString(3).ToString()  
        End If  
     
      End Sub  
     
    End Class  
     
    Monday, October 6, 2008 11:40 AM
  • Load the VB project in SharpDevelop, verify that it runs correctly and convert to C#.  Load the C# project in Visual Studio and correct the errors until it runs correctly.
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 12:15 PM
    • Unmarked as answer by GuyCre8ive Monday, October 6, 2008 12:26 PM
    • Marked as answer by GuyCre8ive Monday, October 6, 2008 2:25 PM
    Monday, October 6, 2008 11:49 AM
  •  Thanks Jannemanrobinson,

    You are right.

    Cheers by anand Ranjan
    Developer
    Monday, October 6, 2008 12:02 PM
  • None of this is working.  I downloaded and installed sharpdevelop, (pretty cool tool) but it's not working after I convert it.  I'm getting an error that the "Method must have a return type" at line 34: public Target1()   Is there an easy way to correct errors or do you pretty much have to know both languages to do so?

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
     
    partial class WebSiteC_Target1 : System.Web.UI.Page  
    {  
     
        protected void Page_PreRender(object sender, System.EventArgs e)  
        {  
     
            //Extract each query string value using variable name after  
            //testing for existence of the variable.  
            if ((Request.QueryString("CustID") != null))  
            {  
                labCustID.Text = Request.QueryString("CustID").ToString();  
            }  
            if ((Request.QueryString("CustName") != null))  
            {  
                labCustName.Text = Request.QueryString(1).ToString();  
            }  
            if ((Request.QueryString("ProdID") != null))  
            {  
                labProdID.Text = Request.QueryString("ProdID").ToString();  
            }  
            if ((Request.QueryString("ProdDesc") != null))  
            {  
                labProdDesc.Text = Request.QueryString(3).ToString();  
            }  
     
        }  
        public Target1()  
        {  
            PreRender += Page_PreRender;  
        }  
     
    }  
     
    Monday, October 6, 2008 12:38 PM
  • Dear there is nothing like for do something in any programming language you have to know each and every language...but yes almost you have to know about the syntax...

    you got the error message "Method must have a return type" at line 34: public Target1()  which is more then sufficient to find...where the problem is and why it happens...

    in Line 34  public Target1()  

    it should be public WebSiteC_Target1()



    because it treats public Target1()   as a method which must have some return type thats why it showing you an error.


    Cheers by Anand Ranjan

    Developer
    Monday, October 6, 2008 1:20 PM
  •  Getting this:  Non-invocable member 'System.Web.HttpRequest.QueryString' cannot be used like a method.

    using Microsoft.VisualBasic;  
    using System;  
    using System.Collections;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Drawing;  
    using System.Diagnostics;  
    using System.Web;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
     
    public partial class WebSiteC_Target1 : System.Web.UI.Page  
    {  
        protected void Page_PreRender(object sender, System.EventArgs e)  
        {  
     
            //Extract each query string value using variable name after  
            //testing for existence of the variable.  
            if ((Request.QueryString("CustID") != null)) {  
                labCustID.Text = Request.QueryString("CustID").ToString();  
            }  
            if ((Request.QueryString("CustName") != null)) {  
                labCustName.Text = Request.QueryString(1).ToString();  
            }  
            if ((Request.QueryString("ProdID") != null)) {  
                labProdID.Text = Request.QueryString("ProdID").ToString();  
            }  
            if ((Request.QueryString("ProdDesc") != null)) {  
                labProdDesc.Text = Request.QueryString(3).ToString();  
            }  
     
        }  
        public WebSiteC_Target1()  
        {  
            PreRender += Page_PreRender;  
        }  
     
    Monday, October 6, 2008 2:19 PM
  • '*****************空间直角坐标---大地坐标  Bow Ring思路算法***************

    Public Sub XYZ_BL(X As Double, Y As Double, Z As Double, BB As Double, LL As Double, h As Double)

    If tq = 0 Then

        a = 6378245 '54椭球参数

        b = 6356863.01877305

        ep = 0.006693421622966

        e1p = 0.006738525414683

    ElseIf tq = 1 Then

        a = 6378140 '75椭球参数

        b = 6356755.28815753

        ep = 0.006694384999588

        e1p = 0.006739501819473

    Else

        a = 6378137   'WGS-84椭球

        b = 6356752.3142

        ep = 0.0066943799013

        e1p = 0.0067394974227

    End If

      Dim m As Double, N1 As Double, r As Double, c As Double, a1 As Double, t As Double, p As Double, q As Double, K As Double

     x = Val(Text1.Text)

    y = Val(Text2.Text)

    z = Val(Text3.Text)

     m = (x ^ 2 + y ^ 2) / a ^ 2: N1 = (1 - ep) * z ^ 2 / a ^ 2: r = (m + N1 - ep ^ 2) / 6: c = m * N1 * ep ^ 2 / 4

     a1 = ((c + r ^ 3) + Sqr(c * (c + 2 * r ^ 3))) ^ (1 / 3) / r

     t = r * (a1 + 1 / a1 + 1)

     q = Sqr(t ^ 2 + m * ep ^ 2): p = ep * (m - t) / q

     K = (ep - p + Sqr((ep - p) ^ 2 + 4 * (q + t))) / 2

     If K < ep Then

      K = (ep + p + Sqr((ep + p) ^ 2 - 4 * (q - t))) / 2

      End If

      n = Sqr(a ^ 2 + z ^ 2 * ep / (K - ep) ^ 2)

     sinbb = z / (n * (K - ep))

     cosbb = Sqr(1 - sinbb ^ 2)

     BB = Atn(sinbb / cosbb)

      LL = Atn(y / x)

     h = Sqr(x ^ 2 + y ^ 2) / Cos(BB) - n

     If BB < 0 Then

       BB = BB + 90

     End If

     LL = angle(LL)

     BB = angle(BB)

    If LL < 0 Then

      LL = LL + 180

    End If

     BB = Format(BB, "######0.0000######")

         LL = Format(LL, "######0.0000######")

         h = Format(h, "######0.0000")

    Text4.Text = BB

    Text5.Text = LL

    Text6.Text = h

    End If

    End Sub

     

     '*************大地坐标———空间直角坐标******************************

    Public Sub BL_XYZ(dB As Double, l As Double, h As Double, X As Double, Y As Double, Z As Double)

    If tq = 0 Then

        a = 6378245 '54椭球参数

        b = 6356863.01877305

        ep = 0.006693421622966

        e1p = 0.006738525414683

    ElseIf tq = 1 Then

        a = 6378140 '75椭球参数

        b = 6356755.28815753

        ep = 0.006694384999588

        e1p = 0.006739501819473

    Else

        a = 63781137   'WGS-84椭球

        b = 6356752.3142

        ep = 0.0066943799013

        e1p = 0.0067394974227

    End If

        n = a / (1 - ep * (Sin(dB)) ^ 2) ^ (1 / 2)    '卯酉圈曲率半径

        X = (n + h) * Cos(dB) * Cos(l)

        Y = (n + h) * Cos(dB) * Sin(l)

        Z = (n * (1 - ep) + h) * Sin(dB)

        X = Format(X, "######0.0000")

        Y = Format(Y, "######0.0000")

        Z = Format(Z, "######0.0000")

    End Sub

     

    '***************平面坐标———空间直角坐标***************************

    Public Sub xyz_XYZ(x1 As Double, y1 As Double, h1 As Double, X As Double, Y As Double, Z As Double)

         Dim b As Double, l As Double, l0 As Double, h As Double, tq As Integer

         Call DadiFs(x1, y1, h1, b, l, l0, h, tq)

         Call BL_XYZ(b, l, h, X, Y, Z)

        

        X = Format(X, "######0.0000")

        Y = Format(Y, "######0.0000")

        Z = Format(Z, "######0.0000")

    End Sub

     

    Wednesday, May 18, 2011 2:55 PM