This is two questions in one really. First, I wonder how to convert the
values in a Dictionary to an array. Here's the dictionary:
private Dictionary<Uri, Schemaschemas = new Dictionary<Uri, Schema>();
Uri is the System.Uri class, and Schema is a class I made. Now, I want
the class where this Dictionary is contained to have a property that
returns all the values in this Dictionary. Like such:
public Schema[] Schemas
How do I implement this property? I can figure out how to do it, but
that involves looping through all the elements in the dictionary, and
that's probably not the way it's meant to be done. I assume there must
be a one-liner solution, like the ArrayList.ToArr ay() method. I found
the Dictionary.Valu es property, but can't figure out how to make that
into an array.
My second question is about best practices. I wonder if it makes sense
to have a property that returns an array of Schema objects, rather than
returning another kind of collection? I want a basic and general
interface outwards, something that doesn't assume too much in terms of
what packages are used in the calling class. Then, is an object array
the best choice?
Gustaf 20 34138
Hi Gustaf,
This is two questions in one really. First, I wonder how to convert the
values in a Dictionary to an array. Here's the dictionary:
private Dictionary<Uri, Schemaschemas = new Dictionary<Uri, Schema>();
Uri is the System.Uri class, and Schema is a class I made. Now, I want the
class where this Dictionary is contained to have a property that returns
all the values in this Dictionary. Like such:
public Schema[] Schemas
How do I implement this property? I can figure out how to do it, but that
involves looping through all the elements in the dictionary, and that's
probably not the way it's meant to be done. I assume there must be a
one-liner solution, like the ArrayList.ToArr ay() method. I found the
Dictionary.Valu es property, but can't figure out how to make that into an
array.
public Schema[] Schemas
{
get
{
Schema[] array = new Schema[schemas.Count];
schemas.Values. CopyTo(array, 0);
return array;
}
}
My second question is about best practices. I wonder if it makes sense to
have a property that returns an array of Schema objects, rather than
returning another kind of collection? I want a basic and general interface
outwards, something that doesn't assume too much in terms of what packages
are used in the calling class. Then, is an object array the best choice?
That depends on what the caller will be doing with the return value, but
most likely an array would be your best choice since it only represents a
copy of the values in your dictionary. Any other return type might be
misleading.
--
Dave Sexton
Gustaf <gu*****@algone t.sewrote:
>I found the Dictionary.Valu es property, but can't figure out how to make that into an array. My second question is about best practices. I wonder if it makes sense to have a property that returns an array of Schema objects, rather than returning another kind of collection? I want a basic and general interface outwards, something that doesn't assume too much in terms of what packages are used in the calling class. Then, is an object array the best choice?
I don't think an object array is the best choice. Dictionary.Valu es
seems perfect for what you need. And I think it's more elegant for
objects to return ICollection<Tor IEnumerable<Tra ther than T[].
(e.g. returning an IEnumerable<Two uld let you completely rewrite
your code for returning the thing, perhaps even using the wonderful
"yield return").
--
Lucian
Gustaf wrote:
public Schema[] Schemas
How do I implement this property? I can figure out how to do it, but
that involves looping through all the elements in the dictionary, and
that's probably not the way it's meant to be done. I assume there must
be a one-liner solution, like the ArrayList.ToArr ay() method. I found
the Dictionary.Valu es property, but can't figure out how to make that
into an array.
You can do this as a one-liner, but it's not pretty:
return new List<Schema>(sc hemas.Values).T oArray();
If you don't want to get fired after your next code review, you
probably ought to break this into two statements:
List<SchemaValu es = new List<Schema>(sc hemas.Values);
return Values.ToArray( );
My second question is about best practices. I wonder if it makes sense
to have a property that returns an array of Schema objects, rather than
returning another kind of collection? I want a basic and general
interface outwards, something that doesn't assume too much in terms of
what packages are used in the calling class. Then, is an object array
the best choice?
Probably not. The approach that makes the fewest assumptions and
wastes the fewest cycles is to just expose a
public IEnumerable<Sch emaValues
{
get { return schemas.Values; }
}
Then, if your callers just want to enumerate, they can do so without
any waste of time or memory. If they want a List<Schema>, they can
easily generate one; ditto if they want a Schema[].
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
What you need to know.
"Jon Shemitz" <jo*@midnightbe ach.comwrote in message
news:45******** *******@midnigh tbeach.com...
You can do this as a one-liner, but it's not pretty:
return new List<Schema>(sc hemas.Values).T oArray();
If you don't want to get fired after your next code review
I've never understood that attitude at all - there is absolutely *nothing*
wrong with that single line of code...
Hi Mark,
Well, it's iterating the entire collection to copy it into a new object,
just so it can do another copy internally again to return it as an array,
simply to save 2 lines of code :)
--
Dave Sexton
"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:OC******** ******@TK2MSFTN GP04.phx.gbl...
"Jon Shemitz" <jo*@midnightbe ach.comwrote in message
news:45******** *******@midnigh tbeach.com...
>You can do this as a one-liner, but it's not pretty:
return new List<Schema>(sc hemas.Values).T oArray();
If you don't want to get fired after your next code review
I've never understood that attitude at all - there is absolutely *nothing*
wrong with that single line of code...
Mark Rae wrote:
You can do this as a one-liner, but it's not pretty:
return new List<Schema>(sc hemas.Values).T oArray();
If you don't want to get fired after your next code review
I've never understood that attitude at all - there is absolutely *nothing*
wrong with that single line of code...
It's sort of hard to parse, especially if you're a bit unclear on C#
operator precedence. And, imho, you can be a perfectly fine C#
programmer - comfortable with all the operators and idioms - and still
be a bit unclear on C# operator precedence. I mean **15** levels of
precedence? It makes a lot of sense to ignore that and use
"unnecessar y" parens.
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
What you need to know.
Dave Sexton wrote:
Well, it's iterating the entire collection to copy it into a new object,
just so it can do another copy internally again to return it as an array,
simply to save 2 lines of code :)
Yeah - I was typing under the influence of the impression that Keys
and Values were IEnumerable<T>s , not collections. (It certainly seems
like that would be a better design.)
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
What you need to know.
"Dave Sexton" <dave@jwa[remove.this]online.comha scritto nel messaggio
news:eX******** ******@TK2MSFTN GP06.phx.gbl...
Hi Mark,
Well, it's iterating the entire collection to copy it into a new object,
just so it can do another copy internally again to return it as an array,
simply to save 2 lines of code :)
Premised that I also prefere the second solution, I really don't see
difference into generated IL.
I would be more worried about the fact that a class shouldn't return a
Something<Tclas s to the extern.
"Fabio Z" <zn*******@virg ilio.itwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
Premised that I also prefere the second solution,
That's perfectly fine, but a very different argument...:-)
I really don't see difference into generated IL.
That's because there isn't any, AFAICS
I would be more worried about the fact that a class shouldn't return a
Something<Tclas s to the extern.
:-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: What-a-Tool |
last post by:
Dim MyMsg
Set MyMsg = server.createObject("Scripting.Dictionary")
MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"
for i = 1 To MyMsg.Count
Response.Write(MyMsg.Item(i))
next
|
by: jg |
last post by:
I was trying to get custom dictionary class that can store generic or
string; So I started with the example given by the visual studio 2005 c#
online help for simpledictionay object
That seem to miss a few things including #endregion directive and the ending
class }
Is there not a simple way like in dotnet vb?
I managed to get the sample to code to this:
|
by: Rico |
last post by:
Hello,
I have an application that I'm converting to Access 2003 and SQL Server 2005
Express. The application uses extensive use of DAO and the SEEK method on
indexes. I'm having an issue when the recordset opens a table. When I
write
Set rst = db.OpenRecordset("MyTable",dbOpenTable, dbReadOnly)
I get an error. I believe it's invalid operation or invalid parameter, I'm
|
by: Terry |
last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces.
One of the major uses is to be able to split out Read-only access to an
obect. Let me give you a simple (contrived) example:
In Project RoObjDefs:
RoPerson.cls file:
Public Property Get FirstName() as String
Public Property Get LastName() as String
<end of file RoPerson.cls>
RoPersons.cls file
Public Function Count() as Integer
|
by: Assimalyst |
last post by:
Hi
I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.
i.e. the dictionary essentially creates the following array:
Key Value
| |
by: =?Utf-8?B?VHJlY2l1cw==?= |
last post by:
Hello, Newsgroupians:
I've a question regarding dictionaries. I have an array elements that I
created, and I'm trying to sort those elements into various sections. Here's
the template of my data type.
DT
{
int nSize;
...
|
by: GVDC |
last post by:
Example server-side JavaScript Web script, Dictionary class
//Dictionary class, hash array unlimited length configurable speed/efficiency
//
printf("<html><body>");
printf("<b>Creating dictionary</b><br\n>");
var dictobj = new Dictionary(5); //dictionary class
|
by: Bob Altman |
last post by:
Hi all,
I'm trying to do something that should be really easy, but I can't think of an
obvious way to do it. I have a dictionary whose value is a "value type" (as
opposed to a reference type -- in this case, a Boolean):
Dim dic As New Dictionary(Of Int32, Boolean)
dic(123) = True
I want to set all of the existing entries in the dictionary to False. The
|
by: Gilles Ganault |
last post by:
Hello
I'm using the APSW wrapper to SQLite, and I'm stuck at how to pass
data from a dictionary to the database which expects an integer:
#array filled by reading a two-column text file as input
for (isbn,carton) in data.items():
#TypeError: int argument required
sql = "INSERT INTO books (isbn,carton) VALUES ('%s',%u)" %
(isbn,carton)
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |