?
Solved

Passing a value from one class to another

Posted on 2011-04-22
8
Medium Priority
?
545 Views
Last Modified: 2013-12-16
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)
0
Comment
Question by:luciepaul
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35447218
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 35447237
           tOakSensor sensorxx = new tOakSensor();
            sensorxx.SN = "Hello world";
            textBox1.Text = sensorxx.SN;


You need to set a value of SN
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35447251
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 35447306
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
 

Author Comment

by:luciepaul
ID: 35448797
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
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 35449984
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
 

Author Closing Comment

by:luciepaul
ID: 35450054
An answer ... and a great tutorial
Thanks a lot for going beyond my expectations
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 35452447
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question