5 Replies - 804 Views - Last Post: 26 January 2010 - 04:23 PM Rate Topic: -----

#1 nmgod  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

[Solved]Array of custom type

Posted 26 January 2010 - 05:03 AM

Hey haven't done c# in a while and was just looking for some quick help

I have a custom structure and I want to create an array of that type and initialize it while I'm at it. Here is an example.

struct Vect2d
{
	int x;
	int y;
}

Vect2d[] myvect = new Vect2d[] {new Vect2d(1,2), new Vect2d(2,2)}



The error obviously comes at Vect2d(1,2), saying I am trying to use a type(i.e. Vect2d) as a variable. I understand why it's wrong I just can't think of what it should be.

This post has been edited by nmgod: 26 January 2010 - 06:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [Solved]Array of custom type

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: [Solved]Array of custom type

Posted 26 January 2010 - 05:12 AM

Don't initialize the values there in the array itself
just create the array and pass the values like following
check whether it works or not
I am not sure
struct Vect2d
{
	int x;
	int y;
}

Vect2d[] myvect = new Vect2d[] {"firstname", "secondname"};


Was This Post Helpful? 0
  • +
  • -

#3 nmgod  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: [Solved]Array of custom type

Posted 26 January 2010 - 05:28 AM

I would do that if it was an array of base class, but this is a custom structure, you can use it like that, it is an array of that type, i need to dynamically create that type with my desired input.

That would just cause a implicit conversion warning.

This post has been edited by nmgod: 26 January 2010 - 05:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: [Solved]Array of custom type

Posted 26 January 2010 - 05:50 AM

you need to change your structure a little...

struct Vect2d
{
	public int x;
	public int y;

	public Vect2d(int _x, int _y)
	{
		x = _x;
		y = _y;
	}
}



then you can do this..

static void Main(string[] args)
{
	Vect2d[] myvect = new Vect2d[] { new Vect2d(1, 2), new Vect2d(2, 2) };
}


Was This Post Helpful? 1
  • +
  • -

#5 nmgod  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: [Solved]Array of custom type

Posted 26 January 2010 - 06:53 AM

View Posteclipsed4utoo, on 26 Jan, 2010 - 09:50 PM, said:

you need to change your structure a little...

struct Vect2d
{
	public int x;
	public int y;

	public Vect2d(int _x, int _y)
	{
		x = _x;
		y = _y;
	}
}



then you can do this..

static void Main(string[] args)
{
	Vect2d[] myvect = new Vect2d[] { new Vect2d(1, 2), new Vect2d(2, 2) };
}



Thanks I had completely forgotten you can add constructors in a structure.
Was This Post Helpful? 0
  • +
  • -

#6 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 525
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: [Solved]Array of custom type

Posted 26 January 2010 - 04:23 PM

Quote

Thanks I had completely forgotten you can add constructors in a structure.


Yep. You just can't have an explicit parameter-less constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1