Home All Groups Group Topic Archive Search About

no indexof-function for collection object available.

Author
4 Apr 2005 9:15 AM
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

Author
4 Apr 2005 10:55 AM
rawCoder
The 'Item' property return 'Nothing' if there is nothing found so you can
always check for 'Nothing'.

HTH
rawCoder

Show quoteHide quote
"aaapaul" <lvp***@gmx.net> wrote in message
news:36648c93.0504040115.40d694ba@posting.google.com...
> 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
Author
5 Apr 2005 11:17 AM
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
Author
5 Apr 2005 11:37 AM
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
Author
7 Apr 2005 8:32 AM
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>
Author
7 Apr 2005 7:01 PM
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
Author
7 Apr 2005 7:58 PM
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.

--
Message posted via http://www.dotnetmonster.com
Author
13 Apr 2005 7:03 AM
aaapaul
I will use the object NameValueCollection.

Thanks
aaapaul