1

I used a popular online code converter to go from VB to C# and the C# is not working on this for loop. Can any C# experts see the problem?

More specifically intellisense is telling me that CustomerRoles() cannot be used like a method. It works fine in VB.

VB:

 Dim Roles As New List(Of String)
 For x As Integer = 0 To EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles().Count - 1
     Roles.Add(EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles(x).Name)
     ddlRoles.Items.Add(EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles(x).Name)
 Next

C# (this is not working) :

List<string> Roles = new List<string>();
for (int x = 0; x <= EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerRoles().Count - 1; x++) {
  Roles.Add(EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerRoles(x).Name);
  ddlRoles.Items.Add(EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerRoles(x).Name);
}
PaulG
  • 13,871
  • 9
  • 56
  • 78
mlg74
  • 520
  • 1
  • 7
  • 27

2 Answers2

7

Replace

CustomerRoles(x).Name

with

CustomerRoles[x].Name

You access the indexer of arrays (or lists) with squared brackets instead of paranthesis in C#.

If it's not a collection but a property or field you just have to remove the paranthesis:

CustomerRoles.Name

If that doesn't work(for whatever reason) you can try Enumerable.ElementAt:

CustomerRoles.ElementAt(x).Name

That works with any kind of IEnumerable<T> even if it doesn't implement IList<T>(needed for the indexer).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

Try this:

C#(this is not working)
List<string> Roles = new List<string>();
for (int x = 0; x <= EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerRoles.Count - 1; x++) {
    Roles.Add(EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerRoles[x].Name);
    ddlRoles.Items.Add(EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerRoles[x].Name);
}

Ok i've tried in VB CustomerRoles(x) is equivalent to CustomerRoles.ElementAtOrDefault(x); in c#.

Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
  • i get error cannot apply indexing with [] to an expression of type system.collections.generic.icollection – mlg74 Sep 02 '13 at 15:16
  • I think it was a `IList`. In VB you can access the elements by index in a collection??? Can you change it's type to IList ??? – Alessandro D'Andria Sep 02 '13 at 15:18
  • How do I change it? IList, Roles = newe IList();?? – mlg74 Sep 02 '13 at 15:25
  • I see, but it says i may be missing an assemble or reference – mlg74 Sep 02 '13 at 15:29
  • `Using System.Linq` in using directives it's all you need. – Alessandro D'Andria Sep 02 '13 at 15:31
  • _"Ok i've tried in VB CustomerRoles(x) is equivalent to CustomerRoles.ElementAtOrDefault(x); in c#"_ It is _not_ equivalent, you can use `ElementAt`(or `ElementAtOrDefault`) also in VB.NET. It is just a more dynamic way to get an element at a specified index. If it's an `IList`(like array or list) the indexer is used, otherwise `ElementAt` will enumerate the sequnce to find the element. – Tim Schmelter Sep 02 '13 at 20:24
  • in VS2012 intellisense show me like it was the `ElementAtOrDefault` extension method. Have you found some link that explain how work in VB? – Alessandro D'Andria Sep 02 '13 at 20:43
  • Ok after some search I found this http://stackoverflow.com/questions/15864783/why-can-i-apply-an-indexer-to-an-icollection-in-vb-net-but-not-in-c-sharp. – Alessandro D'Andria Sep 03 '13 at 07:20