Code Red > .NET

Using the Generic KeyValuePair<>

(1/3) > >>

It's Alive!:
I thought I might give an example of this. I found this after building my own struct to hold a key pair (Good practice though) .
The built-in KeyValuePair<> struct provides most of the basic methods needed such as:


--- Code: ---Equals()
GetHashCode()
GetType()
{get, set ;}Key()
ToString()
{get, set ;}Value()

--- End code ---


--- Code - C#: ---using System;using System.Collections.Generic;using Autodesk.AutoCAD.Runtime;//using AcEd = Autodesk.AutoCAD.EditorInput;using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application; [assembly: CommandClass(typeof(MyClassLibrary.CRPClass))] namespace MyClassLibrary{  public class CRPClass  {    private static short myTypecode;     public CRPClass()    {    }    [CommandMethod("gen")]    static public void test()     {      //      AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;       //build a list that uses the built-struct KeyValuePair<>;      List<KeyValuePair<short, object>> myList = new List<KeyValuePair<short, object>>();      //add some stuff to the list      myList.Add(new KeyValuePair<short, object>(1000, (object)"Object0"));      myList.Add(new KeyValuePair<short, object>(1001, (object)"Object1"));      myList.Add(new KeyValuePair<short, object>(1002, (object)"Object2"));      myList.Add(new KeyValuePair<short, object>(1003, (object)"Object3"));      myList.Add(new KeyValuePair<short, object>(1004, (object)"Object4"));       //print the list      for (int i = 0; i < myList.Count; i++)      {        ed.WriteMessage("\n" + myList[i].ToString());      }       //find using our delegate below      object Nth1 = nth1(myList, 1001);      ed.WriteMessage("\n.\nReturn: find 1001 [" + Nth1.ToString() + "]");     }    //    internal static object nth1(List<KeyValuePair<short, object>> list, short crpType)    {      myTypecode = crpType;      return (object)list.Find(finder).Value;    }    //helper function    internal static bool finder(KeyValuePair<short, object> i)    {      if (i.Key.Equals(myTypecode))      {        return true;      }      return false;    }    //  }} 
Sometimes it pays to look around before rolling your own.

edit:kdub--> code=csharp

Kerry:
That's the one I was after !
Thought about using it for property lists.

Thanks Daniel.

Glenn R:
Any reason why you didn't use this:


--- Code: ---Dictionary<short, yourObjectTypeHere> someVar = new Dictionary<short, yourObjectTypeHere>();

--- End code ---

...as the Dictionary uses a KeyValuePair anyway to store it's mojo...

Just curious.

Cheers,
Glenn.

It's Alive!:

--- Quote from: Glenn R on January 30, 2007, 07:15:19 PM ---Any reason why you didn't use this:


--- Code: ---Dictionary<short, yourObjectTypeHere> someVar = new Dictionary<short, yourObjectTypeHere>();

--- End code ---

...as the Dictionary uses a KeyValuePair anyway to store it's mojo...

Just curious.

Cheers,
Glenn.

--- End quote ---

Well .. because I’m a tenderfoot and I didn’t see it?
I think I am going to have to roll my own structs anyway, I need to store an association list that’s collected and returned to/from Xdata.

(short)DxfCode.ExtendedDataInteger16
(short)MyKey
(short) DxfCode.ExtendedData…
(object)MyValue

This what I came up with. Any better ideas?



