如何在一个项目中无需修改即可将值从一个项目传递到另一个项目? [英] How to pass a value from one project to another project without modification in one project?

查看:71
本文介绍了如何在一个项目中无需修改即可将值从一个项目传递到另一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个小任务要解决,但我不知道有人解决了这个问题。



//代码:1

Hi all,
I got a small task to be solved but i have no idea about it some one solve me this.

//code:1

namespace Demo1
{
public class Sample1
{
 public void sampleFn1()
 {
   string x = "Hello World";
 }
}
}







//代码: 2




//code:2

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







在上述程序中我想访问值x并在代码2中将其显示为消息框而不修改代码1,我听说这可以通过get set属性来完成。我不清楚是否有人为这个问题提供解决方案。





谢谢

Karthik.R




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

推荐答案

这是解决问题的方法之一。



班级 Sample1 应该在自己的库中

请参阅此示例以了解如何执行此操作: MSDN:Libraries Tutorial [ ^ ]

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[^]
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
        }
    }
}





假设类名为$ 1 c $ c> Sample1 存在于名为Sample1.dll的库中需要在第二个项目中添加对此DLL的引用。请参阅 MSDN:如何:添加或删除引用 [ ^ ]



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 [^]

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
        }
    }
}


你不能在第二个代码示例中访问 x :它是本地的变量到sampleFn1方法,因此仅在方法运行时存在。一旦方法退出,它的堆栈空间就被释放,变量不再以任何有意义的方式存在 - 它完全可能永远不会存在,因为它是优化器的主要目标,因为它从未使用过!即使优化器将其保留,它所引用的字符串占用的堆空间也不能保证存在,因为没有进一步的引用,并且GC可以自由地回收内存空间。



我不认为代码是你想要做的:我不确定你认为什么或为什么这是一个好主意,即使你可以做它。



坐下来,仔细考虑一下你想要达到的目标,或许对我们解释得更好?
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?


你有很多方法可以做到这一点;这里有两个例子:



第一个例子:使用'静态变量:
There are lots of ways you do this; here's two examples:

First example: using a 'static variable:
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);}
    }
}

如果我们在其他一些NameSpace(不是Demo1或Demo2)中执行此代码:

If we then executed this code in some other NameSpace (not Demo1, or Demo2):

Demo2.Sample2 newSample2 = new Demo2.Sample2();

newSample2.sampleFn2();

你会得到一个MessageBox弹出窗口,显示'x它被初始化为的值。



如果您更改了'x所以它不是'静态的访问修饰符:您将收到一条错误消息,因为没有可用的Sample1实例,并且Sample2 无法访问到Sample1的一个实例,即使创建了一个。



第二个例子:'x不是'静态,并且在Sample1的实例中访问'x通过将Sample1的实例传递给方法'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:

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);


这篇关于如何在一个项目中无需修改即可将值从一个项目传递到另一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