Click here to Skip to main content
15,891,431 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is my program idea possible? Pin
Gerry Schmitz28-Nov-20 7:51
mveGerry Schmitz28-Nov-20 7:51 
QuestionError in SQL Light Compact 4 Pin
Alex Dunlop23-Nov-20 3:47
Alex Dunlop23-Nov-20 3:47 
AnswerRe: Error in SQL Light Compact 4 Pin
Victor Nijegorodov23-Nov-20 3:56
Victor Nijegorodov23-Nov-20 3:56 
GeneralRe: Error in SQL Light Compact 4 Pin
Alex Dunlop23-Nov-20 4:04
Alex Dunlop23-Nov-20 4:04 
GeneralRe: Error in SQL Light Compact 4 Pin
Richard Deeming23-Nov-20 4:18
mveRichard Deeming23-Nov-20 4:18 
GeneralRe: Error in SQL Light Compact 4 Pin
OriginalGriff23-Nov-20 4:29
mveOriginalGriff23-Nov-20 4:29 
GeneralRe: Error in SQL Light Compact 4 Pin
Alex Dunlop23-Nov-20 7:38
Alex Dunlop23-Nov-20 7:38 
GeneralRe: Error in SQL Light Compact 4 Pin
OriginalGriff23-Nov-20 7:53
mveOriginalGriff23-Nov-20 7:53 
GeneralRe: Error in SQL Light Compact 4 Pin
Alex Dunlop23-Nov-20 8:10
Alex Dunlop23-Nov-20 8:10 
GeneralRe: Error in SQL Light Compact 4 Pin
OriginalGriff23-Nov-20 8:19
mveOriginalGriff23-Nov-20 8:19 
GeneralRe: Error in SQL Light Compact 4 Pin
Alex Dunlop23-Nov-20 8:28
Alex Dunlop23-Nov-20 8:28 
GeneralRe: Error in SQL Light Compact 4 Pin
Richard Deeming23-Nov-20 17:43
mveRichard Deeming23-Nov-20 17:43 
GeneralRe: Error in SQL Light Compact 4 Pin
Alex Dunlop24-Nov-20 6:06
Alex Dunlop24-Nov-20 6:06 
GeneralRe: Error in SQL Light Compact 4 Pin
Dave Kreskowiak24-Nov-20 8:52
mveDave Kreskowiak24-Nov-20 8:52 
AnswerRe: Error in SQL Light Compact 4 Pin
Dave Kreskowiak23-Nov-20 5:58
mveDave Kreskowiak23-Nov-20 5:58 
Questioninteresting issue with SortedSet and ElementAt Pin
BillWoodruff22-Nov-20 18:59
professionalBillWoodruff22-Nov-20 18:59 
AnswerRe: interesting issue with SortedSet and ElementAt Pin
Sandeep Mewara22-Nov-20 20:06
mveSandeep Mewara22-Nov-20 20:06 
Hey Bill!

For using KeyValuePair in SortedSet, you need to define IComparable for it. Until it's not there, SortedSet would not be able to work on the datatype (as inherently it is supposed to sort)

To see what I say, comment your ElementAt line and run the code. You will get a runtime error as "At least one object must implement IComparable."

UPDATE: Below sample works fine with ElementAt. Where is the gap?

Once we have a defined sorted data, we can access with ElementAt. I put up a quick example:
C#
class SortedSetDemo
{
    static void Main(string[] args)
    {
        var words = new string[]
            {"the", "quick", "brown", "fox", "jumps",
             "over", "the", "lazy", "dog"};

        // Create a sorted set.
        var wordSet2 = new SortedSet<KeyValuePair<int, string>>(new KVPKeyComparer<int, string>());
        int x = 0;
        foreach (string word in words)
        {
            x++;
            wordSet2.Add(new KeyValuePair<int, string>(x, word));
        }

        // List the members of the sorted set.
        Console.WriteLine("Set items in sorted order:");

        // Access an item at a specified index (position).
        for (int j = 0; j < wordSet2.Count; j++)
        {
            var item = wordSet2.ElementAt(j);
            Console.WriteLine(item);
        }
    }

    public class KVPKeyComparer<TKey, TValue> : IComparer<KeyValuePair<TKey, TValue>> where TKey : IComparable
    {
        public int Compare(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y)
        {
            if(x.Key == null)
            {
                if(y.Key == null)
                    return 0;
                return -1;
            }

            if(y.Key == null)
                return 1;

            return x.Key.CompareTo(y.Key);
        }
    }
}
Outputs:
[1, the]
[2, quick]
[3, brown]
[4, fox]
[5, jumps]
[6, over]
[7, the]
[8, lazy]
[9, dog]


modified 23-Nov-20 4:44am.

GeneralRe: interesting issue with SortedSet and ElementAt Pin
BillWoodruff22-Nov-20 22:19
professionalBillWoodruff22-Nov-20 22:19 
GeneralRe: interesting issue with SortedSet and ElementAt Pin
Sandeep Mewara22-Nov-20 22:38
mveSandeep Mewara22-Nov-20 22:38 
GeneralRe: interesting issue with SortedSet and ElementAt Pin
BillWoodruff22-Nov-20 22:46
professionalBillWoodruff22-Nov-20 22:46 
GeneralRe: interesting issue with SortedSet and ElementAt Pin
Sandeep Mewara22-Nov-20 22:51
mveSandeep Mewara22-Nov-20 22:51 
AnswerRe: interesting issue with SortedSet and ElementAt Pin
Richard Deeming22-Nov-20 22:15
mveRichard Deeming22-Nov-20 22:15 
GeneralRe: interesting issue with SortedSet and ElementAt Pin
BillWoodruff22-Nov-20 22:40
professionalBillWoodruff22-Nov-20 22:40 
GeneralRe: interesting issue with SortedSet and ElementAt Pin
Richard Deeming22-Nov-20 22:58
mveRichard Deeming22-Nov-20 22:58 
Questionneed some guidance and advice to build a questionnaire system similar to Google forms in vb.net or c# and sql server Pin
albasheer210019-Nov-20 18:09
albasheer210019-Nov-20 18:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.