佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1328|回复: 12

想请教大家一下 C#.net

[复制链接]
发表于 19-2-2008 03:16 PM | 显示全部楼层 |阅读模式
请问我该如何在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。
回复

使用道具 举报


ADVERTISEMENT

发表于 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;
回复

使用道具 举报

Follow Us
 楼主| 发表于 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;
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
原帖由 为人民服务 于 20-2-2008 10:29 PM 发表


放到public Form1()的前一行。

一样啊,没改变。。。。。。。
回复

使用道具 举报

发表于 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;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 31-12-2025 09:49 PM , Processed in 0.112690 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表