[英]using class from another class c#
Got problem with my c# code on form1.cs : 在form1.cs上我的C#代码出现问题:
using sonep = session.Broker;
using maind = ClassLibrary1.Personn;
sonep boi = new sonep();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
maind p = new maind();
p.Nom = txtnom.Text;
p.Prenom = txtprenom.Text;
boi.Insert(p);
}
}
I found this but don't helped me : 我找到了,但是没有帮助我:
https://docs.microsoft.com/fr-fr/dotnet/csharp/language-reference/keywords/using-directive https://docs.microsoft.com/fr-fr/dotnet/csharp/language-reference/keywords/using-directive
You cannot put sonep boi = new sonep();
你不能把
sonep boi = new sonep();
anywhere outside of the class scope as you did. 您可以在班级范围之外的任何地方进行操作。 You are trying to create an object of type
sonep
called boi
. 您正在尝试创建名为
boi
sonep
类型的对象。
This is how it should be: 这应该是这样的:
private void Form1_Load(object sender, EventArgs e)
{
maind p = new maind();
p.Nom = txtnom.Text;
p.Prenom = txtprenom.Text;
sonep boi = new sonep();
boi.Insert(p);
}
The code at the top of your file: 文件顶部的代码:
using sonep = session.Broker;
using maind = ClassLibrary1.Personn;
sonep boi = new sonep();
Has some issues. 有一些问题。 The space outside the class scope is where you will define
using
directives as a way to "import" namespaces to be used in this class. 在类范围之外的空间是您将
using
指令定义的位置,以“导入”要在此类中使用的名称空间。 You will never instantiate classes outside the scope of the class. 您将永远不会在类范围之外实例化类。 Typically
using
directives will look one of two ways: 通常,
using
伪指令将看起来是以下两种方式之一:
using sonep = session.Broker; //this creates an alias for the namespace
using ClassLibrary1.Personn; //This just imports the namespace as is
Later, inside the scope of your class is where you want to instantiate instances of classes: 稍后,您可以在类范围内实例化类实例:
private void Form1_Load(object sender, EventArgs e)
{
sonep boi = new sonep(); //Instantiate like this
maind p = new maind(); //You already Instantiate an object here
p.Nom = txtnom.Text;
p.Prenom = txtprenom.Text;
boi.Insert(p);
}
This 这个
sonep boi = new sonep();
Suggests you're trying to create a new object of type sonep
. 建议您尝试创建一个类型为
sonep
的新对象。 Another example would be 另一个例子是
MyObject myObj = new MyObject();
But this could be anything; 但这可以是任何东西; for example, if you've got an Apple object that takes diameter and colour you could instansiate it like this
例如,如果您有一个带有直径和颜色的Apple对象,则可以像这样实例化它
Apple myApple = new Apple(5, "red");
However 然而
using sonep = session.Broker;
Suggests you're trying to create an alias with the using directive. 建议您尝试使用using指令创建别名。 For example,
例如,
using Project = MyNamespace.MyCompany.Project;
This technique is useful if you've got long names because you can then do this 如果您有长名,则此技术很有用,因为您可以这样做
Project.MyClass mc = new Project.MyClass();
Rather than having to do this 不必这样做
MyNamespace.MyCompany.Project.MyClass mc = new MyNamespace.MyCompany.Project.MyClass();
If you're trying to instantiate a new object of type sonep
you must do so inside your class. 如果要实例化
sonep
类型的新对象, sonep
必须在类内部进行实例化。 For example, 例如,
private void Form1_Load(object sender, EventArgs e)
{
sonep boi = new sonep();
...
}
Alternatively, if you're trying to create an alias to a namespace you can't instantiate it. 或者,如果您尝试创建名称空间的别名,则无法实例化它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.