Results 1 to 15 of 15

Thread: [RESOLVED] [2005] creating a custom array type with .count property

  1. #1

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Resolved [RESOLVED] [2005] creating a custom array type with .count property

    Heya,

    I have created a structure;

    Code:
       Structure Server
            Public ServerName As String
            Public IpAddress As String
    
        End Structure
    And I want a array of servers in a domain structure:

    Code:
        Structure Domain
            Public Name As String
            Public Servers() As Server
        End Structure
    But this does not support the 'domain.servers.count' property. I really need a the .count thing.

    What is the best way of solving this? I don't want to use a 'sortedlist' because I don't want to use 'keys'. Should I make a custom 'array' type, or is it best to use another type to hold the servers? I also need to be able to 'for each' true the servers like:

    Code:
    for each MyServer as Server in MyDomain.Servers

    Thanks in advance...

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] creating a custom array type with .count property

    you can use

    vb.net Code:
    1. for i as integer = 0 to myDomain.Servers.Length-1
    2.   dim s as Server = myDomain.Servers(i)
    3.   'your code on the server here.
    4. next i

    or in the structure

    vb.net Code:
    1. Structure Domain
    2.   public name as string
    3.   public ServerList as List(of Servers) = nothing  
    4. End Structure
    5.  
    6. for each s as Server in MyDomain.Servers
    7.    'ýour code on the server here
    8. next s

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] creating a custom array type with .count property

    I dont understand your question, arrays do not have a 'Count' property in .Net 2.0 (in 3.5 they have an extension method called Count, but thats irrelevant here), but Length will, as talkro showed, return the arrays length.
    So if you create a domain structure:
    VB.NET Code:
    1. Dim myDomain As New Domain
    you can access its length property like so:
    VB.NET Code:
    1. myDomain.Servers.Length
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: [2005] creating a custom array type with .count property

    Thanks Talkro, Atheistn,

    The List seems to work.

    If it is not to much to ask, when does one pick to use an:
    list
    sortedlist
    collection

    Whats the difference?

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    List(Of T) and SortedList are two types of collections (and there are many more), and you should choose whatever fits your situation the best.
    A List(Of T) is the best all-round collection, but if you need the contents to be sorted by each elements key you should use a SortedList.
    Note that there are two types of SortedLists:
    SortedList and SortedList(Of TKey, TValue), you should choose the latter.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    In addition to this issue, something new came up.

    I'm using list(Of Server) now, and I'm for eaching true each member;

    Code:
    Structure Server 
      public name as string
      public LastSeen as Date
    End Structure
    
    Structure Domain  
       public name as string 
       public ServerList as List(of Servers) = nothing  
    End Structure 
    
    for each s as Server in MyDomain.Servers
       s.lastseen = now
    next
    S as server is modified, but the value in mydomain.servers isn't because its read only.

    Anny tips on solving this?

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    What do you mean by readonly? Could you show us the exact code you're using because what you've posted isnt 100% correct.

    You should be aware of that structures are value-types so your loop isnt going to affect the original server instances in the List(Of Server)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Post Re: [RESOLVED] [2005] creating a custom array type with .count property

    Here's the exact code;

    And I think I allready solved it, this now works:

    Code:
        Structure Server
            Dim Name As String
            Dim Ipadress As String
            Dim LastSuccesPing As Date
            Dim LastFailedPing As Date
            Dim CurrentlyOnline As Boolean
    
        End Structure
    
        Public Servers As New SortedList(Of String, Server)
    
           For Each ServerKey As String In Servers.Keys
    
                Dim MyServer As Server = Servers(ServerKey)
    
                MyServer.LastSuccesPing = Now
                MyServer.CurrentlyOnline = True
    
                Servers(ServerKey) = MyServer
    
           Next
    But this seems to me like old-skool programming, isn't this possible in an other way, without having to put back the value (servers(serverkey)=myserver)?

    I'm looking for a conceptial tips, not a exact code example, if someone has a smarter way of doing this, please let me know!

    Thanks in advance....
    Last edited by Lectere; May 28th, 2008 at 07:34 AM.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    Quote Originally Posted by Lectere
    But this seems to me like old-skool programming, isn't this possible in an other way, with having to put back the value (servers(serverkey)=myserver?

    Thanks in advance....
    Not when dealing with value-types. If you changed your structure to be a class instead, then you wouldnt have to put it back into the List, because you would be operating on the same object reference all the time.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    I was wrong, this doesn't work... I get this:

    Code:
    Collection was modified after the enumerator was instantiated.

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    Ah yes of course, you would have to use a normal For loop instead of a For Each loop.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    Thanks,

    With this:

    Code:
        Class Server
            Public Name As String
            Public Ipadress As String
            Public LastSuccesPing As Date
            Public LastFailedPing As Date
            Public CurrentlyOnline As Boolean
    
        End Class
    
        Public Servers As New SortedList(Of String, Server)
    
           For Each MyServer As Server In Servers.Values
    
                MyServer.LastSuccesPing = Now
                MyServer.CurrentlyOnline = True
    
           Next
    It now works! thanks, but to be honest I'm don't understand it for 100%, does this has something to to with the byval,byref difference?

    When working with a class, do I get a pointer to the instance? And working with a structure, I get a map of the structure, and not exactly the instance of this structure?

  13. #13
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    When you create an instance of a class, the variable will not hold the actual data contained in the class, but rather a memory address to where the data is located.
    When you're using a structure, you the variable holds the actual values contained within the structure, and whenever you retrieve a structure from a list (like you did before), you get an exact copy of the structure, and not a reference to the original structure.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  14. #14

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    So in general it's never wise to use structures?, It seems old style programming.

    Or can you name an example on why one should use a structure instead of a class?

  15. #15
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: [RESOLVED] [2005] creating a custom array type with .count property

    For staters; A structure cannot contain methods... nore effectively encapsulate data. so if you require neither then a structure would be fine...
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width