no indexof-function for collection object available.

A

aaapaul

Hello !

I have some values stored in a xml-file. When starting my application,
I read this file and store the values in a collection object.

My problem is the error-handling, if the fieldname does not exist. I
dont want to use try and catch.
The indexof-function to check would be better, but it is not
available.

This is my class:

Public Class clsFestwerte
Public isError As Boolean
Public errMessage As String
Private strDateipfad As String
Private colFestwerte As Collection

Sub New(ByVal pstrPfad As String)
strDateipfad = pstrPfad
colFestwerte = New Collection
XMLDatei_einlesen()
End Sub

Private Sub XMLDatei_einlesen()
Dim strFeldname As String
Dim strFeldwert As String
If File.Exists(strDateipfad) = False Then
Me.errMessage = "Konfigurationsfile " & strDateipfad & "
nicht vorhanden !"
Me.isError = True
Exit Sub
End If
'XML Datei auslesen - Achtung auf Gross- und Kleinschreibung
achten !!
Dim ds As New DataSet
Dim dr As DataRow
Try
ds.ReadXml(strDateipfad)
For Each dr In ds.Tables(0).Rows
strFeldname = dr.Item("feldname")
strFeldwert = dr.Item("feldwert")
colFestwerte.Add(strFeldwert, strFeldname)
Next
Catch objE As Exception
Me.isError = True
Me.errMessage = objE.Message
End Try
End Sub

Public Function getFestwert(ByVal strFeldname As String)
' Here I need: if strfeldname in collection ?? indexof ??

Return colFestwerte.Item(strFeldname)
End Function
End Class

The problem is the getFestwert-Function:
getFestwert("value1") is ok because value1 is in the xml-file.

getFestwert("value2") causes an error, because value2 is not in the
xml-file.

How can I check, if value2 is in the collection ?
There is no indexof-function available.

thanks
aaapaul
 
R

rawCoder

The 'Item' property return 'Nothing' if there is nothing found so you can
always check for 'Nothing'.

HTH
rawCoder
 
A

aaapaul

Thanks, but it doesnt work:

Public Function getFestwert(ByVal strFeldname As String)
If Not (colFestwerte.Item(strFeldname) Is Nothing) Then
Return colFestwerte.Item(strFeldname)
Else
Return ""
End If
End Function

Error Message:
Das Argument 'Index' ist kein gültiger Wert.

Best regards
aaapaul
 
C

Cor Ligthert

Aaapaul,

Although I nowhere can see where you call this function. Do you give
accoording to the code you showed the value instead of the fieldname as
indexer.

Cor
 
A

aaapaul

Hi Cor !

I have some values with different names stored in a xml file. I use
these values is different Programs.

Because I dont want to call the values via a number - I am using a
collection.

I dont want to use:

getFestwert(5)

I want to use:

getFestwert("companyname")

The problem is the case, that there is no element named "companyname"
in the collection.

How can I handle this ?

try .. end try would be possible, but is there no function to check
this ?

thanks
aaapaul


my xml-file

<?xml version="1.0" encoding="ISO-8859-1" ?>
<festwerte id="grund">
<wert>
<feldname>werk</feldname>
<feldwert>vit</feldwert>
</wert>
<wert>
<feldname>olapwerke</feldname>
<feldwert>vb</feldwert>
</wert>
<wert>
<feldname>auftragsarten</feldname>
<feldwert>tdes</feldwert>
</wert>
<wert>
 
C

Cor Ligthert

aaapaul,

I think (not tested) that you can after reading change the datacolumname.

mydataset.tables(0).column(0).Columname = "companyname"
Know that when you write it back than have to change it back before that.

I hope this helps,

Cor
 
C

Chris Murphy via DotNetMonster.com

I've just wrapped up development on a small project that deals with similar
issues. What I ended up doing is writing 2 classes. One was a manager class
that handled all the add/edit/delete/sort routines and implemented
ICollectionsBase interface. I essentially wrote or overloaded collection
routines to affect the collection/innerlist. It's actually quite easy and
intuitive, check out http://www.codeproject.com for some samples. Now the
second class that I wrote was an object that the manager used, taking the
name/value pairs loaded in from the XML file. By adding instances of this
object to my manager class I was able to use InnerList.Items.IndexOf
("string") to locate elements in the collection. The manager class had a
property which would contain a reference to the current element I was
editing. Add a little serialization/deserialzation support and you have a
pretty useful tool. In any case I hope this helps or points you in the
right direction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top