From: Jackie on
On 5/17/2010 17:21, Rick wrote:
[snip]

I know you asked Arne but something like this should work..

class MyClass
{
public string StrVar;
public int IntVar;
}

....

MyClass[] someArray =
{
new MyClass { StrVar = "Mike", IntVar = 28 },
new MyClass { StrVar = "Mary", IntVar = 25 },
new MyClass { StrVar = "John", IntVar = 31 }
};
From: Jason Newell on
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Examle
{
class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>();
list.Add(new Person("Mike", 28));
list.Add(new Person("Mary", 25));
list.Add(new Person("John", 31));

foreach (Person person in list)
{
}
}
}

class Person
{
private string m_firstName;
private uint m_age;

public Person(string firstName, uint age)
{
m_firstName = firstName;
m_age = age;
}

public string FirstName { get { return m_firstName; } set {
m_firstName = value; } }
public uint Age { get { return m_age; } set { m_age = value; } }
}
}

Jason Newell
www.jasonnewell.net

Rick wrote:
> Arnie,
> Could you please provide me a sample code?
>
> "Arne Vajhøj" wrote:
>
>> On 14-05-2010 16:34, Rick wrote:
>>> How do I create an array like the following which contains string and integer?
>>>
>>> string[,] siblings = { {"Mike", 28}, {"Mary", 25}, {"John", 31} };
>> Jason already showed you how to use object[,].
>>
>> And it works.
>>
>> But I will strongly recommend that you use X[] where X is
>> a class with a string and an int property instead. It is
>> a lot more type safe.
>>
>> Arne
>> .
>>
From: Peter Duniho on
Rick wrote:
> Arnie,
> Could you please provide me a sample code?

class X
{
public string Name { get; private set; }
public int Age { get; private set; }

public X(string name, int age)
{
Name = name;
Age = age;
}
}

then…

X[] siblings = { new X("Mike", 28), new X("Mary", 25), new X("John",
31) };
From: Abby Brown on

"Jason Newell" <nospam(a)nospam.com> wrote in message
news:%23dH3LX68KHA.5476(a)TK2MSFTNGP06.phx.gbl...
> object[,] siblings = { { "Mike", 28 }, { "Mary", 25 }, {
> "John", 31 } };
>
> Jason Newell
> www.jasonnewell.net
>
> Rick wrote:
>> How do I create an array like the following which contains
>> string and integer?
>>
>> string[,] siblings = { {"Mike", 28}, {"Mary", 25}, {"John",
>> 31} };

Is it possible to do:

object[][] siblings = { { "Mike", 28 }, { "Mary", 25 }, {
"John", 31 } };

without verbosity of doing something like:

object[][] siblings = { new object[]{ "Mike", 28 }, new
object[]{ "Mary", 25 }, new object[]{ "John", 31 } };

?


From: vanderghast on
I only see four different possibilities (there may be more, though):

1- You can use anonymous type syntax:

var siblings = new[]{
new { name= "Mike", age= 28} ,
new { name= "Mary", age= 25},
new { name= "John", age= 31}
};
where some typing is required (even if auto completion can kick in).


2- You use explicit type syntax, which means you can call a constructor and
specify public properties (or fields) values. I use the predefined Rectangle
type, for illustration:

Rectangle x = new Rectangle(1, 2, 3, 4) { Width = 10 };

( I don't claim that makes sense, but just use it to show it is possible).
Sounds unrelated, at first, but that is because that second case has some
short cut, mainly, if you use the default constructor:

Rectangle x = new Rectangle() { X = 1, Y = 2, Height = 3, Width = 4 };

You can then drop the ( ) :

Rectangle x = new Rectangle { X = 1, Y = 2, Height = 3, Width = 4 };


which can also be:

var x = new Rectangle { X = 1, Y = 2, Height = 3, Width = 4 };

and if we drop Rectangle, we got the anonymous type, as in the first case,
which explain a little bit about what happen in the first case, as it is
associated to call first a constructor (the default one, by ... default )
and then to assign public fields, or properties.


Note also that

var siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John", 31 } };


does NOT work, but can have something else which is close:


3- Use tuples

var others = new[] {
Tuple.Create( "Mike", 28 ),
Tuple.Create( "Mary", 25 ),
Tuple.Create( "John", 31 ) };

if( others[1].Item1== "Mike") { }

which can be seen like an anonymous type where the 'names' are supplied for
you as being item1, item2,.... You can further on use LINQ to convert that
array ( using OfType, or ToList, or anything you wish for).


4- Use default array construction.

While

var siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John", 31 } };

does not work,

object[,] siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John", 31 } };

does, but that seems to be related to a short cut syntax for an array
constructor on value types, and unfortunately, a jagged array,

object[][] sibligns;

does not offer short cuts that the array does, and you need the solution you
found, or use one of the other short cuts presented.


Vanderghast, Access MVP



"Abby Brown" <abbybrown(a)charter.net> wrote in message
news:OpFU7Ze9KHA.980(a)TK2MSFTNGP04.phx.gbl...
>
> "Jason Newell" <nospam(a)nospam.com> wrote in message
> news:%23dH3LX68KHA.5476(a)TK2MSFTNGP06.phx.gbl...
>> object[,] siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John", 31 } };
>>
>> Jason Newell
>> www.jasonnewell.net
>>
>> Rick wrote:
>>> How do I create an array like the following which contains string and
>>> integer?
>>>
>>> string[,] siblings = { {"Mike", 28}, {"Mary", 25}, {"John", 31} };
>
> Is it possible to do:
>
> object[][] siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John",
> 31 } };
>
> without verbosity of doing something like:
>
> object[][] siblings = { new object[]{ "Mike", 28 }, new object[]{
> "Mary", 25 }, new object[]{ "John", 31 } };
>
> ?
>
>

First  |  Prev  | 
Pages: 1 2
Prev: When to use ThreadPool
Next: using the ThreadPool