How to search array

  • Thread starter Thread starter Morten Snedker
  • Start date Start date
M

Morten Snedker

So far I've been blessed with a whole-lotta nice help and explaining
in here, so I'll give it a go once again:


I have some difficulty searching an array.

I have a structure:

Public Structure Naturgas

Public Id As Integer
Public Fabrikat As String
Public Effekt As String

End Structure

This is assigned to an ArrayList called _naturgasList:

lst.Id = CType(reader(0).ToString, Integer)
lst.Fabrikat = reader(1).ToString
lst.Effekt = reader(2).ToString
_naturgasList.Add(lst)

Now, in _naturgasList I wish to look up Effekt via Id. How do I do
this?

The runtime content of the Array is at www.dbconsult.dk/db/array.jpg

Thanks in advance for any help.


Regards /Snedker
 
Ahaaaaaaaaaa! =B-)

First I didn't know how to work the array. Back-drilling was the
trick.

Then my array was empty. And of course it is - it should be, since the
page finished loading ages ago. :-)

The trick was to load it again:

SetNaturgas()

Dim s As String
Dim a As Naturgas

a = CType(_naturgasList(3), Naturgas)
s = a.Effekt

MsgBox(s)


Thanks for your help, Snedker.
Your're welcome, Snedker. Anytime.

Also thanks to all replies in between. :-)


/Snedker
 
Public Struct Naturgas
Public int Id;
Public string Fabrikat;
Public string Effekt;
End Structure

class SomeClass {
ArrayList _naturgasList = new ArrayList();

public void Add(Reader reader) { // or whatever the class of your reader
variable is
Naturgas lst = new Naturgas();
lst.Id = int32.Parse(reader[0].ToString());
lst.Fabrikat = reader[1].ToString();
lst.Effekt = reader[2].ToString();
_naturgasList.Add(lst);
}

public Naturgas FindEffektById(int Id) {
for (int i = 0, i < _natrugasList.Count, i++) {
if (_naturgasList.Id == Id) {
return _naturgasList.Effekt;
}
}
return null;
}
}

Something like that anyway.

Sorry, I don't do VB. It's a religious thing.


Peter
 
Ooops! Forgot the typecast. Got too used to generics.

Sorry


Peter

Peter Bradley said:
Public Struct Naturgas
Public int Id;
Public string Fabrikat;
Public string Effekt;
End Structure

class SomeClass {
ArrayList _naturgasList = new ArrayList();

public void Add(Reader reader) { // or whatever the class of your
reader variable is
Naturgas lst = new Naturgas();
lst.Id = int32.Parse(reader[0].ToString());
lst.Fabrikat = reader[1].ToString();
lst.Effekt = reader[2].ToString();
_naturgasList.Add(lst);
}

public Naturgas FindEffektById(int Id) {
for (int i = 0, i < _natrugasList.Count, i++) {
if (_naturgasList.Id == Id) {
return _naturgasList.Effekt;
}
}
return null;
}
}

Something like that anyway.

Sorry, I don't do VB. It's a religious thing.


Peter


Morten Snedker said:
So far I've been blessed with a whole-lotta nice help and explaining
in here, so I'll give it a go once again:


I have some difficulty searching an array.

I have a structure:

Public Structure Naturgas

Public Id As Integer
Public Fabrikat As String
Public Effekt As String

End Structure

This is assigned to an ArrayList called _naturgasList:

lst.Id = CType(reader(0).ToString, Integer)
lst.Fabrikat = reader(1).ToString
lst.Effekt = reader(2).ToString
_naturgasList.Add(lst)

Now, in _naturgasList I wish to look up Effekt via Id. How do I do
this?

The runtime content of the Array is at www.dbconsult.dk/db/array.jpg

Thanks in advance for any help.


Regards /Snedker
 
Which brings up two important points...

If he is using 2.0, he probably _should_ be using a generic....and

if he is using 2.0, he _could_ use a delegate to search through the
generic..

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


Peter Bradley said:
Ooops! Forgot the typecast. Got too used to generics.

Sorry


Peter

Peter Bradley said:
Public Struct Naturgas
Public int Id;
Public string Fabrikat;
Public string Effekt;
End Structure

class SomeClass {
ArrayList _naturgasList = new ArrayList();

public void Add(Reader reader) { // or whatever the class of your
reader variable is
Naturgas lst = new Naturgas();
lst.Id = int32.Parse(reader[0].ToString());
lst.Fabrikat = reader[1].ToString();
lst.Effekt = reader[2].ToString();
_naturgasList.Add(lst);
}

public Naturgas FindEffektById(int Id) {
for (int i = 0, i < _natrugasList.Count, i++) {
if (_naturgasList.Id == Id) {
return _naturgasList.Effekt;
}
}
return null;
}
}

Something like that anyway.

Sorry, I don't do VB. It's a religious thing.


Peter


Morten Snedker said:
So far I've been blessed with a whole-lotta nice help and explaining
in here, so I'll give it a go once again:


I have some difficulty searching an array.

I have a structure:

Public Structure Naturgas

Public Id As Integer
Public Fabrikat As String
Public Effekt As String

End Structure

This is assigned to an ArrayList called _naturgasList:

lst.Id = CType(reader(0).ToString, Integer)
lst.Fabrikat = reader(1).ToString
lst.Effekt = reader(2).ToString
_naturgasList.Add(lst)

Now, in _naturgasList I wish to look up Effekt via Id. How do I do
this?

The runtime content of the Array is at www.dbconsult.dk/db/array.jpg

Thanks in advance for any help.


Regards /Snedker

 
Back
Top