Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 July 26th, 2006, 09:46 AM
Authorized User
 
Join Date: May 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default class and struct variables are confusing

// Look at comment below. Am I right?

using System;
using System.Collections.Generic;
using System.Text;

namespace Ch09Ex03
{
    class Program
    {
        class MyClass
        {
            public int val;
        }
        struct myStruct
        {
            public int val;
        }

        static void Main(string[] args)
        {
            MyClass objectA = new MyClass();
            MyClass objectB = objectA;
            objectA.val = 10; /*Both objectA.val and objectB.val actually point to the same variable. So they will output the newest value 20.*/
            objectB.val = 20;
            myStruct structA = new myStruct();
            myStruct structB = structA;
            structA.val = 30; /*structA.val and structB.val are actually two variables. So they will output two different values: 30 and 40*/
            structB.val = 40;
            Console.WriteLine("objectA.val = {0}", objectA.val);
            Console.WriteLine("objectB.val = {0}", objectB.val);
            Console.WriteLine("structA.val = {0}", structA.val);
            Console.WriteLine("structB.val = {0}", structB.val);
            Console.ReadKey();

        }
    }
}


 
Old July 27th, 2006, 08:35 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

U r right.
This is the good example of value type and reference type.

Bijgupt
 
Old August 7th, 2006, 03:42 AM
Authorized User
 
Join Date: May 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, that's really an encouragement to me!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple yet confusing jack123 SQL Server 2000 1 July 21st, 2007 01:24 PM
Class module variables in VB6 maxpotters Pro VB 6 2 September 18th, 2005 12:15 PM
Confusing problem in C++ RCB C++ Programming 3 February 26th, 2005 07:35 PM
Accessing Session variables from custom class mohyneenm ASP.NET 1.0 and 1.1 Professional 2 January 26th, 2005 07:25 PM
who destroy static variables(no instance of class) MikoMax J2EE 1 March 31st, 2004 08:01 AM





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