Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I got a small task to be solved but i have no idea about it some one solve me this.

//code:1
C#
namespace Demo1
{
public class Sample1
{
 public void sampleFn1()
 {
   string x = "Hello World";
 }
}
}



//code:2
C#
namespace Demo2
{
 public class Sample2
 {
   public void sampleFn2()
 {
   MessageBox.Show(x);
 }
}
}



In the above program i want to access the value x and display it as messagebox in code 2 without modifying code 1, I heard that this could be done by get set property. I'm not clear about that anyone give solution for this problem.


Thanks
Karthik.R
Posted
Comments
Kornfeld Eliyahu Peter 2-Dec-14 14:02pm    
Try reflection...
BillWoodruff 2-Dec-14 19:45pm    
"reflection" ? That's shooting a horsefly with a cannon.
Kornfeld Eliyahu Peter 3-Dec-14 2:09am    
You missed the point that we are talking about a non-public variable, but I missed the point that this variable is local to a member method, so even reflection can't help.
In fact the optimizer will remove that line of code entirely from the code, so we end with an empty method...
BillWoodruff 7-Dec-14 9:59am    
Well, no I didn't miss the fact it was a non-public variable inside a public method, inside a public class, but what I DID miss was considering the idea that the line would be removed by the compiler (if the variable was not re-used locally ?) and so would be unable to be parsed via Reflection at run-time.

In my head, what I was pointing-out as simpler was just moving the variable and changing its access modifier.

And, I appreciate your bringing this to my attention !
Kornfeld Eliyahu Peter 7-Dec-14 10:01am    
At your service :-)

This is one way of many to solve your problem.

The class Sample1 should be in its own library
See this example for how to do it: MSDN: Libraries Tutorial[^]
C#
namespace Demo1
{
    public class Sample1
    {
        public string X { get; set; } // Public property

        public void sampleFn1()
        {
            this.X = "Hello World"; // Set the property to a value
        }
    }
}


Assuming the class Sample1 exists in a library called Sample1.dll you need to add a reference to this DLL in your second project. See MSDN: How to: Add or Remove References [^]
C#
using Demo1;  // Include the namespace used in project 1

namespace Demo2
{
    public class Sample2
    {
        private Sample1 sample1;   // Declare a variable of type Sample1

        public Sample2()
        {
            sample1 = new Sample1(); // Create an instance of Sample1
        }

        public void sampleFn2()
        {
            sample1.sampleFn1();  // Call the function to set a value to the property X
            MessageBox.Show(sample1.X); // Show the value of property X
        }
    }
}
 
Share this answer
 
v2
Comments
Karthik Ravi 7-Dec-14 12:02pm    
Thanks a lot @George Jonsson for your valuable solution. I need one confirmation that it is immpossible to get value from code 1 to code 2 without modifying code 1.
George Jonsson 8-Dec-14 2:00am    
It is not impossible without modifications.
Read Solution 1 once more for a good explanation why?
You can't access x in the first code sample from the second: it's a local variable to the sampleFn1 method and as such will only exist while the method is running. As soon as the method exits, it's stack space is released and the variable ceases to exist in any meaningful way - it's entirely likely that it will never exist at all, since it is a prime target for the optimiser to remove as it is never used! Even if the optimizer leaves it in, the heap space that the string it references occupies is not guaranteed to remain in existence either, as there is no further reference to it, and the GC is at liberty to recycle the memory space.

I don't think that code is what you are trying to do: and I'm not sure what or why you think that would be a good idea even if you could do it.

Sit down, have a think about exactly what you are trying to achieve, and perhaps explain that rather better to us?
 
Share this answer
 
There are lots of ways you do this; here's two examples:

First example: using a 'static variable:
C#
using System.Windows.Forms;

namespace Demo1
{
    public class Sample1
    {
        public static string x = "initial value of 'x";

        public void sampleFn1() {x = "value of 'x set in function";}
    }
}

namespace Demo2
{
    public class Sample2
    {
        public void sampleFn2(){MessageBox.Show(Demo1.Sample1.x);}
    }
}
If we then executed this code in some other NameSpace (not Demo1, or Demo2):
C#
Demo2.Sample2 newSample2 = new Demo2.Sample2();

newSample2.sampleFn2();
You'd get a MessageBox pop-up showing the value of 'x it was initialized to.

If you changed the access-modifier of 'x so it was not 'static: you'd get an error message because there is no instance of Sample1 available, and Sample2 has no access to an instance of Sample1 even if one was created.

Second example: where 'x is not 'static, and access to 'x in an instance of Sample1 is provided by passing the instance of Sample1 into the method 'sampleFn2:
C#
namespace Demo1
{
    public class Sample1
    {
        public string x = "initial value of 'x";
        
        public void sampleFn1(){x = "value of 'x set in class constructor";}
    }
}

namespace Demo2
{
    public class Sample2
    {
        public void sampleFn2(Demo1.Sample1 instance){MessageBox.Show(instance.x);
    }
}

// test

Demo1.Sample1 newSample1 = new Demo1.Sample1();
Demo2.Sample2 newSample2 = new Demo2.Sample2();

// will show value of 'x as it is initialized to in Sample1
newSample2.sampleFn2(newSample1);

// this will change the value of 'x
newSample1.sampleFn1();

// this will show the changed value of 'x
newSample2.sampleFn2(newSample1);
 
Share this answer
 
Comments
Karthik Ravi 7-Dec-14 4:48am    
Hi @BillWoodruff thanks for your time, the value which i declared is present inside the method as local variable not outside the method.
BillWoodruff 7-Dec-14 9:50am    
To access 'x, as others have pointed out here, you must expose the value of 'x, by moving it outside the method, setting its access to 'public, using 'static, creating a public property that "wraps" the private 'x, etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900