By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

no indexof-function for collection object available.

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
The 'Item' property return 'Nothing' if there is nothing found so you can
always check for 'Nothing'.

HTH
rawCoder

"aaapaul" <lv****@gmx.net> wrote in message
news:36**************************@posting.google.c om...
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

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a
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>
Nov 21 '05 #5

P: n/a
I will use the object NameValueCollection.

Thanks
aaapaul
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.