Structures in VB .NET

Hi, i define my structure and need little issues :

Structure Employee
'declaring a structure named Employee
Dim EmpName As String
Dim EmpDesignation As String
Dim EmpCity As String
Dim EmpSal As Double
Dim EmpId As Integer
'declaring five fields of different data types in the structure
End Structure

Dim san As New Employee()

'creating an instance of Employee
san.EmpName = "Sandeep"
san.EmpDesignation = "Software Developer"
san.EmpCity = "Sydney"
san.EmpSal = 60000
san.EmpId = 2707


How i can Search inside structure and delete to EmpID = 2707 ???


vitotoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vitotoAuthor Commented:
This sample Search Value inside Structure :

Public Structure SystemInfo
  Public CPU As String
  Public Memory As Long
  Public DiskDrives() As String   ' Array of changeable size.
  Public PurchaseDate As Date
End Structure


Dim AllSystems(100) As SystemInfo
' ...
Function FindByDate(ByVal SearchDate As Date) As SystemInfo
  Dim I As Integer
  For I = 1 To 100
     If AllSystems(I).PurchaseDate = SearchDate Then Return AllSystems(I)
  Next I
  ' Process error: system with desired purchase date not found.
End Function




But i need Delete Record when is Founded , Any Idea ???
sj_hicksCommented:
You could use an ArrayList class instead of an array to store your instances of a structure.  The ArrayList class allows you to remove elements with the "Remove" method.
vitotoAuthor Commented:
You can post little Sample ?

This is my data :

Public Structure SystemInfo
  Public CPU As String
  Public Memory As Long
  Public DiskDrives() As String   ' Array of changeable size.
  Public PurchaseDate As Date
End Structure

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

sj_hicksCommented:
VS help also has a sample:  ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylistclassremovetopic.htm

   dim myList as ArrayList()
   dim myStruct1, myStruct2, myStruct3 as  SystemInfo

   myList.Add(myStruct1)
   myList.Add(myStruct2)
   myList.Add(myStruct3)

When you've found the index (call it intIndex) of the element you want to remove:

   myList.RemoveAt(intIndex)
sj_hicksCommented:
Sorry, should read:    dim myList as New ArrayList()
jarnsaterCommented:
This works:

private mEmployeeList as new arraylist

private sub test()
  dim emp as Employee
  emp.EmpName = "mike"
  emp.EmpId = 123
  mEmployeeList.Add(emp)

  dim emp2 as Employee
  emp2.EmpName = "bill"
  emp2.EmpId = 987
  mEmployeeList.Add(emp2)

  DeleteEmployee(123)
end sub

private sub DeleteEmployee(byval id as integer)
  for each emp as Employee in mEmployeeList
    if emp.EmpId = id then
       mEmployeeList.Remove(emp)
       return
    endif
  next
end sub

Bob LearnedCommented:
Realize that there is a performance hit from using a structure with anything, such as the ArrayList, that takes Object as a parameter type (ArrayList.Add(object)).  For repetitive loops, this hit can be pretty bad.

Structures are value types, and objects are reference types and require boxing/unboxing to store in the ArrayList.

Therefore, I would suggest that you use classes and not structures.  There is very little to change, as the syntax for classes and structures are very similar (Public Class...End Class vs. Public Structure...End Structure).

Performance enhancements in VB.NET:
http://www.startvbdotnet.com/oop/structure.aspx

Bob
Bob LearnedCommented:
Also, realize that this code will cause an error, because you can modify the contents of mEmployeeList while you are iterating through the enumerator.

private sub DeleteEmployee(byval id as integer)
  for each emp as Employee in mEmployeeList
    if emp.EmpId = id then
       mEmployeeList.Remove(emp)
       return
    endif
  next
end sub

Bob
vitotoAuthor Commented:
What is best way for managment max 72 records ?

5 Char type
1 Object Endpoint.Entry
1 Integer

I Can use Array String, inside ArrayList ?
Bob LearnedCommented:
Use a class, that you store in an ArrayList.

Bob
vitotoAuthor Commented:
You can post little Sample ?

I only need insert this values to ArrayList

IP_Server_GR = String 15
Nick = String 10
IP_Cliente = String 15
Port_Remoto = Integer
ID Cliente ACP = Object Net.IPEndPoint
IDs_Hardware = String 40
Nombre_PC = String 20

I Need two type for Search :

1) Search using parameters : Port_Remoto and IP_Cliente

2) Search using parameter : ID Cliente ACP
For this Search when Found, get Port_Remoto and IP_Cliente and Delete Record.

Thank you.

Bob LearnedCommented:
Do you need fixed-length strings?  Are you making a Windows API call with the structure?

Bob
vitotoAuthor Commented:
None, only need managment data.

Maybe Listview working ?

I need display data too, maybe i can managment data inside ListView.

Bob LearnedCommented:
Public Class ManagementData

   Public IP_Server_GR As String = String.Empty
   Public Nick As String = String.Empty
   Public IP_Cliente As String = String.Empty
   Public Port_Remoto As Integer
   Public Cliente_ACP_ID As IPEndPoint
   Public IDs_Hardware As String = String.Empty
   Public Nombre_PC As String = String.Empty

End Class

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vitotoAuthor Commented:
You can help me in Function for ADD values and Seach values by IP_Cliente and Port_Remoto ??


jarnsaterCommented:
"TheLearnedOne": The delete routine I posted earlier _does_ in fact work. Yes, I know you're not allowed to modify collections while iterating, which is why I put the Return statement directly after the Remove line, inside the loop.
I tested the code after I wrote it, and it works like a charm. I'd appreciate comments like yours to be verified first!

I do agree with TheLearnedOne regarding boxing of objects though. I would definitely use classes instead of structures in this case.

vitotoAuthor Commented:
Thank you.
vitotoAuthor Commented:
How, Sync in ListView ?
vitotoAuthor Commented:
I Implement ListView Class and Fix problem.


Thank you.
vitotoAuthor Commented:
I Fix using Array inside ListArray, Spiting and Join string.

For get data use ListView and GetEnumerator for analisis the values.

Thank you, please Close this Post.
vitotoAuthor Commented:
Thank you TheLearnedOne, i am use class and make add method.

very cool.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.