Vb转换为C#of Strings.InStr [英] Vb Conversion to C# of Strings.InStr

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

问题描述

你好



我正在将VB代码转换为C#并面临一些下面列出的错误



以下是我的VB代码



Hello

Am doing a VB code conversion into C# and facing some below listed errors

Below is my VB 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#CODE






Here is my C# CODE

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;
   }



将VB转换为C#之后的代码是错误:


after conversion VB to C# of the code below is the error:

if (String.InStr(pathReader["link"], filename) == 0 | string.IsNullOrEmpty(_parent_Nav_Area))




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



如果我将此Strings关键字替换为String,则其相关函数的InStr会出错:字符串不包含'InStr'的定义,我知道这个'InStr'在C#中无法访问的事实,任何人都可以建议一个替代方案..


If I replace this Strings keyword to String then the InStr which is its associated fuctions gets error: String does not contain a definition of 'InStr', I known to this fact that 'InStr' is not accessible in C# can anyone please suggest an alternative for the same..

推荐答案

你可以使用IndexOf [ ^ ]字符串方法。

但您也可以使用visualbasic原始例程添加对Microsoft.VisualBasic的引用,并处理非零的索引。您可能需要阅读 this [ ^ ]。
You can use IndexOf[^] method of string.
But you can use also visualbasic original routines adding a reference to Microsoft.VisualBasic and taking care of indexes that are not zero based. You may want read this[^].


我已在这里回答了这个问题:将vb.net转换为c# [ ^ ]
I already answered this question here: conversion of vb.net to c#[^]


您可以随时自动翻译代码(而非转换!)。请看我过去的答案:

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



最可靠,最优质的方法是使用开源的ILSpy。



Maciej Los在他最近的回答中得到了很好的建议:停止这样做!请遵循这个建议。

此外,停止重新发布相同的问题;这被视为滥用。



-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.

You got a very good advice by Maciej Los in his recent answer: stop doing it! Do follow this advice.
Also, stop re-posting the same question; this is considered as abuse.

—SA


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

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