如何将类别名称转换为猫ID [英] how do i convert category name to cat id

查看:89
本文介绍了如何将类别名称转换为猫ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将类别名称转换为类别ID?我正在编写的代码给我带来了问题.

这是代码:

Hi, how do I convert category name to category id? The code that I am writing is giving me problem.

This is the code:

''this to convert category name to category id
    Private Function getCatId(ByVal cat As String) As Integer
        Dim preheader() As String = Session("preheader") ''start header
        Dim trgheader() As String = Session("trgheader") ''trailing header
        Dim returnVal As Integer ''the value to return = id
        ''for loop  from 0 to total header element - 1
        For i As Integer = 0 To preheader.Length - 1 Step 1
            Dim header As String = preheader(i) & trgheader(i) ''join up start header and trg header
            header = header.Replace(" ", "")
            ''if the passed cat and the header, which is join up by start header and trg header, is equal
            ''then return i as the id, so it set the returnVal to i
            If (String.Equals(cat, header)) Then
                returnVal = i
            End If
        Next
        ''return i as the id
        Return returnVal
    End Function



还有其他转换方法吗?
谢谢



Is there any other way to convert?
Thanks

推荐答案

您可以简单地使用以下代码:
you could simply use this code:
Dim WordIndex = Array.IndexOf(preheader, cat);


但是您需要一个已经将preheader和trgheader组合在一起的数组.

如果继续使用自己的函数,请确保在找到该项目后立即退出搜索,并返回-1表示找不到该项目,如下所示:


but you would need an array where preheader and trgheader are already combined.

If you continue using your own function, be sure you quit searching as soon as you found the item and return -1 to indicate that the item cannot be found, like this:

Private Function getCatId(ByVal cat As String) As Integer
    Dim preheader() As String = Session("preheader") 'start header
    Dim trgheader() As String = Session("trgheader") 'trailing header
    Dim returnVal As Integer 'the value to return = id
    'for loop  from 0 to total header element - 1
    For i As Integer = 0 To preheader.Length - 1 Step 1
        Dim header As String = preheader(i) & trgheader(i) 'join up start header and trg header
        header = header.Replace(" ", "")
        'if the passed cat and the header, which is join up by start header and trg header, is equal
        'then return i as the id, so it set the returnVal to i
        If (String.Equals(cat, header)) Then
            Return i
        End If
    Next
    ' not found
    Return -1
End Function



您也可以使用泛型,这样也会更安全.您将创建一个包含匹配的preheader和trgheader值的类,而不是两个字符串数组.



You could also use generics which would also be more safe. Instead of two arrays of strings you create a class that holds the matching preheader and trgheader value.

class PreTarget
  private id as int
  private _pre as string     ' create properties for these fields
  private _target as string
  Public Property preTarget() As String
   Get
     Return String.Concat(_pre, _target).Replace(" ", string.Empty)
   End Get</pre>
End Class

List<PreTarget> preTargetList = new List<PreTarget>()



http://msdn.microsoft.com/en-us/library/d9hy2xwa% 28VS.80%29.aspx [ ^ ]

http://www.vbdotnetheaven.com/UploadFile/Saurabh.Mishra/GenericsInCsharp211232006000706AM//a> [ ^ ]

祝你好运!



http://msdn.microsoft.com/en-us/library/d9hy2xwa%28VS.80%29.aspx[^]

http://www.vbdotnetheaven.com/UploadFile/Saurabh.Mishra/GenericsInCsharp211232006000706AM/GenericsInCsharp2.aspx[^]

Good luck!


这篇关于如何将类别名称转换为猫ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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