[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...
Re: [2005] creating a custom array type with .count property
you can use
vb.net Code:
for i as integer = 0 to myDomain.Servers.Length-1
dim s as Server = myDomain.Servers(i)
'your code on the server here.
next i
or in the structure
vb.net Code:
Structure Domain
public name as string
public ServerList as List(of Servers) = nothing
End Structure
for each s as Server in MyDomain.Servers
'ýour code on the server here
next s
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:
Dim myDomain As New Domain
you can access its length property like so:
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?
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.
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?
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)
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....
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.
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.
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.
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?
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.
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?
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...