开发者

Converting VbScript functions (Right, Len, IsNumeric, CInt) in C#

开发者 https://www.devze.com 2023-04-12 22:45 出处:网络
Again, I have got below code in VbScript, can you please suggest what will be equivalent code in C#. Function GetNavID(Title)

Again, I have got below code in VbScript, can you please suggest what will be equivalent code in C#.

Function GetNavID(Title)
    getNavID=UCase(Left(Title, InStr(Title, ". ") -1))
End Function

I have already got above code change from my last question i.e.

public static string GetNavID(string Title)
{
    int index = Title.IndexOf(". ");
    return Title.Substring(0, index - 1).ToUpper();
} 

Now I want to convert below code also 开发者_运维技巧in c#,as there are lots of VBScript functions, so getting confused.

Dim NavigationId 'As String

NavigationId = GetNavID(oPage.Title)

' Is it a subnavigation member page ?
If Left(NavigationId, 1) = "S" Then
    NavigationId = Right(NavigationId, Len(NavigationId) - 1)           
    If IsNumeric(NavigationId) Then
        ' Its a subnavigation non-index page "Sxxx"
        If CInt(NavigationId) > 0 Then

        End If
    End If
End If  

Please suggest!!


Try:

if (NavigationId.StartsWith("S"))
{
    NavigationId = NavigationId.Substring(1);
    int id;
    if (int.TryParse(NavigationId,out id))
    {
        if (id > 0)
        {
        }
    }
}


if (NavigationId.StartsWith("S"))
{
    NavigationId = NavigationId.TrimStart("S");
    int temp = 0;
    if (int.TryParse(NavigationId, out temp))
    {
        if (temp > 0)
        {
            //Do something
        }     
    } 
}


If Left(NavigationId, 1) = "S" Then
    NavigationId = Right(NavigationId, Len(NavigationId) - 1)           
    If IsNumeric(NavigationId) Then
       ' Its a subnavigation non-index page "Sxxx"
       If CInt(NavigationId) > 0 Then

       End If
    End If
End If

Translates into:

if(NavigationId.StartsWith("S")) {
    NavigationId = NavigationId.Substring(1); 
    int navId;
    if(Int32.TryParse(NavigationId, out navId) && navId > 0) {
       // Do what you need to do.
    }
}

However, you should look into the string manipulation workings of both languages.


You should probably put some elses on the ifs and throw some exceptions

string navigationIdString = GetNavID(oPage.Title)

if (navigationIdString.StartWith("S"))
{
    var navigationID = 0;
    if (int.TryParse(navigationIdString.SubString(1), navigationID)
    {
         if(navigationID > 0)
         {
             ...
         } 
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消