Click here to Skip to main content
15,925,444 members
Home / Discussions / C#
   

C#

 
Generalweb site Pin
cmarmr30-Sep-04 14:52
cmarmr30-Sep-04 14:52 
GeneralRe: web site Pin
Christian Graus30-Sep-04 15:12
protectorChristian Graus30-Sep-04 15:12 
GeneralRe: web site Pin
Nick Parker30-Sep-04 17:18
protectorNick Parker30-Sep-04 17:18 
GeneralThis crashes out on me on dataAdapter.Fill(ds) Pin
mcupryk30-Sep-04 14:40
mcupryk30-Sep-04 14:40 
GeneralRe: This crashes out on me on dataAdapter.Fill(ds) Pin
Christian Graus30-Sep-04 15:13
protectorChristian Graus30-Sep-04 15:13 
GeneralRe: This crashes out on me on dataAdapter.Fill(ds) Pin
Heath Stewart30-Sep-04 15:27
protectorHeath Stewart30-Sep-04 15:27 
GeneralBinarySearch problem... Pin
Tesic Goran30-Sep-04 13:18
professionalTesic Goran30-Sep-04 13:18 
GeneralRe: BinarySearch problem... Pin
Heath Stewart30-Sep-04 15:16
protectorHeath Stewart30-Sep-04 15:16 
For starters, you don't need a complex StringTokenizer (from wherever you're getting it). All you need is String.Split for such a simple operation (to split on "="). Also, when defining your own classes, it's better to implement IComparable in most cases so that clients of your class don't have to worry about including an appropriate IComparer implementation. This also gives you access to the private variables which uses fewer instructions when compiled (a field need only be pushed on the stack instead of pushing 'this' on the stack and calling the get accessor, which must push the field onto the stack and return).

As for the actual problem you'd have to step through your code in a debugger (which VS.NET is, of course). I don't see anything wrong off-hand. Are you sure you posted the sample correctly (i.e., sometimes people post examples of their code that actually don't contain the problem while their original source does). Then again, it's been a long day.

Anyway, here's an example of some of the things I mentioned above. I just threw it together quick, but it should hopefully give you an idea about encapsulating functionality like comparisons, string representations, and more when defining your own type:
using System;
using System.Collections;
using System.IO;
 
public class NameValuePair : IComparable
{
  string name;
  string value;
  public NameValuePair(string pair)
  {
    if (pair == null) throw new ArgumentNullException("pair");
    string[] arr = pair.Split('=');
 
    if (arr.Length != 2) throw new ArgumentException("Invalid format.", "pair");
    name = arr[0].Trim();
    value = arr[1].Trim();
  }
 
  public string Name
  {
    get { return name; }
    set { name = value; }
  }
 
  public string Value
  {
    get { return value; }
    set { this.value = value; }
  }
 
  public override string ToString()
  {
    return name + "=" + value;
  }
 
  int IComparable.CompareTo(object obj)
  {
    NameValuePair pair = obj as NameValuePair;
    if (pair != null)
    {
      int retval = string.Compare(name, pair.name);
      if (retval == 0)
        retval = string.Compare(value, pair.value);
 
      return retval;
    }
 
    return 1;
  }
 
  static void Main()
  {
    UniqueList list = new UniqueList();
    string[] values = new string[]
    {
      "Australia=Canberra",
      "Austria=Wiena",
      "Canada=Toronto",
      "Canada=Ottawa",
      "Austria=Wiena"
    };
 
    foreach (string value in values)
    {
      try
      {
        NameValuePair pair = new NameValuePair(value);
        list.Add(pair);
      }
      catch
      {
        Console.Error.WriteLine(@"""{0}"" is not unique", value);
      }
    }
 
    Console.WriteLine("The list contains:");
    list.Print(Console.Out);
  }
}
 
class UniqueList : ArrayList
{
  public override int Add(object value)
  {
    int pos = BinarySearch(value, Comparer.Default);
    if (pos >= 0) throw new ArgumentException("Not unique", "value");
 
    return base.Add(value);
  }
 
  internal void Print(TextWriter writer)
  {
    foreach (object value in this)
      writer.WriteLine(value);
  }
}


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

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles]
GeneralRe: BinarySearch problem... Pin
Tesic Goran30-Sep-04 17:52
professionalTesic Goran30-Sep-04 17:52 
GeneralRe: BinarySearch problem... Pin
Heath Stewart1-Oct-04 14:38
protectorHeath Stewart1-Oct-04 14:38 
GeneralRe: BinarySearch problem... Pin
Tesic Goran2-Oct-04 14:36
professionalTesic Goran2-Oct-04 14:36 
GeneralDigicam wizard Pin
Alex Korchemniy30-Sep-04 11:18
Alex Korchemniy30-Sep-04 11:18 
GeneralDataGrid Custom Column Header Pin
ddelapasse30-Sep-04 10:44
ddelapasse30-Sep-04 10:44 
GeneralRe: DataGrid Custom Column Header Pin
Heath Stewart30-Sep-04 14:30
protectorHeath Stewart30-Sep-04 14:30 
GeneralRe: DataGrid Custom Column Header Pin
ddelapasse30-Sep-04 14:56
ddelapasse30-Sep-04 14:56 
GeneralRe: DataGrid Custom Column Header Pin
Heath Stewart1-Oct-04 14:36
protectorHeath Stewart1-Oct-04 14:36 
GeneralShortcut Manager Pin
pat27088130-Sep-04 10:11
pat27088130-Sep-04 10:11 
GeneralRe: Shortcut Manager Pin
Heath Stewart30-Sep-04 10:32
protectorHeath Stewart30-Sep-04 10:32 
GeneralRe: ProgressBar Pin
Heath Stewart30-Sep-04 10:28
protectorHeath Stewart30-Sep-04 10:28 
GeneralPerformance of DoubleBuffer in .NET Pin
Wizard_0130-Sep-04 9:41
Wizard_0130-Sep-04 9:41 
GeneralRe: Performance of DoubleBuffer in .NET Pin
Heath Stewart30-Sep-04 10:26
protectorHeath Stewart30-Sep-04 10:26 
GeneralRe: Performance of DoubleBuffer in .NET Pin
Wizard_0130-Sep-04 11:10
Wizard_0130-Sep-04 11:10 
GeneralRandom number in C# Pin
goatstudio30-Sep-04 8:01
goatstudio30-Sep-04 8:01 
GeneralRe: Random number in C# Pin
Dave Kreskowiak30-Sep-04 8:22
mveDave Kreskowiak30-Sep-04 8:22 
GeneralRe: Random number in C# Pin
Nick Parker30-Sep-04 8:37
protectorNick Parker30-Sep-04 8:37 

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.