Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. VB Conversion: 'ReadOnly' Error in VB

VB Conversion: 'ReadOnly' Error in VB

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studiohelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sonhospa
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • S Sonhospa

      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

      R Offline
      R Offline
      Ron Beyer
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • R Ron Beyer

        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.

        S Offline
        S Offline
        Sonhospa
        wrote on last edited by
        #3

        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...

        R 1 Reply Last reply
        0
        • S Sonhospa

          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...

          R Offline
          R Offline
          Ron Beyer
          wrote on last edited by
          #4

          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.

          S B 2 Replies Last reply
          0
          • R Ron Beyer

            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.

            S Offline
            S Offline
            Sonhospa
            wrote on last edited by
            #5

            Ron Beyer wrote:

            In C#, the brackets are scoping characters, and in the code you posted, unnecessary for anything.

            ;) Ok, so I don't worry too much about the fact that I don't understand some of it Thank you, anyway!

            1 Reply Last reply
            0
            • R Ron Beyer

              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.

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              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.

              R 1 Reply Last reply
              0
              • B BobJanova

                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.

                R Offline
                R Offline
                Ron Beyer
                wrote on last edited by
                #7

                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.

                B 1 Reply Last reply
                0
                • R Ron Beyer

                  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.

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups