Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > BOOK: Beginning Object-Oriented Programming with C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Object-Oriented Programming with C#
This is the forum to discuss the Wrox book Beginning Object-Oriented Programming with C# by Jack Purdum; ISBN: 978-1-1183-3692-2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Object-Oriented Programming with C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 4th, 2014, 09:07 PM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Location: Suzhou
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default clsInBetweenRules - Question :)

Hi there,

I've written some scripts in VBScript the last years. Since a few month now speeding up with Powershell and getting more interested in .Net. - I bought this book and I think it is quiet good to help me starting with OOP.

I'm now in the chapter which introduces the clsInBetweenRules. Is this already a MVC architecture?


I don't understand the following part:

In the frmMain.cs I have the following line to create an object of the clsInBetweenRules:

Code:
 clsInBetweenRules myRules = new clsInBetweenRules();
By clicking the btn_Deal I call a method of this object named "Dealhand"


Code:
  myRules.DealHand(cards, ref betResult, ref position);
cards is and empty array defined above as
Code:
 string [] cards = new string[3];
betResult and Position are just references to the two variables also declared above in the public class frmMain : Form section. The reference means that the method DealHand will change the variable-values here in this class.

Within the clsInBetweenRules.cs I find the method DealHand as
Code:
 public void DealHand(string[] hand, ref int outCome, ref int position)
    {
        ...
        SetCards(hand);
        ...
    }
The SetCards(hand) methods is declared as:
Code:
    private void SetCards(string[] hand)
    {
        int temp;
        hand[0] = myDeck.getCardPip(lowCardIndex);
        hand[1] = myDeck.getCardPip(hiCardIndx);
        hand[2] = myDeck.getCardPip(dealtCardIndex);

        if (lowCard == hiCard || lowCard < hiCard)
        {
            hand[0] = myDeck.getCardPip(lowCardIndex);
            hand[1] = myDeck.getCardPip(hiCardIndx);
        }
        else
        {
            temp = hiCard;
            hiCard = lowCard;
            lowCard = temp;

            temp = hiCardIndx;
            hiCardIndx = lowCardIndex;
            lowCardIndex = temp;

            hand[0] = myDeck.getCardPip(lowCardIndex);
            hand[1] = myDeck.getCardPip(hiCardIndx);
        }
    }
What is the reason for the hand / cards array? The setCards method is void, means returns nothing. The parameters which are passed as values, but nothing further happens?

Hope to read from you soon :)

Many thanks in advance


Juan
  #2 (permalink)  
Old August 7th, 2014, 09:12 PM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

all classes in C# are reference variables therefore are passed by reference so when passing the hand array you are passing a reference to the array and when element values are set in the function you are setting element value of the array that is in the caller code block.

private void SetCards(string[] hand)
{
int temp;
hand[0] = myDeck.getCardPip(lowCardIndex);
hand[1] = myDeck.getCardPip(hiCardIndx);
hand[2] = myDeck.getCardPip(dealtCardIndex);
.
.
.

}

// callers code block
public void DealHand(string[] hand, ref int outCome, ref int position)
{
...
SetCards(hand); <-- the hand[] first 3 elements will point to what ever card that the this method sets them to.
...
}

so if SetCards sets hand[0] is set to card A in the function then after the function call exits the hand[0] in the DealHand code block will point to card A.

Last edited by mmorgan30; August 7th, 2014 at 09:29 PM.
  #3 (permalink)  
Old August 11th, 2014, 02:12 AM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Location: Suzhou
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mmorgan30,

thanks for your answer, I think I understand. So within the DealHand method I would be able to work with the values I received from the SetCards method?

So within the DealHand I could just say:
Code:
string HandOne = hand[0]
and I would get the value which was specified in the SetCards?

This makes me a bit confused. Why shall I use then references (e.g. ref int a) if references are automatically done within c#?


Hope you can help me to solve this riddle as well ;-)


Thanks


Juan
  #4 (permalink)  
Old August 12th, 2014, 09:22 AM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Location: Suzhou
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mmorgan30,

I made a simple test and it doesn't work-out. Please help me to understand my misunderstanding.

According to the book a string and an array are passed as references; like you also mentioned.

The following codes does not work like I expect, strMain is still the old value:
Code:
using System;

namespace Testreferencetypes_stringandarray
{
	class ClsTstString
	{
		public void ChangeString(string strVar){
			strVar = "after";
		}

	}
	class MainClass
	{
		public static void Main (string[] args)
		{
			string strMain = "before";
			Console.WriteLine (strMain);

			ClsTstString TstStrObj = new ClsTstString ();
			TstStrObj.ChangeString (strMain);
			Console.WriteLine (strMain);

		}
	}
}
The following lines are working fine, which I understand:

Code:
using System;

