代码之家  ›  专栏  ›  技术社区  ›  code4life

这个Excel VBA图形代码的C#等价物是什么?

  •  3
  • code4life  · 技术社区  · 16 年前

    这是Excel模板的VBA代码,我正在处理的VSTO项目中尝试将其转换为C。顺便说一下,这是一个VSTO插件:

    Dim addedShapes() As Variant
    ReDim addedShapes(1)
    addedShapes(1) = aBracket.Name
    
    ReDim Preserve addedShapes(UBound(addedShapes) + 1)
    addedShapes(UBound(addedShapes)) = "unique2"
    
    Set tmpShape = Me.Shapes.Range(addedShapes).Group
    

    addedShapes() ,不知道这是怎么回事。

    更新: 马蒂提到过 表示VBA中的变量数组。所以现在我想知道 添加形状() 应该是。这是在C#中调用Shapes.Range()调用的正确方法吗?

    List<string> addedShapes = new List<string>();
    ...
    Shape tmpShape = worksheet.Shapes.get_Range
      (addedShapes.Cast<object>().ToArray()).Group();
    

    2 回复  |  直到 8 年前
        1
  •  2
  •   Matti Virkkunen    16 年前

    我不知道你真正的问题是什么,但是 addedShapes 是一个数组。在VB及其变体中,数组是使用 () 而不是 [] .

    而且,您的代码看起来只是一种冗长的工作方式:

    object[] addedShapes = new object[] { aBracket.Name, "unique2" };
    Shape tmpShape = worksheet.Shapes.get_Range(addedShapes).Group();
    

    Shape tmpShape = worksheet.Shapes[addedShapes].Group();
    

    看看哪个有用。我真的不知道MSDN建议哪一个。

        2
  •  2
  •   Josh Sterling    16 年前

    请原谅c风格的注释,vb风格的语法不是很好。

    //This declares an array of variants but does not initialize it.
    Dim addedshapes() As Variant
    
    //Initializes the array with a max index of 1. (insert vb index rant here)
    ReDim addedShapes(1)
    
    //assigns the contents of aBracket.Name to element 1 of the array.
    addedShapes(1) = aBracket.Name 
    
    //increases the size of addedShapes by 1, retaining any values.
    ReDim Preserve addedShapes(UBound(addedShapes) + 1) 
    
    //sets the last element to the string literal
    addedShapes(UBOund(addedShapes)) = "unique2" 
    
    //Not sure here because I havent done any VBA in a loooong time,
    //but anyway it's passing the array.
    set tmpShape = Me.Shapes.Range(addedShapes).Group 
    

    Variant 只是一个可以容纳任何数据类型、int、floats、objects等的惰性结构,所以在.Net中最直接的比较应该是对象的集合/数组。不过,如果你知道里面有什么,那么最好把收藏限制在这个范围内。所以不是 List<object> 你会用 List<Class> List<BaseClass> 或 List<ISomeInterface>

    推荐文章