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
DavePosted May 28, 2008, 7:25 PM
Note: I'd be inclined to use a List, rather than redimensioning arrays.
Scott LyslePosted May 28, 2008, 9:15 AM
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;
}
}