VB Conversion: 'ReadOnly' Error in VB
-
Hi all, using online converters, the C# snippet
{ this.listView.Items.Add(new ListViewItem(dc.Line) { SubItems = {dc.Sign}, Tag = (object) dc });
gets converted to
Me.listView.Items.Add(New ListViewItem(dc.Line) With { \_ Key .SubItems = {dc.Sign}, \_ Key .Tag = DirectCast(dc, Object) \_ })
Ok, it's obvious that "Key" results in a Syntax Error in VB (amazing why all the converters I tried still put it). But after removing this, I have another error saying "The property 'SubItems' is ReadOnly". Also, IntelliSense doesn't give it as an option in VB, while it does in C#...
My question now: Would it be possible that the property isn't ReadOnly in C#, while it is in VB? Or has it been converted errorous?
Thank you in advance,
Mick -
Hi all, using online converters, the C# snippet
{ this.listView.Items.Add(new ListViewItem(dc.Line) { SubItems = {dc.Sign}, Tag = (object) dc });
gets converted to
Me.listView.Items.Add(New ListViewItem(dc.Line) With { \_ Key .SubItems = {dc.Sign}, \_ Key .Tag = DirectCast(dc, Object) \_ })
Ok, it's obvious that "Key" results in a Syntax Error in VB (amazing why all the converters I tried still put it). But after removing this, I have another error saying "The property 'SubItems' is ReadOnly". Also, IntelliSense doesn't give it as an option in VB, while it does in C#...
My question now: Would it be possible that the property isn't ReadOnly in C#, while it is in VB? Or has it been converted errorous?
Thank you in advance,
MickConverters are not perfect, some are better than others and I find the online ones to be the worst. Why? Because the best converters will compile the code first, get down to MSIL and then convert back up to whatever you want. Online converters typically don't take compiled code so they attempt to do conversions with text replacements. The one you found isn't bad, but it made up the Key part somewhere, your conversion should be:
Me.listView.Items.Add(New ListViewItem(dc.Line) With { _
.SubItems = dc.Sign _
.Tag = dc _
})(The original code the scoping around dc.Sign is unnecessary as well as casting dc to object). So if you change it, I'm sure you'll see SubItems come back up in Intellisense, as well as Tag. Key is not a property of ListViewItem so that's why nothing comes up in Intellisense.
-
Converters are not perfect, some are better than others and I find the online ones to be the worst. Why? Because the best converters will compile the code first, get down to MSIL and then convert back up to whatever you want. Online converters typically don't take compiled code so they attempt to do conversions with text replacements. The one you found isn't bad, but it made up the Key part somewhere, your conversion should be:
Me.listView.Items.Add(New ListViewItem(dc.Line) With { _
.SubItems = dc.Sign _
.Tag = dc _
})(The original code the scoping around dc.Sign is unnecessary as well as casting dc to object). So if you change it, I'm sure you'll see SubItems come back up in Intellisense, as well as Tag. Key is not a property of ListViewItem so that's why nothing comes up in Intellisense.
Hi Ron, thank you for your answer. It's funny, but during my own attempts for a working solution I had put it just that way... and of course I'm aware of what you wrote about converters. Unfortunately there's still something wrong, the error message stays ("The property 'SubItems' is ReadOnly."). And by just opening a new Sub which contains the line
Dim mylv As ListViewItem = New ListViewItem(dc.Line) With {.s...."
you can see that Intellisense doesn't show "SubItems" then. Do you have any further hints? I was afraid that the curly brackets (".Subitems = {dc.Sign}") have to be converted in a different way – but I had no clue what C# uses them for...
-
Hi Ron, thank you for your answer. It's funny, but during my own attempts for a working solution I had put it just that way... and of course I'm aware of what you wrote about converters. Unfortunately there's still something wrong, the error message stays ("The property 'SubItems' is ReadOnly."). And by just opening a new Sub which contains the line
Dim mylv As ListViewItem = New ListViewItem(dc.Line) With {.s...."
you can see that Intellisense doesn't show "SubItems" then. Do you have any further hints? I was afraid that the curly brackets (".Subitems = {dc.Sign}") have to be converted in a different way – but I had no clue what C# uses them for...
Try SubItems.Add(dc.Line). The collection is read-only (same in C#, so I'm not sure how they are getting away with it in the original code). You can't assign something directly to it, so it should not work in the C# code either. In C#, the brackets are scoping characters, and in the code you posted, unnecessary for anything.
-
Try SubItems.Add(dc.Line). The collection is read-only (same in C#, so I'm not sure how they are getting away with it in the original code). You can't assign something directly to it, so it should not work in the C# code either. In C#, the brackets are scoping characters, and in the code you posted, unnecessary for anything.
-
Try SubItems.Add(dc.Line). The collection is read-only (same in C#, so I'm not sure how they are getting away with it in the original code). You can't assign something directly to it, so it should not work in the C# code either. In C#, the brackets are scoping characters, and in the code you posted, unnecessary for anything.
Ron Beyer wrote:
In C#, the brackets are scoping characters, and in the code you posted, unnecessary for anything.
Incorrect. In this context the brackets designate a list initialiser[^]. This works because list initialisers in C# compile to repeated calls to Add, not an assignment. If that's not the case in VB then the questioner will have to call SubItems.Add directly.
-
Ron Beyer wrote:
In C#, the brackets are scoping characters, and in the code you posted, unnecessary for anything.
Incorrect. In this context the brackets designate a list initialiser[^]. This works because list initialisers in C# compile to repeated calls to Add, not an assignment. If that's not the case in VB then the questioner will have to call SubItems.Add directly.
-
Thanks, I never used them, I guess for good reason, they are also scoping tokens so I'm not completely incorrect but I forgot about using them that way to initialize a list.
List initialisers are actually really nice, particularly in writing unit tests or setting up in-memory test data. Yes, they can be used for scoping, but only when they surround entire statements. That's rarely useful as what you'd want to use as scope regions are usually already in a block (e.g. if, foreach etc). I think case statement groups are the only place I've found that capability useful.