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 January 18th, 2006, 08:55 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default data structures

Hi to everybody.

I'm creating a structure to access to a database. My structure has more or less the next format:

public struct mystruct
{
      type1 tuple1;
      type2 tuple2;
      ...
}

I want to use this struct as an argument for a function that inserts this register in a table. Something like:

      mystruct struc25 = new mystruct();
      myfunct(struc25);

My problem is how can i send this argument to my function with default values.

Should I initialize my structure before sending:

      mystruct struc25 = new mystruct();
      struc25.tuple1=valuedefault1;
      struc25.tuple2=valuedefault2;
      ...
      myfunct(struc25);

or can i someway do this initialization inside the declaration of the struct, and then do just:

      mystruct struc25 = new mystruct();
      myfunct(struc25);


thank you very much for your help

                    silvia


 
Old January 18th, 2006, 10:58 AM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Sylvia.
There are two types of structuring data in C#, structs and classes
structs are not allowed to have a default constructor and for you even worse, in a function call all data are copied. (value semantics).
Using classes you will pass by reference
brgds

 
Old January 18th, 2006, 08:14 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Structs may be a value type, but you CAN still use constructors (default and parameter ctors).
You can use the following code within your struct:

    mystruct(){
        tuble1 = val1;
        tuble2 = val1;
    }
    mystruct( type1 val1, type2 val2){
        tuble1 = val1;
        tuble2 = val2;
    }

You can call your constructor through initialization:

    mystruct struc25 = new mystruct();
    mystruct struc25 = new mystruct( val1, val2 );


By using the “new” keyword we are not actually instantiating the struct, instead we are calling the struct constructor.
Even though structs are value types (value semantics) C# has giving them functionality similar to a class.
 
Old January 18th, 2006, 10:32 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One small error.

Structs can NOT have a default Constructor, but CAN have Ctors with parameters.
Reason being; There are some rare circumstances in which the runtime may not call the default ctor, therefore MS has banned zero parameter ctors from structs.

mystruct() is incorrect.
mystruct( val1, val2 ) is still correct.
 
Old January 19th, 2006, 04:25 AM
Authorized User
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you very much for your help boys :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Topics needed in Data Structures c.vikramnarayan C++ Programming 1 June 19th, 2009 09:56 PM
data structures problems detence C++ Programming 1 April 11th, 2007 11:27 AM
Algorithms Data Structures p0120144 C++ Programming 3 March 25th, 2006 08:34 AM
Complex Data Structures misterqj General .NET 2 March 19th, 2005 04:40 AM
data structures in C# Xanoo C# 4 July 1st, 2004 03:44 PM





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