namespace Testreferencetypes_stringandarray
{
	class ClsTstString
	{
		public string strNv;
		public void ChangeString(string strVar){
			strVar = "after";
			strNv = strVar;
		}

	}
	class MainClass
	{
		public static void Main (string[] args)
		{
			string strMain = "before";
			Console.WriteLine (strMain);

			ClsTstString TstStrObj = new ClsTstString ();
	
			//strMain is changed which is understandable, value is newly assigned
			strMain = TstStrObj.strNv;
			Console.WriteLine (strMain);
		}
	}
}

Thank you very much


Juan
  #5 (permalink)  
Old August 12th, 2014, 08:32 PM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

take a look at this example

Code:

namespace Program
{
    public class Class1
    {
        public string MyString { get; set; }
    }

    public class Program
    {
        static void Main( String[] args )
        {
            string testStr1 = "MainMethod String Value";
            string[] testStrArray = new string[] { testStr1 };
            Class1 testClass = new Class1( ) { MyString = "MainMethod String Value" };

            Console.WriteLine( "testStr1 = '{0}' before 'SetStringTest'", testStr1 );
            SetStringTest( testStr1 );   // this will not affect the value of 'testStr1'
            Console.WriteLine( "testStr1 = '{0}' after 'SetStringTest'", testStr1 );

            Console.WriteLine( "testClass.MyString = '{0}' before 'SetStringTest'", testClass.MyString );
            SetStringTest( testClass ); // this will affect the value of testClass.MyString
            Console.WriteLine( "testClass.MyString = '{0}' after 'SetStringTest'", testClass.MyString );


            Console.WriteLine( "testStrArray[0] = '{0}' before 'SetStringTest'", testStrArray[0] );
            SetStringTest( testStrArray ); // this will affect the value of testStrArray[0]
            Console.WriteLine( "testStrArray[0] = '{0}' after 'SetStringTest'", testStrArray[0] );

            Console.WriteLine( "\nHit enter to terminate" );
            Console.ReadLine( );



        }


        /*when declaring a function that takes premitive types as parameters what you are passing to the 
         *function via the local variable 'myString' is a pointer to the string that is referenced by
         *'testStr1' in the main class 
         *   
         * so when we set the local variable 'myString' to a new value what you are assinging to it a new is a pointer (aka reference) to a new string in memory with a value of "String Set From 'SetString' Method"
         *    
         * your not actually passing the variable 'testStr1' from main but the contents of it, and the 
         * contents are a pointer (aka reference) to an actual string stored in memory
         */
        static void SetStringTest( string myString )
        {
            // setting 'myString' to a new string value here does not affect 
            // the 'testStr1' variable in main because the 'myString' variable now 
            // points to a new instance of a string with the value seen below.
            myString = "String Set From 'SetString' Method";
        }

        /* when declaring a function that take class types as parameters what you are passing to 
         * the function via variable 'myClass' is a pointer to a an object in memory that represents
         * a Class1 object.
         * 
         * In this case it will be a refernece (aka pointer) to the object created by the new operator in the line
         * Class1 testClass = new Class1( ) { MyString = "MainMethod String Value" }; in main.
         * 
         * 
         * you are not actually passing the variable 'testCalss' but the contents of testClass which is a reference to 
         * a Class1 object 
         */
        static void SetStringTest( Class1 myClass )
        {
            // we can change the MyString property here and it will affect the 
            // 'testClass' in main because because the 'myClass' variabl here 
            // and the 'testClass' actually point to the same instance of the 
            // Class1 object in memory

            myClass.MyString = "String Set From 'SetString' Method";

            // if we were to do something like 
            // myClass = new Class1();
            // this would not affect the 'testClass' variable in main 
            // because the 'myClass' variable now points to a new Class1 object in memory
        }

        /* this carries the same primise as the Class version of Set String
         * the local variable contains a reference to an array in memory
         * 
         * 
         * through that reference we can set individual elements to value
         * 
         * 
         * 
         * 
         */
        static void SetStringTest( String[] values )
        {
            // this will affect the 'testStrArray' element 0 by pointint it to a new instance 
            // of a string with the below value.
            values[0] = "String Set From 'SetString' Method";

            // doing something like this would not affect 'testStrArray' from main
            //   values = new String[] { "Set inside 'SetStringTest' method" };
            // because values would then point to a new instance of a string[] in memory.
        }

    }
}
  #6 (permalink)  
Old August 13th, 2014, 03:10 AM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Location: Suzhou
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mmorgan30,


I think I got it. 1000 thanks for you great explanation :)

Finally I can proceed reading the book ;-)

Kind regards


Juan
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Misleading question: Chapter 3, Question 15 rupe120 BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C# 0 July 17th, 2013 09:10 AM
FindControl question and a MasterPage question about controls Rushino ASP.NET 4 General Discussion 3 September 11th, 2010 03:17 PM
Disappearing UserName & Question within Customized PasswordRecovery Question Template eddiemcham ASP.NET 3.5 Basics 13 June 13th, 2009 04:23 AM
COM Question... dbrook007 Pro VB 6 1 June 5th, 2006 03:26 PM



All times are GMT -4. The time now is 06:47 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.