Read File Contents into Array  
Author Message
Sukh1983





PostPosted: .NET Base Class Library, Read File Contents into Array Top

Dear All,

I am new at C# so sorry if this sounds a bit simple. How do i read the contents of s text file into an array I think i need a struct array, as i have four seperate values that i want to read in, namely product code, quantity, buying price, selling price, all I have at the moment is my struct set up i.e.;

struct stockItems

{

string productCode;

string quantity;

string buyingPrice;

string salePrice;

}

I have set up a new class which will contain all the methods which will manipulate the file, but was stuck on how to read in a previous files data into an array. Also if possible could you point me in the right direction for once the file has been read, being able to search through the array, based on the product code in my case, and amend either the quantity, buying price or sale price

Thanks

Sukh



.NET Development24  
 
 
Lepaca





PostPosted: .NET Base Class Library, Read File Contents into Array Top

This example is in VB:

Structure stockItems
  Dim productCode As String
 
Dim quantity As String
 
Dim buyingPrice As String
 
Dim salePrice As String
End
Structure

'This function uses a Generic List to store the values, then converts it to an array of stockItems
Private
Function read() As stockItems()
 
Dim s As stockItems
 
Dim l As New List(Of stockItems)

  Do
   
s = New stockItems
   
'[store values in s]
   
l.Add(s)
  Loop

  Return l.ToArray
End Function

'Main sub calls the function that reads values and searches the value in the array with ProductCode="string"
Sub
main()
  Dim si As stockItems() = read()
  Dim s As stockItems

  searchedProductCode =
"string"
  s = Array.Find(si, matchProductCode)
End Sub

'Delegate used in the search
Dim
matchProductCode As Predicate(Of stockItems) = AddressOf findProductCode
Dim searchedProductCode As String

'Function called by Array.Find
Private
Function findProductCode(ByVal obj As stockItems) As Boolean
  Return (obj.productCode = searchedProductCode)
End Functionu