将vb.net转换为c# [英] conversion of vb.net to c#

查看:75
本文介绍了将vb.net转换为c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......我又回来了......我仍然有一些关于vb.net代码转换为c#的问题。



这是我的vb.net代码

Hi guys...i m back again....i still have some issues regarding code conversion of vb.net to c#.

here is my vb.net code

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
       Page.Response.Expires = -2400

       If Page.Session("auth_EmployeeID") <> Convert.ToInt32(Page.User.Identity.Name) Then Call Reset_Session_Data(Page)

       If _restrictto_groupid <> 0 Then
           If InStr(Session("user_groups"), "~" & _restrictto_groupid & "~") = 0 Then
               'USER DOES NOT HAVE RIGHTS FOR THIS PAGE
               Server.Transfer("/access_denied.aspx")
           End If
       End If

       Dim filename As String
       Dim fileandquery As String

       fileandquery = Page.Request.Path & "?index=5" 'System.IO.Path.GetFileName(Request.Path) & "?index=5"
       Dim query_pos As Integer
       query_pos = InStr(fileandquery, "?")
       If query_pos <> 0 Then
           filename = Left(fileandquery, query_pos - 1)
       Else
           filename = fileandquery
       End If

       If _parent_Nav_Area <> "" Then filename = _parent_Nav_Area

       'Page.Trace.Write("navfix",filename)

       Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
       Dim command As New SqlCommand("sproc_CP_Navigation_PathGet", conn)
       command.CommandType = CommandType.StoredProcedure

       Dim param As SqlParameter = command.Parameters.Add("@filename", SqlDbType.VarChar, 100)
       param.Value = filename

       conn.Open()
       Dim pathReader As SqlDataReader = command.ExecuteReader()
       nav_path.Text = ""

       Dim access_granted As Boolean = False

       If pathReader.HasRows Then
           Do While pathReader.Read()
               If pathReader.Item("depth") <> 1 Then
                   nav_path.Text &= " :: "
               Else
                   nav_path.Text &= "> "
               End If

               access_granted = False
               If pathReader.Item("security_group_required") Is DBNull.Value Then
                   access_granted = True
               Else
                   access_granted = Check_GroupMembership(pathReader.Item("security_group_required"))
               End If

               If Not (pathReader.Item("link") Is DBNull.Value) And access_granted Then
                   If InStr(pathReader.Item("link"), filename) = 0 Or _parent_Nav_Area <> "" Then
                       nav_path.Text &= "<a href=""" & Page.Request.ApplicationPath & "flashnav/" & pathReader.Item("link") & """>" & pathReader.Item("label") & "</a>"
                   Else
                       nav_path.Text &= pathReader.Item("label")
                   End If
               Else
                   nav_path.Text &= pathReader.Item("label")
               End If
           Loop
       End If

       pathReader.Close()
       conn.Close()
       pathReader = Nothing
       command = Nothing
       conn = Nothing
   End Sub





这是我转换的c#代码(使用在线转换工具) -





And here is my converted c# code (using online conversion tool) -

protected void Page_Load(object sender, EventArgs e)
   {
       Page.Response.Expires = -2400;

       if (Page.Session["auth_EmployeeID"] != (Page.User.Identity.Name).ToString())
       {
           Reset_Session_Data(Page);
       }

       if (_restrictto_groupid != 0)
       {
           if (String.InStr(Session["user_groups"], "~" + _restrictto_groupid + "~") == 0)
           {
               //USER DOES NOT HAVE RIGHTS FOR THIS PAGE
               Server.Transfer("access_denied.aspx");
           }
       }

       string filename = null;
       string fileandquery = null;

       fileandquery = Page.Request.Path + "?index=5";
       //System.IO.Path.GetFileName(Request.Path) & "?index=5"
       int query_pos = 0;
       query_pos = String.InStr(fileandquery, "?");
       if (query_pos != 0)
       {
           filename = String.Left(fileandquery, query_pos - 1);
       }
       else
       {
           filename = fileandquery;
       }

       if (!string.IsNullOrEmpty(_parent_Nav_Area))
           filename = _parent_Nav_Area;

       //Page.Trace.Write("navfix",filename)

       SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
       SqlCommand command = new SqlCommand("sproc_CP_Navigation_PathGet", conn);
       command.CommandType = CommandType.StoredProcedure;

       SqlParameter param = command.Parameters.Add("@filename", SqlDbType.VarChar, 100);
       param.Value = filename;

       conn.Open();
       SqlDataReader pathReader = command.ExecuteReader();
       nav_path.Text = "";

       bool access_granted = false;

       if (pathReader.HasRows)
       {
           while (pathReader.Read())
           {
               if (Convert.ToInt32(pathReader["depth"]) != 1)
               {
                   nav_path.Text += " :: ";
               }
               else
               {
                   nav_path.Text += "> ";
               }

               access_granted = false;
               if (object.ReferenceEquals(pathReader["security_group_required"], DBNull.Value))
               {
                   access_granted = true;
               }
               else
               {
                   access_granted = Check_GroupMembership(pathReader["security_group_required"]);
               }

               if ((!object.ReferenceEquals(pathReader["link"], DBNull.Value)) & access_granted)
               {
                   if (String.InStr(pathReader["link"], filename) == 0 | !string.IsNullOrEmpty(_parent_Nav_Area))
                   {
                       nav_path.Text += "<a href="\""" page.request.applicationpath="">" + pathReader["label"] + "</a>";
                   }
                   else
                   {
                       nav_path.Text += pathReader["label"];
                   }
               }
               else
               {
                   nav_path.Text += pathReader["label"];
               }
           }
       }
       pathReader.Close();
       conn.Close();
       pathReader = null;
       command = null;
       conn = null;
   }

这是错误---



Here is the error ---

The name ' Strings' does not exists in current context.





如果我将此Strings关键字替换为String,那么InStr这是它的相关函数获取错误:



If I replace this Strings keyword to String then the InStr which is its associated functions gets error:

String does not contain a definition of InStr





我知道这个事实是在C#中无法访问 InStr 。任何人都可以建议一个替代方案。

,

I known to this fact that InStr is not accessible in C#. Can anyone please suggest an alternative for the same.

推荐答案

你总是可以自动翻译代码(而不是转换!)。请看我过去的答案:

代码解释,C#到VB.NET [ ^ ]。



最可靠,最优质的方法是使用开源的ILSpy。< br $> b $ b

-SA
You can always translate code (not "convert"!) automatically. Please see my past answer:
Code Interpretation, C# to VB.NET[^].

Most reliable and quality method is using open-source ILSpy.

—SA


请参阅: String.IndexOf [ ^ ] - 返回第一次出现的从零开始的索引此实例中指定的字符串,根据 InStr [ ^ ]。



注意: 你应该停止这样做! 尝试理解代码的作用并按照你的方式编写。你永远不会学到任何东西!即使VB.NET和C#非常相似,也会有很多不同之处!
See this: String.IndexOf[^] - returns zero-based index of the first occurrence of the specified string in this instance, as per InStr[^].

Note: you should stop doing it! Try to understand what code does and write it in your way. You'll never learn anything! Even if VB.NET and C# are quite similiar, there's a lot of differences!


这篇关于将vb.net转换为c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