Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. HashSet(Of T) - how can I get elements by position number ?

HashSet(Of T) - how can I get elements by position number ?

Scheduled Pinned Locked Moved Visual Basic
questionhelp
27 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dilkonika

    Thank you ! Please , can you post here the code ( of variant 3 ) that works on C# ? ( I mean the whole code that you have tested , with the Copyentity function too ). Thank you !

    S Offline
    S Offline
    Sascha Lefevre
    wrote on last edited by
    #21

    Sure, here you go:

    class ChildClass
    { }

    class MyClass
    {
    public ICollection<ChildClass> Children { get; set; }

    public MyClass()
    {
        Children = new HashSet<ChildClass>() { new ChildClass() };
    }
    

    }

    T CopyEntity(T entity)
    where T : class, new()
    {
    T copy = new T();
    // actual copying omitted here
    // for testing only the return type matters
    return copy;
    }

    void Test()
    {
    MyClass myClass = new MyClass();
    dynamic children = typeof(MyClass).GetProperty("Children").GetValue(myClass);
    dynamic child = Enumerable.ElementAt(children, 0);
    dynamic copy = CopyEntity(child);
    // copy is of type ChildClass
    }

    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

    D 1 Reply Last reply
    0
    • S Sascha Lefevre

      Sure, here you go:

      class ChildClass
      { }

      class MyClass
      {
      public ICollection<ChildClass> Children { get; set; }

      public MyClass()
      {
          Children = new HashSet<ChildClass>() { new ChildClass() };
      }
      

      }

      T CopyEntity(T entity)
      where T : class, new()
      {
      T copy = new T();
      // actual copying omitted here
      // for testing only the return type matters
      return copy;
      }

      void Test()
      {
      MyClass myClass = new MyClass();
      dynamic children = typeof(MyClass).GetProperty("Children").GetValue(myClass);
      dynamic child = Enumerable.ElementAt(children, 0);
      dynamic copy = CopyEntity(child);
      // copy is of type ChildClass
      }

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      D Offline
      D Offline
      dilkonika
      wrote on last edited by
      #22

      Thank you ! But your code in C# when is converted in vb.net ( with code converters ) , is :

      Class ChildClass
      End Class

      Class [MyClass]
      Public Property Children() As ICollection(Of ChildClass)
      Get
      Return m_Children
      End Get
      Set
      m_Children = Value
      End Set
      End Property
      Private m_Children As ICollection(Of ChildClass)

      Public Sub New()
      	Children = New HashSet(Of ChildClass)() From { \_
      		New ChildClass() \_
      	}
      End Sub
      

      End Class

      Private Function CopyEntity(Of T As {Class, New})(entity As T) As T
      Dim copy As New T()
      ' actual copying omitted here
      ' for testing only the return type matters
      Return copy
      End Function

      Private Sub Test()
      Dim [myClass] As New [MyClass]()
      Dim children As dynamic = GetType([MyClass]).GetProperty("Children").GetValue([myClass])
      Dim child As dynamic = Enumerable.ElementAt(children, 0)
      Dim copy As dynamic = CopyEntity(child)
      ' copy is of type ChildClass
      End Sub

      and this code is not using CTypeDynamic. So what you have tested in vb.net that doesn't work ? and on my vb.net code , the error came up on the line :

      Dim en = ctx.Entry(clone)

      and you don't have a such line in your code. ............................. Also I try to implement your version with reflection , but I can't make it work on vb.net ? can you provide some more help ? .............................. Thank you !

      S 1 Reply Last reply
      0
      • D dilkonika

        Thank you ! But your code in C# when is converted in vb.net ( with code converters ) , is :

        Class ChildClass
        End Class

        Class [MyClass]
        Public Property Children() As ICollection(Of ChildClass)
        Get
        Return m_Children
        End Get
        Set
        m_Children = Value
        End Set
        End Property
        Private m_Children As ICollection(Of ChildClass)

        Public Sub New()
        	Children = New HashSet(Of ChildClass)() From { \_
        		New ChildClass() \_
        	}
        End Sub
        

        End Class

        Private Function CopyEntity(Of T As {Class, New})(entity As T) As T
        Dim copy As New T()
        ' actual copying omitted here
        ' for testing only the return type matters
        Return copy
        End Function

        Private Sub Test()
        Dim [myClass] As New [MyClass]()
        Dim children As dynamic = GetType([MyClass]).GetProperty("Children").GetValue([myClass])
        Dim child As dynamic = Enumerable.ElementAt(children, 0)
        Dim copy As dynamic = CopyEntity(child)
        ' copy is of type ChildClass
        End Sub

        and this code is not using CTypeDynamic. So what you have tested in vb.net that doesn't work ? and on my vb.net code , the error came up on the line :

        Dim en = ctx.Entry(clone)

        and you don't have a such line in your code. ............................. Also I try to implement your version with reflection , but I can't make it work on vb.net ? can you provide some more help ? .............................. Thank you !

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #23

        dilkonika wrote:

        and this code is not using CTypeDynamic.

        Because it works without it. For completeness, I included the analog into the VB-version:

        Public Class ChildClass
        End Class
        
        Public Class SomeClass
            Public Property Children As ICollection(Of ChildClass) = New HashSet(Of ChildClass)
        End Class
        
        Public Function CopyEntity(Of T As {Class, New})(entity As T) As T
            Dim clone As New T()
            Return clone
        End Function
        
        Sub Main()
            Dim someClass = New SomeClass
            someClass.Children.Add(New ChildClass)
        
            Dim el = Enumerable.ElementAt(CallByName(someClass, "Children", CallType.Get), 0)
        
            Dim elcopy = CopyEntity(el)   REM without CTypeDynamic. doesn't work. (elcopy is Object)
        
            Dim el1 = CType(el, ChildClass)
            Dim el1copy = CopyEntity(el1) REM works (el1copy is ChildClass)
        
            Dim el2 = CTypeDynamic(Of ChildClass)(el)
            Dim el2copy = CopyEntity(el2) REM works (el2copy is ChildClass)
        
            Dim el3 = CTypeDynamic(el, GetType(ChildClass))
            Dim el3copy = CopyEntity(el3) REM doesn't work (el3copy is Object)
        End Sub
        

        Quote:

        and on my vb.net code , the error came up on the line : Dim en = ctx.Entry(clone) and you don't have a such line in your code.

        Because the reason why it not works (when it not works) is because clone is of type Object and not of type ChildClass. So I only have to check for the type of clone to know if your line ctx.Entry(clone) would work.

        dilkonika wrote:

        Also I try to implement your version with reflection , but I can't make it work on vb.net ? can you provide some more help ?

        Please post what you have so far.

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        D 1 Reply Last reply
        0
        • S Sascha Lefevre

          dilkonika wrote:

          and this code is not using CTypeDynamic.

          Because it works without it. For completeness, I included the analog into the VB-version:

          Public Class ChildClass
          End Class
          
          Public Class SomeClass
              Public Property Children As ICollection(Of ChildClass) = New HashSet(Of ChildClass)
          End Class
          
          Public Function CopyEntity(Of T As {Class, New})(entity As T) As T
              Dim clone As New T()
              Return clone
          End Function
          
          Sub Main()
              Dim someClass = New SomeClass
              someClass.Children.Add(New ChildClass)
          
              Dim el = Enumerable.ElementAt(CallByName(someClass, "Children", CallType.Get), 0)
          
              Dim elcopy = CopyEntity(el)   REM without CTypeDynamic. doesn't work. (elcopy is Object)
          
              Dim el1 = CType(el, ChildClass)
              Dim el1copy = CopyEntity(el1) REM works (el1copy is ChildClass)
          
              Dim el2 = CTypeDynamic(Of ChildClass)(el)
              Dim el2copy = CopyEntity(el2) REM works (el2copy is ChildClass)
          
              Dim el3 = CTypeDynamic(el, GetType(ChildClass))
              Dim el3copy = CopyEntity(el3) REM doesn't work (el3copy is Object)
          End Sub
          

          Quote:

          and on my vb.net code , the error came up on the line : Dim en = ctx.Entry(clone) and you don't have a such line in your code.

          Because the reason why it not works (when it not works) is because clone is of type Object and not of type ChildClass. So I only have to check for the type of clone to know if your line ctx.Entry(clone) would work.

          dilkonika wrote:

          Also I try to implement your version with reflection , but I can't make it work on vb.net ? can you provide some more help ?

          Please post what you have so far.

          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

          D Offline
          D Offline
          dilkonika
          wrote on last edited by
          #24

          Thank you ! But for the Reflection version , I just converted the code to vb.net but I don't know how to integreate with my code. If you can just post your full version for C# with reflection so I can understand more ( I don't have so much knowledge about reflection , but at this point I really need a solution for my problem , so I need to use it )

          S 1 Reply Last reply
          0
          • D dilkonika

            Thank you ! But for the Reflection version , I just converted the code to vb.net but I don't know how to integreate with my code. If you can just post your full version for C# with reflection so I can understand more ( I don't have so much knowledge about reflection , but at this point I really need a solution for my problem , so I need to use it )

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #25

            class Program
            {
            class ChildClass
            { }

            class SomeClass
            {
                public ICollection Children { get; set; }
            
                public SomeClass()
                {
                    Children = new HashSet() { new ChildClass() };
                }
            }
            
            public static T CopyEntity(T entity)
                where T : class, new()
            {
                T copy = new T();
                return copy;
            }
            
            static void Main(string\[\] args)
            {
                SomeClass someClass = new SomeClass();
            
                dynamic children = typeof(SomeClass).GetProperty("Children").GetValue(someClass);
                object child = Enumerable.ElementAt(children, 0);
            
                // if CopyEntity would be not a static method, BindingFlags.Instance
                // would have to be used, instead of BindingFlags.Static 
                MethodInfo mi = typeof(Program).GetMethod("CopyEntity", BindingFlags.Static | BindingFlags.Public);
                mi = mi.MakeGenericMethod(new Type\[\] { /\* +typeof(YourContext) \*/ child.GetType() /\* +typeof(bool) \*/ });
            
                // if CopyEntity would be not a static method, the first argument
                // to Invoke would have to be the class-instance on which to invoke
                // CopyEntity, instead of null
                object clone = mi.Invoke(null, new object\[\] { /\* +ctx \*/ child /\* +copyKeys \*/ });
                // -> clone is of type ChildClass
            }
            

            }

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            D 1 Reply Last reply
            0
            • S Sascha Lefevre

              class Program
              {
              class ChildClass
              { }

              class SomeClass
              {
                  public ICollection Children { get; set; }
              
                  public SomeClass()
                  {
                      Children = new HashSet() { new ChildClass() };
                  }
              }
              
              public static T CopyEntity(T entity)
                  where T : class, new()
              {
                  T copy = new T();
                  return copy;
              }
              
              static void Main(string\[\] args)
              {
                  SomeClass someClass = new SomeClass();
              
                  dynamic children = typeof(SomeClass).GetProperty("Children").GetValue(someClass);
                  object child = Enumerable.ElementAt(children, 0);
              
                  // if CopyEntity would be not a static method, BindingFlags.Instance
                  // would have to be used, instead of BindingFlags.Static 
                  MethodInfo mi = typeof(Program).GetMethod("CopyEntity", BindingFlags.Static | BindingFlags.Public);
                  mi = mi.MakeGenericMethod(new Type\[\] { /\* +typeof(YourContext) \*/ child.GetType() /\* +typeof(bool) \*/ });
              
                  // if CopyEntity would be not a static method, the first argument
                  // to Invoke would have to be the class-instance on which to invoke
                  // CopyEntity, instead of null
                  object clone = mi.Invoke(null, new object\[\] { /\* +ctx \*/ child /\* +copyKeys \*/ });
                  // -> clone is of type ChildClass
              }
              

              }

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              D Offline
              D Offline
              dilkonika
              wrote on last edited by
              #26

              Thank you ! Your code is working. Just for curiosity , the other variant is working too. I found this solution in vb.net , to make a change on CopyEntityFunction like this : instead of

              Dim clone as New(T)

              I use this :

              Dim clone = Activator.CreateInstance(entity.GetType())

              Now it's working. Thank you for your help.

              S 1 Reply Last reply
              0
              • D dilkonika

                Thank you ! Your code is working. Just for curiosity , the other variant is working too. I found this solution in vb.net , to make a change on CopyEntityFunction like this : instead of

                Dim clone as New(T)

                I use this :

                Dim clone = Activator.CreateInstance(entity.GetType())

                Now it's working. Thank you for your help.

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #27

                dilkonika wrote:

                Dim clone = Activator.CreateInstance(entity.GetType())

                Yes, that's a simpler solution if you don't need CopyEntity to be generic (that is, if you don't need the static type T for anything else). If you haven't already, you can remove that generic stuff from the declaration of CopyEntity then.

                dilkonika wrote:

                Thank you for your help.

                You're welcome.

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups