|
查看: 1327|回复: 12
|
想请教大家一下 C#.net
[复制链接]
|
|
|
请问我该如何在C#.net里share同一个object?即比如Form1和Form2都可以更改同一个object里的variable value。
E.g. object objX里有个boolean variable叫 bl ,在Form1里我可以assign objX.bl=true;在Form2里我又可以更改 objX.bl= false。 |
|
|
|
|
|
|
|
|
|
|
发表于 19-2-2008 06:19 PM
|
显示全部楼层
|
define as public variable |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 20-2-2008 08:59 AM
|
显示全部楼层
原帖由 为人民服务 于 19-2-2008 06:19 PM 发表 
define as public variable
我面对的问题是在哪define variable??? |
|
|
|
|
|
|
|
|
|
|
发表于 20-2-2008 02:39 PM
|
显示全部楼层
原帖由 lonely_wolf 于 20-2-2008 08:59 AM 发表 
我面对的问题是在哪define variable???
main 入口点之前。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 20-2-2008 04:17 PM
|
显示全部楼层
回复 4# 为人民服务 的帖子
不行啊。。。它出现以下message:
The type or namespace name 'letter2' could not be found (are you missing a using directive or an assembly reference?)
P.S. letter2 是我的class的object。这问题是在Form2那边出现的。
我在Form1的Main()上面declare:
public RandomLetters letter2; |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 20-2-2008 04:17 PM
|
显示全部楼层
回复 4# 为人民服务 的帖子
不行啊。。。它出现以下message:
The type or namespace name 'letter2' could not be found (are you missing a using directive or an assembly reference?)
P.S. letter2 是我的class的object。这问题是在Form2那边出现的。
我在Form1的Main()上面declare:
public RandomLetters letter2; |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 20-2-2008 04:19 PM
|
显示全部楼层
回复 4# 为人民服务 的帖子
不行啊。。。它出现以下message:
The type or namespace name 'letter2' could not be found (are you missing a using directive or an assembly reference?)
P.S. letter2 是我的class的object。这问题是在Form2那边出现的。
我在Form1的Main()上面declare:
public RandomLetters letter2; |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 20-2-2008 04:20 PM
|
显示全部楼层
回复 4# 为人民服务 的帖子
不行啊。。。它出现以下message:
The type or namespace name 'letter2' could not be found (are you missing a using directive or an assembly reference?)
P.S. letter2 是我的class的object。这问题是在Form2那边出现的。
我在Form1的Main()上面declare:
public RandomLetters letter2; |
|
|
|
|
|
|
|
|
|
|
发表于 20-2-2008 10:29 PM
|
显示全部楼层
原帖由 lonely_wolf 于 20-2-2008 04:17 PM 发表 
不行啊。。。它出现以下message:
The type or namespace name 'letter2' could not be found (are you missing a using directive or an assembly reference?)
P.S. letter2 是我的class的object。这问题是在 ...
放到public Form1()的前一行。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 21-2-2008 08:43 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 21-2-2008 02:57 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 22-2-2008 11:39 AM
|
显示全部楼层
想要说的是,C# 本来就没有 Global Variable,但你可以用 static class 来达到。
你先建立一个 class,例如以下的:
static class SimpleObject
{
private static string p_val = "";
// variable 也得是 static
public static string SimpleVariable
{
get
{
return p_val;
}
set
{
p_val = value;
}
}
}
这样,你可以在任何一个 Form 读取以上的 class。 |
|
|
|
|
|
|
|
|
|
|
发表于 21-3-2008 10:42 PM
|
显示全部楼层
define as public static object.
public static class A
{
public string MyName
{ get { return "myName"; } }
}
class B
{
string aName = A.MyName;
} |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|