1

First , I'm a C++ dev and new to C# , Sorry for this simple question. I'm creating a wrapper for my native library.

I want to pass a value in a class and edit it from some other function in class , like pointers in C++ , I found out it can be done with unsafe mode but I need to do it without unsafe and I'm sure it's possible.

Here's my code :

Main Console

namespace ConsoleApp2
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string DATA_VALUE = "Not Set Yet";
            new Data_Picker_Form(DATA_VALUE).ShowDialog();

            Console.WriteLine("Value is {0}", DATA_VALUE);

            Console.ReadKey();
        }
    }
}

Form Code

using System;
using System.Windows.Forms;

namespace ConsoleApp2
{
    public partial class Data_Picker_Form : Form
    {
        object data_in_obj;

        public Data_Picker_Form(string data_in)
        {
            InitializeComponent();
            data_in_obj = data_in;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string data_in_new = data_in_obj as string;
            data_in_new = "OUTPUT_VALUE";
            this.Close();
        }
    }
}

it's not working unfortunately , so I need to pass my string , int and etc. value to a new form , in initializing form creates a instance [like pointer] to original string and it can accessible from other functions ins class like button click.

thanks.

2 Answers2

0

You already have a reference (sort of like a pointer), and that is your problem (well, half of it anyways).

string data_in_new = data_in_obj as string;

Says to create a variable that holds a string reference (as strings are immutable reference types) and copy the reference data_in_obj currently has to it. When you then reassign data_in_new it of course doesn't affect any other variable (just like it wouldn't in C++).

Because strings are immutable, there is no way for that code to affect other things that point to that string (passing by reference aside, but this is about variables/members). You need to store it in a simple struct or class so that everyone is pointing at an object that holds the current string reference, and can be updated.

Same idea with your int, it is actually a value type so any copy will copy the actual value, you can't change a different variable through it. You need a wrapper class so that each user is pointing at the same object.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
0

What about setting up a simple get and set function since you are already Initializing your Form?

In Your Form:

    private string data_in_new = "";

    public Data_Picker_Form(string DATA_VALUE)
   {
     InitializeComponent();
   }

   private void button1_Click(object sender, EventArgs e)
   {
    Data_in_new = "OUTPUT_VALUE";
    Close();
   }

   public string Data_in_new
   {
     get
    {
     return data_in_new;
    }

    set
    {
     data_in_new = value;
    }
   }

And in your Console:

    class Program
   {
     static void Main(string[] args)
    {
     string DATA_VALUE = "Not Set Yet";
     Data_Picker_Form D_P_T = new Data_Picker_Form(DATA_VALUE);

     D_P_T.ShowDialog();
     DATA_VALUE = D_P_T.Data_in_new;

     Console.WriteLine("Value is {0}", DATA_VALUE);

    Console.ReadKey();
   }
  }

This Way you can access you value at any desired point in the project.

Reese Jones
  • 181
  • 8
  • Mike's answer was simpler and better but your answer works fine too , Thank you! –  May 08 '19 at 21:40