Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I'm looking for instructions to convert a demo project into a class

I'm looking for instructions to convert a demo project into a class

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    Hi, In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that. I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times. If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help. Thanks

    OriginalGriffO V 2 Replies Last reply
    0
    • T turbosupramk3

      Hi, In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that. I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times. If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help. Thanks

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      I'm not planning on even looking at 1000 lines of your code - I don't have the time - but I'd start by looking at creating a UserControl[^] rather than a generic class - they display directly. And then either embedding a DataGridView or deriving from one. If you are planning on using it in tabs, I'd also consider creating another UserControl, derived from TabPage which encapsulated the DataGridView UserControl to make that easier to use as well. How easy this is for you to do will depend on how you wrote the original code: it may be worth your creating a new project and "playing with it" until you get the idea before you start launching into moving existing code. I use UserControls a lot - they help to contain code and simplify interfaces between modules. Worth looking at!

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      T 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        I'm not planning on even looking at 1000 lines of your code - I don't have the time - but I'd start by looking at creating a UserControl[^] rather than a generic class - they display directly. And then either embedding a DataGridView or deriving from one. If you are planning on using it in tabs, I'd also consider creating another UserControl, derived from TabPage which encapsulated the DataGridView UserControl to make that easier to use as well. How easy this is for you to do will depend on how you wrote the original code: it may be worth your creating a new project and "playing with it" until you get the idea before you start launching into moving existing code. I use UserControls a lot - they help to contain code and simplify interfaces between modules. Worth looking at!

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        T Offline
        T Offline
        turbosupramk3
        wrote on last edited by
        #3

        Thanks for the reply. I read the MS article, and I also found this video. This video seems a little more understandable for me being new to this, as I can see it done, would this also be a good representation of what you are talking about? https://www.youtube.com/watch?v=l5L_q_jI494[^]

        OriginalGriffO 1 Reply Last reply
        0
        • T turbosupramk3

          Thanks for the reply. I read the MS article, and I also found this video. This video seems a little more understandable for me being new to this, as I can see it done, would this also be a good representation of what you are talking about? https://www.youtube.com/watch?v=l5L_q_jI494[^]

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Difficult to tell - his voice and the microphone "wind noise" was so annoying I couldn't watch that much! :laugh: Seriously, give it a try on a "new project" and see what happens. It really does help with simplifying interfaces between modules and speeds up development too.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          T 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Difficult to tell - his voice and the microphone "wind noise" was so annoying I couldn't watch that much! :laugh: Seriously, give it a try on a "new project" and see what happens. It really does help with simplifying interfaces between modules and speeds up development too.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            T Offline
            T Offline
            turbosupramk3
            wrote on last edited by
            #5

            :laugh: That means you didn't make it to 4:20 as about that point a random hammer starts banging the wall behind him ... or he is residing in a cheap motel, I'm not sure? I've started with a new project and a user control class. I can't get the class to instantiate, but I will keep try and report back if I fail. Thanks.

            OriginalGriffO 1 Reply Last reply
            0
            • T turbosupramk3

              :laugh: That means you didn't make it to 4:20 as about that point a random hammer starts banging the wall behind him ... or he is residing in a cheap motel, I'm not sure? I've started with a new project and a user control class. I can't get the class to instantiate, but I will keep try and report back if I fail. Thanks.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Build the project, and it should add the new control to the top of the toolbox. Drag and drop one onto your form, then look at the designer.cs file - it'll show you the code VS uses to instantiate it.

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              T 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Build the project, and it should add the new control to the top of the toolbox. Drag and drop one onto your form, then look at the designer.cs file - it'll show you the code VS uses to instantiate it.

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                T Offline
                T Offline
                turbosupramk3
                wrote on last edited by
                #7

                Ok, I tried the code below but it did not work and my basic class is at the bottom. Under designer.cs, should I be using some of the code under InitializeComponent()?

                        dataGridViewUserControl1 dgv1 = new dataGridViewUserControl1();
                        dgv1.ColumnCount = 25;
                        dgv1.RowCount = 20;
                

                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.Drawing;
                using System.Data;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using System.Windows.Forms;

                namespace UserControlDemo
                {
                public partial class dataGridViewUserControl1 : UserControl
                {
                public dataGridViewUserControl1()
                {
                InitializeComponent();
                }

                    private int columnCount;
                
                    public int ColumnCount
                    {
                        get { return columnCount; }
                        set { columnCount = value; }
                    }
                
                    private int rowCount;
                
                    public int RowCount
                    {
                        get { return rowCount; }
                        set { rowCount = value; }
                    }
                
                    private void UserControl1\_Load(object sender, EventArgs e)
                    {
                        // create datagridviewcolumns
                        for (int i = 0; i < columnCount; i++)
                        {
                            dataGridView1.Columns\[i\].Name = "column " + i;
                        }
                
                        // create datagridviewrows
                        for (int i = 0; i < rowCount; i++)
                        {
                            string\[\] rowName = new string\[\] { "row" + i.ToString() };
                            dataGridView1.Rows.Add(rowName);
                        }
                    }
                }
                

                }

                OriginalGriffO 1 Reply Last reply
                0
                • T turbosupramk3

                  Ok, I tried the code below but it did not work and my basic class is at the bottom. Under designer.cs, should I be using some of the code under InitializeComponent()?

                          dataGridViewUserControl1 dgv1 = new dataGridViewUserControl1();
                          dgv1.ColumnCount = 25;
                          dgv1.RowCount = 20;
                  

                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Drawing;
                  using System.Data;
                  using System.Linq;
                  using System.Text;
                  using System.Threading.Tasks;
                  using System.Windows.Forms;

                  namespace UserControlDemo
                  {
                  public partial class dataGridViewUserControl1 : UserControl
                  {
                  public dataGridViewUserControl1()
                  {
                  InitializeComponent();
                  }

                      private int columnCount;
                  
                      public int ColumnCount
                      {
                          get { return columnCount; }
                          set { columnCount = value; }
                      }
                  
                      private int rowCount;
                  
                      public int RowCount
                      {
                          get { return rowCount; }
                          set { rowCount = value; }
                      }
                  
                      private void UserControl1\_Load(object sender, EventArgs e)
                      {
                          // create datagridviewcolumns
                          for (int i = 0; i < columnCount; i++)
                          {
                              dataGridView1.Columns\[i\].Name = "column " + i;
                          }
                  
                          // create datagridviewrows
                          for (int i = 0; i < rowCount; i++)
                          {
                              string\[\] rowName = new string\[\] { "row" + i.ToString() };
                              dataGridView1.Rows.Add(rowName);
                          }
                      }
                  }
                  

                  }

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  If you look at the designer file, you'll find some more code: of nothing else there will be a Controls.Add method call to add the new control instance to the forms Controls collection - that's the line which actually makes it display within the form. (For your app, it'll eventually be in the TabPage.Controls collection to make it a "child" of that control rather than the firm itself, but that's the advanced bit :laugh: )

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • T turbosupramk3

                    Hi, In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that. I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times. If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help. Thanks

                    V Offline
                    V Offline
                    V 0
                    wrote on last edited by
                    #9

                    you´re graduating and you have no clue how to develop something more descent than this? Don´t get me wrong, I like the fact you´re willing to improve, but when I was graduating I had to do way and way and way better then this... :confused:

                    V.
                    (MQOTD rules and previous solutions)

                    M T 2 Replies Last reply
                    0
                    • V V 0

                      you´re graduating and you have no clue how to develop something more descent than this? Don´t get me wrong, I like the fact you´re willing to improve, but when I was graduating I had to do way and way and way better then this... :confused:

                      V.
                      (MQOTD rules and previous solutions)

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #10

                      I don't think graduating is used in an educational context in this instance, I think he is just trying to become (graduate to) a better developer.

                      Never underestimate the power of human stupidity RAH

                      1 Reply Last reply
                      0
                      • V V 0

                        you´re graduating and you have no clue how to develop something more descent than this? Don´t get me wrong, I like the fact you´re willing to improve, but when I was graduating I had to do way and way and way better then this... :confused:

                        V.
                        (MQOTD rules and previous solutions)

                        T Offline
                        T Offline
                        turbosupramk3
                        wrote on last edited by
                        #11

                        Not from school, you misread. I'm self taught and trying to graduate to the next level.

                        V 1 Reply Last reply
                        0
                        • T turbosupramk3

                          Not from school, you misread. I'm self taught and trying to graduate to the next level.

                          V Offline
                          V Offline
                          V 0
                          wrote on last edited by
                          #12

                          OK apologies. In that case, best way to learn is go through a good book. Digest it from A to Z and do all the exercises starting with the "Hello World" program. Tutorials on the internet are not always that good. Also look up encapsulation, inheritence and polymorphism, the corner stones for OO programming. That should help you getting a good structure in your application. Hope this helps.

                          V.
                          (MQOTD rules and previous solutions)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups