473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Initialising array of struct KeyValuePair<TKey,TValue>

I want to create an array of key value pairs. This code compiles fine:

KeyValuePair<string, object>[] kvps = {
new KeyValuePair<string,object>( "int", 5 ),
new KeyValuePair<string,object>( "string", "robot")
};

But is their no shorthand to this such as

KeyValuePair<string,object>[] kvps = {{"int",5 },{"string","a string"}};

This works:

object[,] kvps = {{"int",5},{"string","a string"}};

But I would like the array to be KeyValuePairs<and the top syntax is
very long winded.

Cheers

Russell
Jul 7 '06 #1
7 25605
Russell Hind wrote:
I want to create an array of key value pairs. This code compiles fine:

KeyValuePair<string, object>[] kvps = {
new KeyValuePair<string,object>( "int", 5 ),
new KeyValuePair<string,object>( "string", "robot")
};

But is their no shorthand to this such as

KeyValuePair<string,object>[] kvps = {{"int",5 },{"string","a string"}};

This works:

object[,] kvps = {{"int",5},{"string","a string"}};

But I would like the array to be KeyValuePairs<and the top syntax is
very long winded.

Cheers

Russell
Hi Russell,

May I ask why you aren't using a Dictionary<K,V>?

///
Dictionary<string, objectmyDict = new Dictionary<string, object>();

myDict.Add( "int", 5 );
myDict.Add( "string", "robot" );
///

Is it because you need duplicate keys?

--
Hope this helps,
Tom Spink
Jul 7 '06 #2
Tom Spink wrote:
>
Hi Russell,

May I ask why you aren't using a Dictionary<K,V>?

///
Dictionary<string, objectmyDict = new Dictionary<string, object>();

myDict.Add( "int", 5 );
myDict.Add( "string", "robot" );
///

Is it because you need duplicate keys?
I am using a dictionary inside the objectthat this kvp array is passed
to. I'm trying to find the easiest way for a user to initialise the
structure. I started with your code above but would have liked to to
come down to something like this:

Dictionary<string,objectD = new Dictionary<string,object>();
D.AddItems({{"int",5},{{"string","robot"}});

But the dictionary is wrapped up in a larger object so I wanted the user
to have a very simple way to create the items as an array to fill in the
dictionary.

I'm just trying to reduce the code the user has to write basically as
this will be done in many places for us.

Cheers

Russell
Jul 7 '06 #3
I want to create an array of key value pairs. This code compiles fine:
>
KeyValuePair<string, object>[] kvps = {
new KeyValuePair<string,object>( "int", 5 ),
new KeyValuePair<string,object>( "string", "robot")
};
But is their no shorthand to this such as
KeyValuePair<string,object>[] kvps = {{"int",5 },{"string","a string"}};
I think that's not, because you need create an object that will be
referenced from the array item
This works:
object[,] kvps = {{"int",5},{"string","a string"}};
Yes, because in that case you object created explicitly for the simple types
see object[] kvps = {"12", 5, new YouClass()};
>
But I would like the array to be KeyValuePairs<and the top syntax is
very long winded.
--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Jul 7 '06 #4
Hi Russell,

Thank you for posting.

I don't think you could use the shorthand array initializers in this case.
The element type of kvps is KeyValuePair structure which is a complex data
type and array initializers can only be used in a variable or field
initializer.

In the statement "object[,] kvps = {{"int",5},{"string","a string"}};", the
element type of kvps is object which is a simple data type, so array
initializers can be used in this statement.

If you have anything unclear, please feel to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

================================================== ==========================
=============================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.

================================================== ==========================
=============================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 7 '06 #5
Hi Russell,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If you have anything unclear, please feel free to post in the newsgroup and
we will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!
Sincerely,
Linda Liu
Microsoft Online Community Support

================================================== ==========================
=============================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.

================================================== ==========================
=============================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 11 '06 #6
Linda Liu [MSFT] wrote:
Hi Russell,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If you have anything unclear, please feel free to post in the newsgroup and
we will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!
Thanks for the follow-up. There are numerous solutions to this, some
simpler than others, that I can use so no need for anything more.

Ultimately it would be nice if the language could be altered so that
something like this is possible

KeyValuePair<string, object>[] kvps = {("int",5), ("string","robot")};

(i.e. The compiler looks for a constructor of the array element type
that can take the given parameters) but as it isn't, I can use another
solution.

Cheers

Russell
Jul 11 '06 #7
Hi Russell,

Thanks for your quick response.

I think your thought is good. However, at the present time it is
unfortunately not feasible. Thank you for your understanding and support
for Microsoft!

If you have any other questions in the future, please don't hesitate to
contact us. It is always our pleasure to be of assistance.

Have a nice day!
Sincerely,
Linda Liu
Microsoft Online Community Support

================================================== ==========================
=============================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.

================================================== ==========================
=============================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 11 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Richter | last post by:
KeyValuePair<string,stringhas a ToString method that returns the KeyValue pair seperated by a comma and enclosed in : Is this method used as a building block for serialization? The reason I...
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.