读书人

怎么在两个类之间传递数据

发布时间: 2012-04-12 15:46:35 作者: rapoo

如何在两个类之间传递数据?
假如有两个CLASS A和B
我在A中有一个变量n,但是我在写B时发现要用到变量n的内容,有什么好的办法可以使B中顺利的读取到A中的n变量?
变量n是在A实例化后才会有值。

[解决办法]
Public Class A
Public Shared MyA As A
Public n As String
Public Sub New()
MyA = Me
n = "蛋疼的变量"
End Sub
End Class

Public Class B
Public Sub ShowA()
If Not A.MyA Is Nothing Then
MsgBox(A.MyA.n)
Else
MsgBox("A还没有初始化")
End If
End Sub
End Class
[解决办法]
我不清楚你为什么要这么做,但是做的方法有许多。

A:

C# code
    class ClassA    {        private string _n = "";        public string n        {            get            {                return _n;            }            set            {                _n = value;            }        }        public string doExcute()        {            _n = "123";            ClassB b = new ClassB(_n);            return b.doExcuteB();        }    } 

读书人网 >VB Dotnet

热点推荐