Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Array of Classes with user-defined constructor with parameters

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
amir - 25 Mar 2008 19:58 GMT
Is there a way to declare and use an array of classes using a constructor
that takes parameters i.e.

class something
{
 int var1;
 double var2;
 
 something(int var, double variable)
 {
   var1 = var; var2 = variable
 }

 something()
 {
    var1 = 0; var2 = 0.00;
  }
}

class MainClass
{
 something[] manytypes = new something(2, 4.305)[2];
}

I want 2 types of the something class but the compiler gives an error. How
can I do this?
Jon Skeet [C# MVP] - 25 Mar 2008 20:04 GMT
> Is there a way to declare and use an array of classes using a constructor
> that takes parameters i.e.
[quoted text clipped - 22 lines]
> I want 2 types of the something class but the compiler gives an error. How
> can I do this?

Create the array, and then populate it:

something[] manytypes = new something[2];
for (int i=0; i < manytypes.Length; i++)
{
   manytypes[i] = new something(2, 4.305);
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Peter Duniho - 25 Mar 2008 20:12 GMT
> [...]
> class MainClass
[quoted text clipped - 5 lines]
> How
> can I do this?

With a relatively small number of array elements, it's simple enough to  
write:

    something[] manytypes = { new something(2, 4.305), new something(2,  
4.305) };

Of course, while more lengthy for small numbers of elements, it'd be more  
maintainable to write:

    something[] manytypes = new something[2];

    for (int i = 0; i < manytypes.Length; i++)
    {
        manytypes[i] = new something(2, 4.305);
    }

And for longer arrays, that's actually likely to be easier.

And of course, if the "something" class is immutable, you could even write:

    something init = new something(2, 4.305);
    something[] manytypes = new something[2];

    for (int i = 0; i < manytypes.Length; i++)
    {
        manytypes[i] = init;
    }

That would be more efficient than creating a new instance of the class for  
each array element.

Pete
amir - 25 Mar 2008 20:20 GMT
Thank you both for the insight. It helps a great deal to know there is some
one smarter.

Cheers..

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.