Wrox Programmer Forums
|
BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
This is the forum to discuss the Wrox book Beginning Microsoft Visual C# 2008 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780470191354
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 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 October 24th, 2009, 07:41 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Question Structs

I am reading through chapter 5 and I am just not undertsanding structs at all.

A struct in this book is described as:
"data structures composed of several peices of data, possibly of different types. that allow you to define your own types of variables based on this structure"

so then in the following exercise we call upon the same variable

myRoute:

Code:
struct route
    {
        public orientation direction;
        public double distance;
    }

    class Program
    {
        static void Main(string[] args)
        {
            route myRoute;
and then later we use it with two different types:

Code:
myRoute.direction = (orientation)myDirection;
            myRoute.distance = myDistance;
So it seems that a struct just allows you to use the same variable but hold different values in it throughout your code?

Is that a crude way of putting it? I feel like I am missing the point can anyone explain it in another way to me?

Cheers,
 
Old January 6th, 2010, 06:53 AM
Registered User
 
Join Date: Jan 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A Struct type is a value type that is typically used to encapsulate small groups of related variables.

within a struct declaration,fields cannot be initialized unless they are declared as const or static.

A struct may not declare a default constructor or a destructor

struct can be instantiated without using a new operator
 
Old January 10th, 2010, 12:00 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Thanks for the reply, I have a better understanding of them now.
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
 
Old January 12th, 2010, 01:33 AM
Registered User
 
Join Date: Dec 2009
Posts: 4
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via Yahoo to jawadh_khan786
Smile The Basics

A struct is a simple user-defined type, a lightweight alternative to a class. They support access modifiers, constructors, indexers, methods, fields, nested types, operators, and properties. They may declare a default constructor (a constructor with no parameters), however you can also declare a custom constructor. Unlike classes, structures do not support compile-time initialization of instance fields, they cannot derive from anything other than System.ValueType and they cannot be the base of another class. However, they may implement an Interface. They can be instantiated without using a new operator.

Structures take up fewer resources in memory than classes, so when you have a small, frequently used class, give some thought to using a struct instead. Note though that performance can suffer when using structures in situations where reference types are expected due to boxing and unboxing.

struct members may not be declared protected.

Declaration

A struct is declared as follows:

Code:
[attributes] [modifiers] struct identifier [:interfaces]
{
  body [;]
}
The attributes is optional and is used to hold additional declarative information.

The modifier is optional. The allowed modifiers are new, static, virtual, abstract, override, and a valid combination of the four access modifiers (public, protected, internal and private).

The keyword struct must be followed by an identifier that names the struct.

The interfaces list is optional. The list contains the interfaces implemented by the struct, all separated by commas.

The body contains the member declarations.

Example 1 - A coordinate

Code:
public struct Coords
{
  public int X, Y;
 
  public Coords(int p1, int p2)
  {
    X = p1;
    Y = p2;
  }
 
  // Override the ToString method so the value appears in text
  public override string ToString()
  {
    return String.Format("({0},{1})", X, Y);
  }
}
Which can be created in the following manner:

Code:
class Program
{
  static void Main(string[] args)
  {
    Coords coord = new Coords(3,4);
    Console.WriteLine(coord.ToString());
    Console.ReadKey();
  }
}
Please do rate my answer
The Following User Says Thank You to jawadh_khan786 For This Useful Post:
Will (January 12th, 2010)
 
Old January 12th, 2010, 03:12 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Thanks for taking the time to write that out it has definitely helped me.

Cheers
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Enums VS Structs MoDulus Intro Programming 4 January 19th, 2012 10:42 AM
arrays of structs seymour_glass C# 2 April 2nd, 2007 08:14 PM
SizeOf Structs CDMBR C# 1 February 21st, 2007 06:24 PM
declaring structs scoobie C++ Programming 3 April 6th, 2006 12:44 AM
structs - read in from data. mclavo Visual C++ 0 October 25th, 2004 10:49 AM





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