Read File Contents into Array  
Author Message

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



.NET Development24  

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

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

s = New stockItems
'[store values in s]

  Return l.ToArray
End Function

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

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

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

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