--- Code - C#: ---public struct XdataStruct  {    //    private short m_xdata1;    private short m_crptype;    private short m_xdata2;    private object m_crpvalue;    //This is slower Box/UnBox    public XdataStruct(object[] arrayList)    {      if (arrayList == null)      {        throw new ArgumentNullException("arrayList");      }      else      {        m_xdata1 = (short)arrayList[0];        m_crptype = (short)arrayList[1];        m_xdata2 = (short)arrayList[2];        m_crpvalue = (object)arrayList[3];      }    }    public XdataStruct(ArrayList arrayList)    {      if (arrayList == null)      {        throw new ArgumentNullException("arrayList");      }      else      {        m_xdata1 = (short)arrayList[0];        m_crptype = (short)arrayList[1];        m_xdata2 = (short)arrayList[2];        m_crpvalue = (object)arrayList[3];      }    }    //This is faster    public XdataStruct(short extData1, short crpType, short extData2, object crpValue)    {      m_xdata1 = extData1;      m_crptype = crpType;      m_xdata2 = extData2;      m_crpvalue = crpValue;    }    public short XData1    {      get      {        return m_xdata1;      }      set      {        m_xdata1 = value;      }    }    public short CrpType    {      get      {        return m_crptype;      }      set      {        m_crptype = value;      }    }    public short XData2    {      get      {        return m_xdata2;      }      set      {        m_xdata2 = value;      }    }    public object CrpValue    {      get      {        return m_crpvalue;      }      set      {        m_crpvalue = value;      }    }    public static bool operator ==(XdataStruct XdataStructA, XdataStruct XdataStructB)    {      return ((((XdataStructA.m_xdata1 == XdataStructB.m_xdata1) &&                (XdataStructA.m_crptype == XdataStructB.m_crptype)) &&                (XdataStructA.m_xdata2 == XdataStructB.m_xdata2)) &&                (XdataStructA.m_crpvalue == XdataStructB.m_crpvalue));    }    public static bool operator !=(XdataStruct XdataStructA, XdataStruct XdataStructB)    {      return !(XdataStructA == XdataStructB);    }    public override bool Equals(object obj)    {      if (obj is XdataStruct)        return this == (XdataStruct)obj;      else        return false;    }    public override int GetHashCode()    {      return base.GetHashCode();    }    public string ToString(IFormatProvider provider)    {      object[] objArray1 = new object[] { this.m_xdata1, this.m_crptype, this.m_xdata2, this.m_crpvalue };      return string.Format(provider, "({0},{1},{2},{3})", objArray1);    }    public override string ToString()    {      return ToString(null);    }  }  #endregion 
edit:kdub--> code=csharp

TonyT:
Hi Dan.

It helps to explore the new classes in .NET 2.0, because
a lot of things have changed with the introduction of
generics (like the Dictionary class, for example).

In the case of XData, the ResultBuffer and TypedValue
objects are the managed storage medium.

One problem there, is that its hard to incrementally build
an array of TypedValue[], because arrays don't allow you
to add elements that easily.

To deal with that, I use something like this:


--- Code - C#: --- public class TypedValueList : List<TypedValue>{   public TypedValueList( params TypedValue[] args )   {      AddRange( args );   }    // Make it a bit easier to add items:     public void Add(int typecode, object value)   {      base.Add(new TypedValue(typecode, value));   }    // Implicit conversion to SelectionFilter   public static implicit operator SelectionFilter( TypedValueList src )   {      return src != null ? new SelectionFilter( src ) : null;   }    // Implicit conversion to ResultBuffer   public static implicit operator ResultBuffer( TypedValueList src )   {      return src != null ? new ResultBuffer( src ) : null;   }    // Implicit conversion to TypedValue[]    public static implicit operator TypedValue[]( TypedValueList src )   {      return src != null ? src.ToArray() : null;   }    // Implicit conversion from TypedValue[]    public static implicit operator TypedValueList( TypedValue[] src )   {      return src != null ? new TypedValueList( src ) : null;   }    // Implicit conversion from SelectionFilter   public static implicit operator TypedValueList( SelectionFilter src )   {      return src != null ? new TypedValueList( src.GetFilter() ) : null;   }    // Implicit conversion from ResultBuffer   public static implicit operator TypedValueList( ResultBuffer src )   {      return src != null ? new TypedValueList( src.AsArray() ) : null;   } } // Example:    TypedValueList values = new TypedValueList();    values.Add( 1001, "myappname");   values.Add( 1040, 99.99 );   values.Add( 1070, 25);        //......    // a method that takes a result buffer   public void foo(ResultBuffer buffer)   {   }    foo(values);  // can pass a TypedValueList anywhere that                     // ResultBuffer or SelectionFilter is required    // method that returns a ResultBuffer:   public ResultBuffer bar()   {        return new ResultBuffer(...);     }    // Direct assignment from ResultBuffer:   TypedValueList values = bar();      
edit:kdub--> code=csharp

Navigation

[0] Message Index

[#] Next page

Go to full version