|
If I have this:
IDictionary<int, string> dict = new SortedDictionary<int, string>();
then add a bunch of stuff to it...
and want to retrieve the last value (sorted, not inserted) and do this:
string val = dict.Values.Last();
Will this have the expected behavior of giving the the value that corresponds with the last sorted key?
|
|
|
|
|
Did you try it to see ? It doesn't seem likely to me, given that it's a SORTED dictionary.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Not sure I follow. I know its sorted, thats kind of the point of a sorted dictionary. But are the keys AND values sorted, or just keys? I dont know how the thing works- if they are two separate lists for example the keys may be sorted properly with references to values which may be in no particular order at all. In which case if I grab the last value without checking the key (i.e. dict.Values.Last()) I may be in trouble.
Maybe my question isnt clear... all I need to know is if I take the last value, it corresponds properly with the last sorted key.
I guess the guarantee is to just get the last key then its value.
|
|
|
|
|
saxisa wrote: Maybe my question isnt clear... all I need to know is if I take the last value, it corresponds properly with the last sorted key.
Your question is clear. I am saying that the best way to answer this, is to try it, but if it's sorted, it doesn't seem to me like it would be guarenteed to work for you. You can also view the source code of the container if you want a definitive answer, with reflektor.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
What does the documentation say?
|
|
|
|
|
I believe it has a link to this forum....
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
|
Looking around here, you have to suspect it tho.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
PIEBALDconsult wrote: I hope not.
It wouldn't matter much as there clearly are only a few people actually looking into the documentation anyway.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Well... if you want the value that corresponds with the last sorted key... why not ask for exactly that?
string val = dict[dict.Keys.Last()];
Adam Maras | Software Developer
Microsoft Certified Professional Developer
|
|
|
|
|
What is all this non-sense?
for such a question:
- you can read the documentation
- you can set up a little experiment
Doing so you would have found out in one minute there is no Last property in an ICollection, so ÿes, it is extremely safe, as it will not compile.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hmm, sure it does. Using System.Linq. I guess I should have put that using statement in my OP.
|
|
|
|
|
Yes, you should have.
Just one of the things not to like about Extension Methods -- people don't realize they're not actual members.
|
|
|
|
|
Sorry, I didn't realize that. maybe I should read up on it then.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
|
What is your context? Are you building a winforms application? an asp.net app? When you "load a reusable template" are you trying to support screen display? print output?
|
|
|
|
|
Sorry if I was too vague. For this instance I am trying to export a screenshot to a bmp file, but need to enter in some specifics about the selected object I am taking a screenshot of to the top the bmp. It will end up looking like a small report on top with an attached screenshot on the bottom.
I think there was hope of using this also in winforms, hence the ambiguity. I'm just having a bit of a mental road block visualizing using HTML vs. using GDI.
|
|
|
|
|
I want to take user input in a DataGridView. I am trying to adapt a code sequence I had from a text box input. I get the error "The name 'PRWEB1' does not exist in the current context". Below is a code sample I had before and next is what I am using now. Please help!
string AS1 = as1.Text;
string sAS1 = AS1.PadRight(AS1.Length + (as1.MaxInputLength - AS1.Length));
foreach (DataGridViewRow row in prweb1.DataGridView.Rows)
{
DataGridViewCell cell = row.Cells[0];
string PRWEB1 = cell.Value.ToString();
}
string sPRWEB1 = PRWEB1.PadRight(PRWEB1.Length + (prweb1.MaxInputLength- PRWEB1.Length));
|
|
|
|
|
You are declaring the variable PRWEB1 inside a loop. That is the only place it exists.
Change it to something like:
string PRWEB1 = string.Empty;
foreach (DataGridViewRow row in prweb1.DataGridView.Rows)
{
DataGridViewCell cell = row.Cells[0];
PRWEB1 = cell.Value.ToString();
}
string sPRWEB1 = PRWEB1.PadRight(PRWEB1.Length + (prweb1.MaxInputLength- PRWEB1.Length));
modified on Monday, July 27, 2009 5:46 PM
|
|
|
|
|
Hi -
I'm a hardware engineer doing my first little c# project and have gotten stuck.
In the code below, the compiler objects to the open and close parentheses in the last line where I do the .add . I get the same results if I just use the class instead of the instance.
I can't find much Google help on c# list adds except simple strings, and found nothing on a list of classes.
Help!
Chuck
public class CUserMem
{
public UInt32 bUsed; // supposed to be a Boolean but occupies 4 bytes
public UInt32 nCFreqHz; // Center Frequency
public UInt32 nDemIdx; // demod index: 4 or 5
public UInt32 nDemBWIdx; // normally 3 for 6 kHz
public UInt32 nVBFBWHz; // user selected filter BW (3.63 is a normal choice)
public int nVBFFcHz; // frequency offset of filter
public long aTime; // long (epoch time when memory was created)
public char version; // 0 currently
public char[] extDC; // 3 bytes of external downconverter info
public char [] rsvd; // 4 bytes reserved
public char [] desc; // memory tag
}
List <CUserMem> MemoryBank = new List<CUserMem>(10);
CUserMem TempCUserMem = new CUserMem();
MemoryBank.Add(TempCUserMem);
|
|
|
|
|
What error message are you getting?
Life goes very fast. Tomorrow, today is already yesterday.
modified on Friday, September 25, 2009 5:12 AM
|
|
|
|
|
Strange, your code compiles fine for me.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hmmm, maybe his code is exactly as he has put. I mean with the .Add line at class level. That would cause compile error, if I am not mistaken?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Ah, I think I may have it. Is the error:
Invalid token '(' in class, struct, or interface member declaration ?
If so, it's because you can't add to the list outside of a method.
Wrong:
public class MyClass
{
List<CUserMem> MemoryBank = new List<CUserMem>(10);
CUserMem TempCUserMem = new CUserMem();
MemoryBank.Add(TempCUserMem);
} Correct:
public class MyClass
{
List<CUserMem> MemoryBank = new List<CUserMem>(10);
public MyClass()
{
CUserMem TempCUserMem = new CUserMem();
AddToList(TempCUserMem);
}
void AddToList(CUserMem userMem)
{
MemoryBank.Add(userMem);
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Great Minds...
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|