Jump to content

Making code into Model View Controller format

- - - - -

  • Please log in to reply
2 replies to this topic

#1
rubbadrools

rubbadrools

    Newbie

  • Members
  • PipPip
  • 12 posts
Hello!
I have a problem and I hope someone here can help me :)
This code below is a code I wrote with some help from the guys here at codecall.
My issue is that it's all in one class and I intend to make it into MVC format...
But when I try to do this a bunch of errors occur and I have now given up :P
Anyone that mind helping me set up the different classes?
Would be much appriecated!


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;


namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        Random rand = new Random();

        List<int> RemainNums;

        int ARandomNumber;

        Timer timer = new Timer();

        private int counter = 0; 

        


        public Form1()

        {

            InitializeComponent(); 

            

            timer.Interval = 350;

            timer.Tick += new EventHandler(timer_Tick); 


            

            pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

            pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

            pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

            pictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

            PopulateCards();

        }

            

        public void timer_Tick(object sender, EventArgs e)    

        {

           counter++;

           if (counter == 1) 

            //or whatever amount of time you want it to be invisible            

        {

            pictureBox1.Visible = true;

            

        }

        if (counter == 2)

        {

            pictureBox2.Visible = true;

         

        }

        if (counter == 3)

        {

            pictureBox3.Visible = true;

        }

        if (counter == 4)

        {

            pictureBox4.Visible = true;

            timer.Stop();

            counter = 0;

            

        }

    } 



        private void PopulateCards()

            {

            

            RemainNums = new List<int>();

            RemainNums.AddRange(new int[] { 0, 1, 2, 3, 4, 5, 6, 7,

                8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,

                22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51  });

        }


       


        private void button1_Click(object sender, EventArgs e)

        {

            timer.Start();

            pictureBox1.Visible = false; 

            pictureBox2.Visible = false; 

            pictureBox3.Visible = false; 

            pictureBox4.Visible = false; 


            int index = rand.Next(0, RemainNums.Count - 1);

            ARandomNumber = RemainNums[index];

            RemainNums.RemoveAt(index);


            string path = @"\\Fs\Homedir\sys09gma\Desktop\Kortlek\Kortlek\";

            pictureBox1.Image = Image.FromFile(path + ARandomNumber + ".png");

            index = rand.Next(0, RemainNums.Count - 1);

            ARandomNumber = RemainNums[index];

            RemainNums.RemoveAt(index);

            pictureBox2.Image = Image.FromFile(path + ARandomNumber + ".png");

            index = rand.Next(0, RemainNums.Count - 1);

            ARandomNumber = RemainNums[index];

            RemainNums.RemoveAt(index);

            pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");

            index = rand.Next(0, RemainNums.Count - 1);

            ARandomNumber = RemainNums[index];

            RemainNums.RemoveAt(index);

            pictureBox4.Image = Image.FromFile(path + ARandomNumber + ".png");

            Console.WriteLine(ARandomNumber);

            if (RemainNums.Count <= 1){

                RemainNums.Clear();

                PopulateCards();

            }

        }


        private void pictureBox2_Click(object sender, EventArgs e)

        {


        }


        private void pictureBox3_Click(object sender, EventArgs e)

        {


        }


        private void pictureBox4_Click(object sender, EventArgs e)

        {


        }


        private void pictureBox1_Click(object sender, EventArgs e)

        {


        }


        private void Form1_Load(object sender, EventArgs e)

        {


        }

    }

}



Thanks in advance!

#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
First of all you will need to set up an MVC project. Next, If you are doing strict MVC you will need to set up your controllers, models, views and then make sure your routes are set up for the controller correctly in the global.asax file. For great information on MVC you should check out Scott Guthrie's blog here: MVC - ScottGu's Blog. Also, you can do some searching for example MVC projects.
-CDG10620
Software Developer

#3
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
Just took a quick look at your code. Not sure it's MVC specifically that you're looking for. Or maybe just a little re-factoring.
Sometimes separating code into specific domains, can be really helpful, especially in terms of maintainability.
A lot of the time, it's a little overrated.

Consider, will this code ever be used again? Is it likely that it needs to be really scalable?

don't get me wrong, there's always worth in going over things you've written, to try and make it better. That's one way we learn.

Anyways, in this example, comments would do wonders; also try breaking your code up some. And try to keep from implementing things in your events. Instead, build that functionality into methods, that are called from events.

quick example:
change this:

private void button1_Click(object sender, EventArgs e) {

            timer.Start();

            pictureBox1.Visible = false;

            pictureBox2.Visible = false;

            pictureBox3.Visible = false;

            pictureBox4.Visible = false;


            int index = rand.Next(0, RemainNums.Count - 1);

            ARandomNumber = RemainNums[index];

            RemainNums.RemoveAt(index);


            string path = @"\\Fs\Homedir\sys09gma\Desktop\Kortlek\Kortlek\";

            pictureBox1.Image = Image.FromFile(path + ARandomNumber + ".png");

            index = rand.Next(0, RemainNums.Count - 1);

            ARandomNumber = RemainNums[index];

            RemainNums.RemoveAt(index);

            pictureBox2.Image = Image.FromFile(path + ARandomNumber + ".png");

            index = rand.Next(0, RemainNums.Count - 1);

            ARandomNumber = RemainNums[index];

            RemainNums.RemoveAt(index);

            pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");

            index = rand.Next(0, RemainNums.Count - 1);

            ARandomNumber = RemainNums[index];

            RemainNums.RemoveAt(index);

            pictureBox4.Image = Image.FromFile(path + ARandomNumber + ".png");

            Console.WriteLine(ARandomNumber);

            if (RemainNums.Count <= 1) {

                RemainNums.Clear();

                PopulateCards();

            }

        }



into something like this:

private void button1_Click(object sender, EventArgs e) {

            cycle_cards();

        }


        private void cycle_cards() {

            timer.Start();

            hide_controls();

            string path = @"\\Fs\Homedir\sys09gma\Desktop\Kortlek\Kortlek\";

            pictureBox1.Image = Image.FromFile(path + get_next() + ".png");

            pictureBox2.Image = Image.FromFile(path + get_next() + ".png");

            pictureBox3.Image = Image.FromFile(path + get_next() + ".png");

            pictureBox4.Image = Image.FromFile(path + get_next() + ".png");

            Console.WriteLine(ARandomNumber);

            if (RemainNums.Count <= 1) {

                RemainNums.Clear();

                PopulateCards();

            }

        }


        private void hide_controls() {

            pictureBox1.Visible = false;

            pictureBox2.Visible = false;

            pictureBox3.Visible = false;

            pictureBox4.Visible = false;

        }


        private int get_next() {

            int index = rand.Next(0, RemainNums.Count - 1);

            int rand_num = RemainNums[index];

            RemainNums.RemoveAt(index);

            return rand_num;

        }


well... you get the idea...
it makes it so you can call bits of functionality from where ever, and notice, there is much less repeated logic. this is more manageable, and less error prone, especially as updates are made to the code.

Also, try playing with :


#region [Events]


//stick your events here


#endregion


#region [Application Logic]


//stick logic stuff here


#endregion


#region [UI helpers]


//stick things like hide controls, and show controls here


#endregion



if you have a lot of things happening in one place. separating them with collapsible regions can also help keep your head straight.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users