On 6/12/2013 10:35 PM, Brian wrote:
On 6/12/2013 10:06 AM, Brian wrote:
On 6/9/2013 11:07 AM, Brian wrote:
First of all I'm a Visual Basic programmer who is wondering if C#
can get
over some of the limits of Visual Basic so I hope I can make
myself clean
on what I would like to know.
Do you need to define a space for variables in C# such as Dim
var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?
If you have 12 items but have only dimensioned variable to
contain 10
items then is the variable dimension automatically adjusted to
hold more
data in C# ?
Not for arrays but collections has this ability.
string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";
but:
List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");
How do you access the aaa that has been stored? Can it be searched
for?
You can access as:
lst[0]
lst[1]
There is a ton of ways to search in and manipulate List<>.
If you explain how you want to search, then I can create an
example.
A search in VB Net would normally involve a look and a comparing two
variables.
For example
For X = 1 to MaxObjects
If ObjectTest = ObjectName(X) Then
Carryout_Actions
Exit For
Next X
In C# and VB.NET you could use the Find method with
a delegate/lambda that compares a property of each value with
what you are looking for.
Example:
using System;
using System.Collections.Generic;
namespace E
{
public class Data
{
public int A { get; set; }
public string B { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
List<Data> lst = new List<Data>();
lst.Add(new Data { A=123, B="ABC" });
lst.Add(new Data { A=456, B="DEF" });
Data d123 = lst.Find(d => d.A==123);
Console.WriteLine(d123.B);
Data ddef = lst.Find(d => d.B=="DEF");
Console.WriteLine(ddef.A);
Console.ReadKey();
}
}
}
Translated to VB.NET:
Imports System
Imports System.Collections.Generic
Namespace E
Public Class Data
Public Property A() As Integer
Get
Return m_A
End Get
Set
m_A = Value
End Set
End Property
Private m_A As Integer
Public Property B() As String
Get
Return m_B
End Get
Set
m_B = Value
End Set
End Property
Private m_B As String
End Class
Public Class Program
Public Shared Sub Main(args As String())
Dim lst As New List(Of Data)()
lst.Add(New Data() With { .A = 123, .B = "ABC" })
lst.Add(New Data() With { .A = 456, .B = "DEF" })
Dim d123 As Data = lst.Find(Function(d) d.A = 123)
Console.WriteLine(d123.B)
Dim ddef As Data = lst.Find(Function(d) d.B = "DEF")
Console.WriteLine(ddef.A)
Console.ReadKey()
End Sub
End Class
End Namespace
Arne