Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 12th, 2008, 08:05 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

@chodo2 -

I linked to a full example of the MVC implementation in my previous post. You can download that and see a full working example with code.

-Peter
compiledthoughts.com
 
Old September 12th, 2008, 08:41 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Quote:
quote:Originally posted by planoie
 @chodo2 -

I linked to a full example of the MVC implementation in my previous post.  You can download that and see a full working example with code.

-Peter
compiledthoughts.com
Ya I downloaded it and I still don't know how it works. Like first I don't know the namespace needed.

When I went my file and put say this in.

private Model model;

It would not recognize "Model" as a keyword and it would bitch.

 
Old September 12th, 2008, 08:52 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The Model class is a class that I created as part of the sample. That represents your data model. You need to create a class that defines the properties that are meaningful to your application.

I'm sorry, if I did not explain this thoroughly enough before.

-Peter
compiledthoughts.com
 
Old September 13th, 2008, 02:05 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

The button click event code for form1 is shown below. The code assumes that the array named myValues[] holds the data you need to pass to the second form.

    private void btnNextForm_Click(object sender, EventArgs e)
    {
        bool eflag;

        eflag = int.TryParse(txtValue1.Text, out myValues[0]);
        if (eflag == false)
        {
            MessageBox.Show("Input error 1");
            return;
        }
        eflag = int.TryParse(txtValue2.Text, out myValues[1]);
        if (eflag == false)
        {
            MessageBox.Show("Input error 2");
            return;
        }

        FrmForm2 frm = new FrmForm2(myValues); // Form2 instantiated
        frm.ShowDialog();

    }

The constructor code and return-to-form1 button code is shown here:



    int[] temp; // To hold array from form1

    public FrmForm2() // Default constructor
    {
        InitializeComponent();
    }

    public FrmForm2(int[] values) // Parametized constructor
    {
        InitializeComponent();

        temp = values; // Make available to form

        txtPassedInValue1.Text = temp[0].ToString(); // Show values on screen
        txtPassedInValue2.Text = temp[1].ToString();
    }

    private void btnReturn_Click(object sender, EventArgs e)
    {
        temp[3] = 50; // Place your new value in the array here
        Close();
    }


Notice that the line of code in form1 that instantiates the second form:

        FrmForm2 frm = new FrmForm2(myValues); // Form2 instantiated

passes in an array of integer values named myValues[]. The Parametized contructor receives this array as values[] and copies it to the array named temp[] defined in form2. This is where the magic takes place. Because we did not use the new keyword to define temp[], it actually is a pointer variable. Therefore, when the statement:

   temp = values;

completes, temp[] has the same memory address as values[]. However, since values[] is the same as myValues[] in form1, temp[] and myValues[] occupy the same space in memory. Therefore, anything you do in form2 is going to permanently change myValues[] in form1. This is exactly what you want to do.

We use the btnReturn in form2 to set a new values into the array and close form2. When control returns to form1, you will see that element 3 of myValues[3] is now equal to 50.

Jack



Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old September 13th, 2008, 04:12 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Dr. Jack,

Have you by chance looked at the sample I linked to? I'd be curious to get your feedback on the technique. Your and my approaches are theoretically similar, however, if you look at my example, you'll notice that there is hardly any hand written code in it. Where you have an array of values, I'm simply using a class instance with strongly typed and named properties. Also, the built in databinding technology automatically handles all the linking and updating of the values in the controls with their data source.

I'm also intrigued by your suggestion to use a value array for managing the values between the forms. You have mentioned (more than once) your book which is obviously about OO programming. Yet, you are suggesting a fairly primitive type rather than a class to represent a set of data. I'm interested to hear your thoughts on this as well.

-Peter
compiledthoughts.com
 
Old September 15th, 2008, 12:06 AM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Plaoie I have still some questions about your way.

First is there any books on this MVC stuff it seems pretty neat and I am sure there is alot more too it. Also what you really use it for and how it relates to everything else.

Also I must be blind I can't believe I missed the connection with the Model class and the Model keyword.

I guess it is because you where not using the "new" keyword but you are somehow referencing that class and nothing in that class is static.

How does that work anyways? I thought you need to either declare it static or make a new object of it.

 
Old September 15th, 2008, 10:54 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by chobo2
 First is there any books on this MVC stuff it seems pretty neat and I am sure there is alot more too it. Also what you really use it for and how it relates to everything else.

A very good book that covers MVC as well as many other design patterns is O'Reilly's Head First Design Patterns. I have read this book cover to cover several times. Understanding patterns can be tricky when you don't have a sensible context to use them in. I'm currently working on a Windows forms application and have found very good uses for many of the patterns that are found in the book. One example of how I'm using the MVC pattern is where I have several "views" of the same set of data. One view is a search view, one is a "most recently changed" view, another is a calendar view. Ultimately, each view displays some information about the model items and triggers an when the use wishes to act on one of the items, however each view is nothing more than a different logical and visual presentation of the same data (model). This fits perfectly into the model-view-controller design where you can swap out any view for any other view that conforms to a given set of rules (or design contract), in this case an interface that defines the common expected members (methods, properties and events).

Quote:
quote:Originally posted by chobo2
 Also I must be blind I can't believe I missed the connection with the Model class and the Model keyword.

I guess it is because you where not using the "new" keyword but you are somehow referencing that class and nothing in that class is static.
I'm not sure what "Model" keyword you are referring to. I'm not familiar with one. Are you thinking of "Module"?

I am using a new instance of the Model class. It's kind of buried in the program startup code, in the program.cs file in the sample:
Code:
    Application.Run(new View(new Model()));
The startup form is a new instance of the "View" form, which is passed a new instance of "Model".

-Peter
compiledthoughts.com
 
Old September 15th, 2008, 05:31 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

Hi Peter:

Your way works just fine and doesn't have all that much code in it. I chose my approach for several reasons. First, my text is a beginning book and I gauged from the person posing the question that they were at that level, too. The approach I used is pretty direct and easily understood by a beginner. Also, if they wanted further info, they could go to my book for some details. Second, my approach does away with the overhead associated with a class. I reasoned that, since the inputs were coming from a form's textbox, he would be checking the inputs himself a probably not using getters and setters. (My code shrinks considerably if I remove the TryParse() blocks.) Third, he implied a single data type for the information (indeed, never mentioned more than one piece of input data) so its a simple task to pass in a matching array. Using a pointer in form2 to reference it is about as efficient as you can get for accessing the data from form1. (I also use this approach to teach the difference between value types and reference types.) Unless a single-property class does something really tricky in one of its methods, I usually shy away from a class for a single property.

As to complexity and lines of code, it's not all that unusual to find more lines of code easier to read and understand than fewer lines of code that use complex expressions. In the final analysis, it's a matter of choice in many situations.



Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Forms sambathrajmca Windows Workflow 1 November 27th, 2007 06:25 AM
windows forms lakshmiR .NET Framework 1.x 1 August 31st, 2007 08:32 PM
Help with Windows Forms tycotrix C# 1 January 16th, 2007 11:58 AM
windows forms chandrasekhar ASP.NET 2.0 Professional 2 February 27th, 2006 10:41 PM
Windows forms eyan C# 1 July 1st, 2004 04:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.