reDim vars(0).VarValue(3)
vars(0).VarValue(0) = New Object
vars(0).VarValue(1) = New Object
vars(0).VarValue(2) = New Object
Another alternative in VB.net to do this is
vars(0).VarValue = New Object() {CObj("qwer"), CObj("asdf")}
but I haven't found how to do in C#
I have try in C#
vars[0].VarValue = new Object()[3];
But evidently the type of object insn't resized to Object[3], an when i try to access
Vars[0].VarValue[0] = "asdf"
I get the error "Error Cannot apply indexing with [] to an expression of type 'object' "
Anybody knows how to do this in C# ?
I believe that the problem is C# can't do dynamics resize.
Thanks for your help in advance

Scott LysleRank #49 · 31K reputationPosted May 28, 2008, 5:15 AM EDT
I would take a look at using a typed list; you can create a list (List) where 'T' is the type of object you plan to store in the list and then you can dynamically add objects to the list and retrieve them or iterate through the list; here is an example, in it there is a class called Employee that holds employee information; when the application runs, a List is created, three employees are then created and added to the list, next the code iterates through the list to report on all of the employees, and last, using LINQ to Objects, the code finds a single employee (#2) and reports that employee's first and last name:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Junk
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<Employee> employees = new List<Employee>();
Employee e1 = new Employee();
e1.FirstName = "Harry";
e1.MiddleName = "Wilson";
e1.LastName = "Anderson";
e1.EmployeeNumber = 1;
employees.Add(e1);
Employee e2 = new Employee();
e2.FirstName = "Wallace";
e2.MiddleName = "Authur";
e2.LastName = "Slawson";
e2.EmployeeNumber = 2;
employees.Add(e2);
Employee e3 = new Employee();
e3.FirstName = "Debra";
e3.MiddleName = "Michelle";
e3.LastName = "Carmichael";
e3.EmployeeNumber = 3;
employees.Add(e3);
// iterate
foreach (Employee e in employees)
{
StringBuilder sb = new StringBuilder();
sb.Append(e.FirstName + " " + e.MiddleName + " "
+ e.LastName + Environment.NewLine);
sb.Append("Employee Number: " + e.EmployeeNumber.ToString());
MessageBox.Show(sb.ToString(), "Employee Number " +
e.EmployeeNumber);
}
// search (using Linq to Objects)
var q =
(from e in employees
where e.EmployeeNumber == 2
select e).SingleOrDefault<Employee>();
MessageBox.Show(q.EmployeeNumber + " " + q.FirstName + " " +
q.LastName, "Search");
}
}
public class Employee
{
public string FirstName;
public string MiddleName;
public string LastName;
public int EmployeeNumber;
}
}
Dave131 reputationPosted May 28, 2008, 3:25 PM EDT
using System; class App { static void Main() { // Construct an array of 3 elements Int32[] arr = new Int32[] { 1 , 2, 3 }; // Display all the elements in the array foreach (Int32 x in arr) Console.Write(x + " "); Console.WriteLine(); // Redimension the array so that it now contains 5 elements arr = (Int32[]) Redim(arr, 5); // Display all the elements in the array foreach (Int32 x in arr) Console.Write(x + " "); Console.WriteLine(); // Redimension the array so that it now contains 2 elements arr = (Int32[]) Redim(arr, 2); // Display all the elements in the array foreach (Int32 x in arr) Console.Write(x + " "); } public static Array Redim(Array origArray, Int32 desiredSize) { // Determine the types of each element Type t = origArray.GetType().GetElementType(); // Construct a new array with the desired number of elements // Each element is the same type as was in the original array Array newArray = Array.CreateInstance(t, desiredSize); // Copy the element from the original array in to the new array Array.Copy(origArray, 0, newArray, 0, Math.Min(origArray.Length, desiredSize)); // Return the new array return newArray; } }Note: I'd be inclined to use a List, rather than redimensioning arrays.