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. Passing a Form reference to a new Form

Passing a Form reference to a new Form

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 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.
  • M Offline
    M Offline
    mikemilano
    wrote on last edited by
    #1

    I'm trying to pass a reference of Form1 to Form2 so I can change things on Form1 from Form2. Here is my logic: Form1private void btnShow_Click(object sender, System.EventArgs e) { FormTwo = new Form2(ref this); FormTwo.Show(); }
    Form2:// .. yes form one was declared up here somewhere as public public Form2(ref Form1 PassingForm) { InitializeComponent(); FormOne = PassingForm; }
    error CS1605: Cannot pass '' as a ref or out argument because it is read-only i can read the error, .. but what would the proper logic be here?

    M 1 Reply Last reply
    0
    • M mikemilano

      I'm trying to pass a reference of Form1 to Form2 so I can change things on Form1 from Form2. Here is my logic: Form1private void btnShow_Click(object sender, System.EventArgs e) { FormTwo = new Form2(ref this); FormTwo.Show(); }
      Form2:// .. yes form one was declared up here somewhere as public public Form2(ref Form1 PassingForm) { InitializeComponent(); FormOne = PassingForm; }
      error CS1605: Cannot pass '' as a ref or out argument because it is read-only i can read the error, .. but what would the proper logic be here?

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      You don't need 'ref', because a Form1 is already a reference type. The error occurs because you're trying to pass a reference to 'this', which cannot be changed. Personally I find reference languages confusing, because of the nature of aliasing and that there are no clues in the syntax indicating whether you're dealing with a value (which uses a shallow copy) or a reference (which merely copies the reference).

      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