0

Having a bit of trouble following VB.NET to C# conversion. It was an automatic conversion. I'm currently taking a crash course in C# so there's a lot I'm unsure on.

The below code essentially takes a table apptableoriginal and copies it apptablecopy. However it first checks that ICON exists in original table. If not, sets it to an empty string.

However where row is referenced (e.g. row("ICON"), row("NAME')) I'm getting a Method Name Expected error. From searching online I though it might be a case of replacing the round brackets with square brackets, but this doesn't seem to be the case.

            apptablecopy = new DataTable();
            apptablecopy.Columns.Add("ICON", typeof(string));
            apptablecopy.Columns.Add("NAME", typeof(string));

            foreach (var row in apptableoriginal.Rows)

            {
                string icon;

                try
                {
                    icon = row("ICON");
                }

                catch
                {
                    icon = "";
                }

                apptablecopy.Rows.Add(icon, row("NAME"));
            }
  • 1
    Change `()` to `[]`. – mjwills Dec 23 '20 at 11:40
  • 1
    `From searching online I though it might be a case of replacing the round brackets with square brackets, but this doesn't seem to be the case.` Why do you think that? What happened when you tried? – J... Dec 23 '20 at 11:40
  • When I replace with square brackets I get a `CS0021: Cannot apply indexing with [] to an expression of type 'object'` error. –  Dec 23 '20 at 11:43
  • 2
    `var row in apptableoriginal.Rows.OfType()` or `DataRow row in apptableoriginal.Rows` – mjwills Dec 23 '20 at 11:44
  • Ah perfect. Adding that in along with the square brackets has worked. Do you mind explaining why it wasn't working without the `.ofType()`? I'm only just starting to learn C# so it's all new to me. –  Dec 23 '20 at 11:46
  • 1
    Check the duplicate @AaronWright. Short answer - DataTable was written before generics, so its enumerator doesn't say that its individual rows are `DataRow` - so you need to "remind" it. – mjwills Dec 23 '20 at 11:49
  • Thanks @mjwills. That helps a lot. I'll have a read through the duplicate as well. –  Dec 23 '20 at 11:51
  • 1
    Banish `var` from your code and you'll be happier for it. Unless you're explicitly using anonymous types it's just a lazy crutch that makes code harder to read and prone to buggy behaviour. – J... Dec 23 '20 at 12:12
  • Thanks @J.... I'll bear that in mind going forward. Only just started learning C#. –  Dec 23 '20 at 12:22
  • Ignore @J... - `var` is not a crutch at all. The issue here isn't `var` - the issue is that `DataRowCollection` is old and crap. If I saw that code the key advice I wouldn't give is "stop using `var`" It would be "stop using `DataTable`". Check out Dapper. – mjwills Dec 23 '20 at 12:25
  • @mjwills DataTable may be old, but it's insanely useful and very widely used. The c# universe is filled with code that is far from perfect. `var` will help you discover this the hard way. Fine, if that's what you're into, but you're effectively putting trust and responsibility onto other people when you use it. Just because you have airbags doesn't mean you shouldn't wear a seatbelt. – J... Dec 23 '20 at 12:33
  • Thanks for your insights @J... I've been coding for a few years (cough, cough) in C#. I am aware of its pros and cons. I certainly wouldn't encourage warning new C# developers against it. – mjwills Dec 23 '20 at 12:35
  • @mjwills You're suggesting that OP refactor their entire application to switch to a different technology entirely simply because it will let them save five keystrokes and save them the onerous burden of actually knowing what type they are coding against. Surely if you're an experienced developer you're aware of the careful tradeoffs we have to make when managing a codebase. This seems a weak reason to undertake a massive rewrite of an application, and generally poor advice from a business case perspective. – J... Dec 23 '20 at 12:39
  • `"stop using DataTable". Check out Dapper` - I mistook that to be a suggestion that OP rewrite their application to avoid `System.Data` and to use Dapper instead. Apologies. – J... Dec 23 '20 at 12:41
  • Thanks for the input (both of you). I'll continue learning C# and take all of this into consideration –  Dec 23 '20 at 12:51

0 Answers0