locked
Beginner Question RRS feed

  • Question

  • namespace Harmony2015

    { public partial class BuildModel : Form { public BuildModel() { InitializeComponent(); } public class ImpedanceEl { public ImpedanceEl() { } public int Id { get; set; } public int Length { get; set; } public double Re_X { get; set; } // I am not sure what type of value impedance has } public class Power_Sys { public Power_Sys() { } public string Power_Sys_ID { get; set; } public ICollection<ImpedanceEl> ImpedanceEl { get; set; } } } namespace Power_System_Simulation_Harmonics { public class SystemContext : System.Data.Entity.DbContext { public SystemContext() : base() { } //PROBLEM OCCURS IN THE NEXT TWO LINES. public DbSet<ImpedanceEl> ImpedanceElements { get; set; } public DbSet<Power_Sys> PowerSystems { get; set; } } }

    I'm having some trouble with the code above.

    I'm working through the database tutorial at:

    http://www.entityframeworktutorial.net/code-first/setup-entity-framework-code-first-environment.aspx

    .

    It appears that I am missing a "class" definition,

    but I have defined the two classes above the code that has the problem.

    This seems a little mysterious.

    Can anyone help (please).

    Thank you.

    Thursday, September 17, 2015 3:00 PM

Answers

  • Hi, 

    The events should be put insides the class. 

    you must know the sequence as below.

    namspace -> class -> [function, event, property, fields]

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.Entity;
    
    namespace Harmony2015
    {
        public partial class BuildModel : Form
        {
            public BuildModel()
            {
                InitializeComponent();
            }
    		
    		private void groupBox3_Enter(object sender, EventArgs e)
    		{
    
    		}
    
    		private void groupBox4_Enter(object sender, EventArgs e)
    		{
    
    		}
    
    
    		private void label8_Click(object sender, EventArgs e)
    		{
    
    		}
    
    		private void groupBox1_Enter(object sender, EventArgs e)
    		{
    
    		}
    
    		private void label12_Click(object sender, EventArgs e)
    		{
    
    		}
    
    		private void groupBox7_Enter(object sender, EventArgs e)
    		{
    
    		}
    
    		private void label24_Click(object sender, EventArgs e)
    		{
    
    		}
    
    		private void label28_Click(object sender, EventArgs e)
    		{
    
    		}
    
    		private void textBox44_TextChanged(object sender, EventArgs e)
    		{
    		}
    
    		private void textBox39_TextChanged(object sender, EventArgs e)
    		{
    
    		}
    
    		private void BuildModel_Load(object sender, EventArgs e)
    		{
    
    		}
    
    		private void conductor_TextChanged(object sender, EventArgs e)
    		{
    
    		}
    
        }
    
            public class ImpedanceEl
            {
                public ImpedanceEl()
                {
                }
                public int Id { get; set; }
                public int Length { get; set; }
                public double Re_X { get; set; } // I am not sure what type of value impedance has
            }
            public class Power_Sys
            {
                public Power_Sys()
                {
                }
                public string Power_Sys_ID { get; set; }
    
    
                public ICollection<ImpedanceEl> ImpedanceEl { get; set; }
    
            }
        namespace Power_System_Simulation_Harmonics
        {
    
            public class SystemContext : System.Data.Entity.DbContext
            {
                public SystemContext() : base()
                {
    
                }
    
    
                public DbSet<ImpedanceEl> ImpedanceElements { get; set; }
                public DbSet<Power_Sys> PowerSystemElements { get; set; }
    
            }
        }
    }
    Thanks

    Friday, September 18, 2015 12:39 AM

All replies

  • Hi,

    both of the classes need to located outisde Buildmodel,

    Please refer the code below:

    namespace Harmony2015
    {
        public partial class BuildModel : Form
        {
            public BuildModel()
            {
                //InitializeComponent();
            }
        }
    
        public class ImpedanceEl
        {
                public ImpedanceEl()
                {
                }
                public int Id { get; set; }
                public int Length { get; set; }
                public double Re_X { get; set; } // I am not sure what type of value impedance has
         }
         
         public class Power_Sys
         {
                public Power_Sys()
                {
                }
                public string Power_Sys_ID { get; set; }
    
    
                public ICollection<ImpedanceEl> ImpedanceEl { get; set; }
    
         }
        namespace Power_System_Simulation_Harmonics
        {
    
            public class SystemContext : System.Data.Entity.DbContext
            {
                public SystemContext() : base()
                {
    
                }
    //PROBLEM OCCURS IN THE NEXT TWO LINES.
                public DbSet<ImpedanceEl> ImpedanceElements { get; set; }
                public DbSet<Power_Sys> PowerSystems { get; set; }
    
            }
        }
    }
    

    thanks.

    Thursday, September 17, 2015 3:57 PM
  • The compiler is still not happy.  When I click on the problematic lines, it says:

    DBSet<ImpedanceEl><invalid global code>.ImpedanceElements(get;set;)

    It adds:  A namespace cannot contain members such as fields or methods.

    It is also giving me problems with the lines of code it writes for the textboxes I entered.  It seems to have the same problem.  I suspect a bracket out of place.

    Code is below:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.Entity;
    
    namespace Harmony2015
    {
        public partial class BuildModel : Form
        {
            public BuildModel()
            {
                InitializeComponent();
            }
        }
        public class ImpedanceEl
        {
            public ImpedanceEl()
            {
            }
            public int Id { get; set; }
            public int Length { get; set; }
            public double Re_X { get; set; } // I am not sure what type of value impedance has
        }
        public class Power_Sys
        {
            public Power_Sys()
            {
            }
            public string Power_Sys_ID { get; set; }
    
    
            public ICollection<ImpedanceEl> ImpedanceEl { get; set; }
    
        }
        namespace Power_System_Simulation_Harmonics
        {
    
            public class SystemContext : System.Data.Entity.DbContext
            {
                public SystemContext() : base()
                {
    
                }
               
            }
            public DbSet<ImpedanceEl>ImpedanceElements{ get; set; }
            public DbSet<Power_Sys> PowerSystemElements { get; set; }
    
            }
        }
    
        private void groupBox3_Enter(object sender, EventArgs e)
            {
    
            }
    
            private void groupBox4_Enter(object sender, EventArgs e)
            {
    
            }
    
    
            private void label8_Click(object sender, EventArgs e)
            {
    
            }
    
            private void groupBox1_Enter(object sender, EventArgs e)
            {
    
            }
    
            private void label12_Click(object sender, EventArgs e)
            {
    
            }
    
            private void groupBox7_Enter(object sender, EventArgs e)
            {
    
            }
    
            private void label24_Click(object sender, EventArgs e)
            {
    
            }
    
            private void label28_Click(object sender, EventArgs e)
            {
    
            }
    
            private void textBox44_TextChanged(object sender, EventArgs e)
            {
                       }
    
            private void textBox39_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void BuildModel_Load(object sender, EventArgs e)
            {
    
            }
    
            private void conductor_TextChanged(object sender, EventArgs e)
            {
    
            }
        }
    }
    

    Thursday, September 17, 2015 5:54 PM
  • There is definitely a problem with the brackets.  I simply can not identify it.  It starts giving me errors when it defines "groupbox" code elements, saying a namespace can not directly contain members such as fields or methods.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.Entity;
    
    namespace Harmony2015
    {
        public partial class BuildModel : Form
        {
            public BuildModel()
            {
                InitializeComponent();
            }
        }
    
            public class ImpedanceEl
            {
                public ImpedanceEl()
                {
                }
                public int Id { get; set; }
                public int Length { get; set; }
                public double Re_X { get; set; } // I am not sure what type of value impedance has
            }
            public class Power_Sys
            {
                public Power_Sys()
                {
                }
                public string Power_Sys_ID { get; set; }
    
    
                public ICollection<ImpedanceEl> ImpedanceEl { get; set; }
    
            }
        namespace Power_System_Simulation_Harmonics
        {
    
            public class SystemContext : System.Data.Entity.DbContext
            {
                public SystemContext() : base()
                {
    
                }
    
    
                public DbSet<ImpedanceEl> ImpedanceElements { get; set; }
                public DbSet<Power_Sys> PowerSystemElements { get; set; }
    
            }
        }
    
        private void groupBox3_Enter(object sender, EventArgs e)
        {
    
        }
    
        private void groupBox4_Enter(object sender, EventArgs e)
        {
    
        }
    
    
        private void label8_Click(object sender, EventArgs e)
        {
    
        }
    
        private void groupBox1_Enter(object sender, EventArgs e)
        {
    
        }
    
        private void label12_Click(object sender, EventArgs e)
        {
    
        }
    
        private void groupBox7_Enter(object sender, EventArgs e)
        {
    
        }
    
        private void label24_Click(object sender, EventArgs e)
        {
    
        }
    
        private void label28_Click(object sender, EventArgs e)
        {
    
        }
    
        private void textBox44_TextChanged(object sender, EventArgs e)
        {
        }
    
        private void textBox39_TextChanged(object sender, EventArgs e)
        {
    
        }
    
        private void BuildModel_Load(object sender, EventArgs e)
        {
    
        }
    
        private void conductor_TextChanged(object sender, EventArgs e)
        {
    
        }
    
    }


    • Edited by LingJingLing Thursday, September 17, 2015 5:57 PM
    Thursday, September 17, 2015 5:55 PM
  • What is a "namespace"?  Is my definition of the classes in the right spot?  Should I put is inside a button?  I need to define these classes for the entire project.  This is only one screen of many.  In this case, it is where the model is to be built and stored in a database.
    Thursday, September 17, 2015 5:59 PM
  • What is a "namespace"?

    http://www.csharp-station.com/Tutorial/CSharp/Lesson06

    And the code you have posted is using a namespace at the top of the class, which is in namespace Harmony2015.

    Thursday, September 17, 2015 7:30 PM
  • Hi, 

    The events should be put insides the class. 

    you must know the sequence as below.

    namspace -> class -> [function, event, property, fields]

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.Entity;
    
    namespace Harmony2015
    {
        public partial class BuildModel : Form
        {
            public BuildModel()
            {
                InitializeComponent();
            }
    		
    		private void groupBox3_Enter(object sender, EventArgs e)
    		{
    
    		}
    
    		private void groupBox4_Enter(object sender, EventArgs e)
    		{
    
    		}
    
    
    		private void label8_Click(object sender, EventArgs e)
    		{
    
    		}
    
    		private void groupBox1_Enter(object sender, EventArgs e)
    		{
    
    		}
    
    		private void label12_Click(object sender, EventArgs e)
    		{
    
    		}
    
    		private void groupBox7_Enter(object sender, EventArgs e)
    		{
    
    		}
    
    		private void label24_Click(object sender, EventArgs e)
    		{
    
    		}
    
    		private void label28_Click(object sender, EventArgs e)
    		{
    
    		}
    
    		private void textBox44_TextChanged(object sender, EventArgs e)
    		{
    		}
    
    		private void textBox39_TextChanged(object sender, EventArgs e)
    		{
    
    		}
    
    		private void BuildModel_Load(object sender, EventArgs e)
    		{
    
    		}
    
    		private void conductor_TextChanged(object sender, EventArgs e)
    		{
    
    		}
    
        }
    
            public class ImpedanceEl
            {
                public ImpedanceEl()
                {
                }
                public int Id { get; set; }
                public int Length { get; set; }
                public double Re_X { get; set; } // I am not sure what type of value impedance has
            }
            public class Power_Sys
            {
                public Power_Sys()
                {
                }
                public string Power_Sys_ID { get; set; }
    
    
                public ICollection<ImpedanceEl> ImpedanceEl { get; set; }
    
            }
        namespace Power_System_Simulation_Harmonics
        {
    
            public class SystemContext : System.Data.Entity.DbContext
            {
                public SystemContext() : base()
                {
    
                }
    
    
                public DbSet<ImpedanceEl> ImpedanceElements { get; set; }
                public DbSet<Power_Sys> PowerSystemElements { get; set; }
    
            }
        }
    }
    Thanks

    Friday, September 18, 2015 12:39 AM