通常情况下,如果我想将一个对象传递给某个实例,我会这样做......
清单1 文件1:
public class SomeClass
{
// Some Properties
public SomeClass()
{
public int ID
{
get { return mID; }
set { mID = value; }
}
public string Name
{
set { mName = value; }
get { return mName; }
}
}
}
public class SomeOtherClass
{
// Method 1
private void Method1(int one, int two)
{
SomeClass USER; // Create an instance
Squid RsP = new Squid();
RsP.sqdReadUserConf(USER); // Able to pass 'USER' to class method in different file.
}
}
类似于列表1的方法,但我无法让它工作。我在这里做错了什么?
清单2(这不起作用,为什么?)
文件1:
private void SomeClass1
{
[snip]
TCOpt_fM.AutoUpdate = optAutoUpdate.Checked;
TCOpt_fM.WhiteList = optWhiteList.Checked;
TCOpt_fM.BlackList = optBlackList.Checked;
[snip]
private TCOpt TCOpt_fM;
TCOpt_fM.SaveOptions(TCOpt_fM);
}
文件2:
public class TCOpt:
{
public TCOpt OPTIONS;
[snip]
private bool mAutoUpdate = true;
private bool mWhiteList = true;
private bool mBlackList = true;
[snip]
public bool AutoUpdate
{
get { return mAutoUpdate; }
set { mAutoUpdate = value; }
}
public bool WhiteList
{
get { return mWhiteList; }
set { mWhiteList = value; }
}
public bool BlackList
{
get { return mBlackList; }
set { mBlackList = value; }
}
[snip]
public bool SaveOptions(TCOpt OPTIONS)
{
[snip]
Some things being written out to a file here
[snip]
Squid soSwGP = new Squid();
soSgP.sqdWriteGlobalConf(OPTIONS);
}
}
文件3:
public class SomeClass2
{
public bool sqdWriteGlobalConf(TCOpt OPTIONS)
{
Console.WriteLine(OPTIONS.WhiteSites); // Nothing prints here
Console.WriteLine(OPTIONS.BlackSites); // Or here
}
}
在这个例子中,我无法使用清单1中的方法。可能是因为清单1在类之间传递了一个对象。然而,在下面,事物是在一个类中定义的。我不得不使用一些额外的步骤(试验和错误)来使事情发挥作用。我不确定我在这里做了什么或者它叫什么。这是很好的编程习惯吗?或者有一种更简单的方法(如清单1所示)。
清单3(这是有效的)
文件1:
private void SomeClass1
{
private TCOpt TCOpt_fM;
[snip]
TCOpt_fM.AutoUpdate = optAutoUpdate.Checked;
TCOpt_fM.WhiteList = optWhiteList.Checked;
TCOpt_fM.BlackList = optBlackList.Checked;
[snip]
TCOpt_fM.SaveOptions(TCOpt_fM);
}
文件2:
public class TCOpt:
{
public TCOpt OPTIONS;
[snip]
private bool mAutoUpdate = true;
private bool mWhiteList = true;
private bool mBlackList = true;
public bool AutoUpdate
{
get { return mAutoUpdate; }
set { mAutoUpdate = value; }
}
public bool WhiteList
{
get { return mWhiteList; }
set { mWhiteList = value; }
}
public bool BlackList
{
get { return mBlackList; }
set { mBlackList = value; }
}
[snip]
public bool SaveOptions(TCOpt OPTIONS)
{
[snip]
Some things being written out to a file here
[snip]
Squid soSwGP = new Squid();
soSwGP.OPTIONS = OPTIONS;
}
}
文件3:
[snip]
private TCOptions TCOpt_TCS;
public TCOpt OPTIONS
{
get { return TCOpt_TCS; }
set
{
TCOpt_TCS = value;
sqdWriteGlobalConf();
}
[snip]
public class SomeClass2
{
public bool sqdWriteGlobalConf()
{
Console.WriteLine(OPTIONS.WhiteSites);
Console.WriteLine(OPTIONS.BlackSites);
}
}
提前致谢,
XO
答案 0 :(得分:1)
起初,你似乎对OOP的工作方式(或者希望只是错误的单词)有完全错误的描述。
您不在文件之间传递对象。你在课堂之间传递它们。通常你会把一个类放到一个文件中,在这种情况下你的描述是正确的。但是,您可以将多个类放入一个文件中,也可以在多个文件上生成一个类(使用partial关键字)。
所以要完全正确,你也不要传递课程。您传递类的实例。您的示例中的问题是您声明了特定类的对象,但您没有实例化它:
private void Method1(int one, int two)
{
SomeClass USER; // This doesn't create an instance!
USER = new SomeClass(); //This creates an instance.
//SomeClass USER = new SomeClass(); //Can also be written as one-liner.
Squid RsP = new Squid();
RsP.sqdReadUserConf(USER); // Able to pass 'USER' to class method in different file.
}
阅读完评论后,我查看了你的第二个和第三个例子。也许它只是清单2文件1中的一个错字,但是因为你在清单3中更改了文件1我觉得你的问题肯定存在:
private void SomeClass1
{
[snip]
TCOpt_fM.AutoUpdate = optAutoUpdate.Checked;
TCOpt_fM.WhiteList = optWhiteList.Checked;
TCOpt_fM.BlackList = optBlackList.Checked;
[snip]
//It is impossible to declare here a variable name that is already used
//above.
//But it looks like you just declared here a variable (without instantiation)
//and trying to use it as parameter for the SaveOptions() function.
private TCOpt TCOpt_fM;
TCOpt_fM.SaveOptions(TCOpt_fM);
}
通常这会导致一些错误消息。
null
对其进行初始化,或使用out keyword