1

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

5
  • 1
    Problem is on "boi" Commented Jun 14, 2017 at 13:56
  • So, what is the problem? Are you getting an error? Commented Jun 14, 2017 at 13:56
  • 1
    If boi is a variable it cannot be declared out of the scope of a class or method. Commented Jun 14, 2017 at 13:58
  • I actually cant use : using session; so i tried to use using sonep = session.Broker; but when i try to boi.Insert(p); it dont work Commented Jun 14, 2017 at 14:00
  • @sok why cant you use using session? This should be valid C#, provided that your namespace is actually called session (namespaces are case-sensitive) Commented Jun 14, 2017 at 14:06

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh ok ! Thank you !
@sok no problem, I am glad that it works for you now
1

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

4 Comments

I see your point and your solution is correct, but it is true that you can do using sonep = Session.Broker; if sonep is a class in Session.Broker namespace
using sonep = session.Broker creates an alias. See this learn.microsoft.com/en-us/dotnet/csharp/language-reference/… for more details
@Gareth thank you, that was exaclty what I was wanting to say
Sorry, I knew that but I went a little blind with the other issues and just glossed right over it. I have edited my post.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.