Passing a value from one class to another

I'm a beginner in C#
I declare a structure in a class named OakSensor.cs

public struct tOakSensor
    {
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 24)]
        public string SN;
      }
I try to use the SN value in another class named form3.cs
I'm a beginner in C#
I declare a structure in a class named OakSensor.cs

public struct tOakSensor
    {
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 24)]
        public string SN;
      }
I try to use the SN value in another class named form3.cs

How can I pass the SN value from OakSensor.cs to form3.cs ?

I've tried

            tOakSensor sensorxx = new tOakSensor();
            textBox1.Text = (sensorxx.SN);

but it returns "" (the default value)
luciepaulAsked:
Who is Participating?
 
Todd GerbertIT ConsultantCommented:
The DLL's function is defined in your class OakSensor is marked static (all DllImport methods are required to be static), and since it's also marked public you call this method from outside of the OakSensor class by using the syntax:
OakSensor.Oak_FindSensor_a(blah, blah, blah);

Open in new window

(were it marked private you would only be able to call Oak_FindSensor_a from inside of the OakSensor class)


Therefore, if your Form3 has a button named "Button1", your code might look something like:
private void Button1_Click(object sender, EventArgs e)
{
	tOakSensor sensor = new tOakSensor();
			
	// Note that at this point sensor.SN doesn't contain
	// anything since we just now created "sensor", and
	// when we did so it in turn created it's members,
	// which is "SN", and nothing has been assigned to
	// "SN" yet - as was already pointed out

	OakSensor.Oak_FindSensor_a("some string parameter required here", ref sensor);

	// MAYBE the Oak_FindSensor_a function filled in sensor.SN for us?
	textBox1.Text = sensor.SN;
}

Open in new window



However, that assumes your OakSensor.cs looks like this:
using System;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1 // <-- This should match the namespace in Form3.cs
{
	public class OakSensor
	{
		[DllImport("oaka.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern bool Oak_FindSensor_a(string SN, ref tOakSensor SensorFound);
	}

	public struct tOakSensor
	{
		public string SN;
	}
}

Open in new window

Note that while both the tOakSensor struct and OakSensor class are declared in the same file, the struct is not defined within the class, and the class is not defined within the struct; they are side-by-side not nested one inside the other.


If, on the other hand, OakSensor.cs looks like:
using System;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1 // <-- This should match the namespace in Form3.cs
{
	public class OakSensor
	{
		[DllImport("oaka.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern bool Oak_FindSensor_a(string SN, ref tOakSensor SensorFound);

		public struct tOakSensor
		{
			public string SN;
		}
	}
}

Open in new window

Then Form3.cs will have OakSensor.tOakSensor sensor = new OakSensor.tOakSensor(); instead of tOakSensor sensor = new tOakSensor(); (note that in this case tOakSensor is defined within OakSensor).


Be aware that the names of functions you declare with a DllImport attribute must exactly match the name of the function in the DLL (I would not be surprised if the correct name is actually OakFindSensorA, but you should just double-check the documentation for this function).

Also note that the Oak_FindSensor_a function, as you have it declared, expects two parameters: one string and one tOakSensor.
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,
you may have to write some logic to get SN value. if you are assigning some value in the other class/form? as by default this value should be empty.
thanks
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
           tOakSensor sensorxx = new tOakSensor();
            sensorxx.SN = "Hello world";
            textBox1.Text = sensorxx.SN;


You need to set a value of SN
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi, you can do some thing like this

public struct tOakSensor
    {
        public tOakSensor()
        {
            SN = "Your Code or other logic"; // your logic to get SN
        }

        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 24)]
        public string SN;
    }
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
I suspect you need something like this, based on this and your previous question:

tOakSensor sensorxx = new tOakSensor();
NowCallTheDllFromTheManufacturerOfTheSensorForTheSensorID(sensorxx);
textBox1.Text = (sensorxx.SN);
0
 
luciepaulAuthor Commented:
What i've got in OakSensor.cs is :
public class OakSensor
{
      [DllImport("oaka.dll", CallingConvention = CallingConvention.Cdecl)]
      public static extern bool Oak_FindSensor_a(string SN, ref tOakSensor SensorFound);
        public static void Oak_PrintSensorInfo(ref tOakSensor Sensor)
        {
           Console.WriteLine("SN:{3}", Sensor.SN,
         }
 }

I understand I'me supposed to "CallTheDllFromTheManufacturer.." in Form3.cs

That's were I need help with the exact syntax
Thanks again
0
 
luciepaulAuthor Commented:
An answer ... and a great tutorial
Thanks a lot for going beyond my expectations
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Note - what you really wanted is not what you asked a question about.  Don't just ask a random question, really think first about what you require otherwise you just waste time.  The experts here are volunteers, not paid.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.