Array of Classes with user-defined constructor with parameters

  • Thread starter Thread starter amir
  • Start date Start date
A

amir

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?
 
amir said:
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?

Create the array, and then populate it:

something[] manytypes = new something[2];
for (int i=0; i < manytypes.Length; i++)
{
manytypes = new something(2, 4.305);
}
 
Thank you both for the insight. It helps a great deal to know there is some
one smarter.

Cheers..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top