sok
sok

Reputation: 23

using class from another class c#

Got problem with my c# code on form1.cs :

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://learn.microsoft.com/fr-fr/dotnet/csharp/language-reference/keywords/using-directive

Upvotes: 1

Views: 10374

Answers (3)

maccettura
maccettura

Reputation: 10819

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. You will never instantiate classes outside the scope of the class. Typically using directives will look one of two ways:

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);
}

Upvotes: 1

Gareth
Gareth

Reputation: 911

This

sonep boi = new sonep();

Suggests you're trying to create a new object of type 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 myApple = new Apple(5, "red");

However

using sonep = session.Broker;

Suggests you're trying to create an alias with the using directive. 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. 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.

Upvotes: 0

Thomas Flinkow
Thomas Flinkow

Reputation: 5115

You cannot put 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.

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);
}

Upvotes: 1

Related Questions