473,394 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

a questions about classes

I have a class like this

Public Class Class1
Public Class Token
Public TokenString As String
Public TokenValue As Double
End Class
End Class

in my code I fill an array of these classes like this

Dim Tokens As New ArrayList

Dim token As Class1.Token= New Class1.Token
token.TokenString= "APPEAL"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "APPEALING"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "APPLAUD"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "ATTRACTIVE"
token.TokenValue = 2.0
Tokens.Add(token)

Now what I want to do is to retreive the TokenValue for the token
string "Attractive"

Can anyone tell me how to do this?

Thanks

Jan 25 '07 #1
2 827
PamelaFoxcr wrote:
<snip>
Public Class Class1
Public Class Token
Public TokenString As String
Public TokenValue As Double
End Class
End Class

in my code I fill an array of these classes like this

Dim Tokens As New ArrayList

Dim token As Class1.Token= New Class1.Token
token.TokenString= "APPEAL"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "APPEALING"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "APPLAUD"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "ATTRACTIVE"
token.TokenValue = 2.0
Tokens.Add(token)

Now what I want to do is to retreive the TokenValue for the token
string "Attractive"
<snip>

Well, it's easy: 2.0! =)))

No kidding, now... You may want to use one of the generic collections
instead of an ArrayLIst:

<aircode>
Class TokenList
Inherits System.Collections.ObjectModel.KeyedCollection( _
Of String, Class1.Token)

Sub New()
MyBase.New( _
System.StringComparer.InvariantCultureIgnoreCase _
)
End Sub

Protected Overrides Function _
GetKeyForItem(ByVal Item As Token) As String
Return Item.TokenString
End Function

End Class

Dim Tokens As New TokenList
Dim token As Class1.Token= New Class1.Token
token.TokenString= "ATTRACTIVE"
token.TokenValue = 2.0
Tokens.Add(token)
Debug.Print Tokens("Attractive").TokenValue
</aircode>

HTH.

Regards,

Branco.

Jan 25 '07 #2
Hi,

First, you have a more serious problem. You've only created one Token
object. You then add the same token to the ArrayList over and over
again. But, each time you change it just before it's added. As a
result you have a list that contains 4 entries of the same object which
now ends up having the string "ATTRACTIVE" since that was the last
change. You need to create a new Token object everytime.

Second, maybe an ArrayList isn't what you want. If you store the
objects in a dictionary then you can key them for easy lookups. A
Hashtable or Dictionary<Of Tmight be better. In that case you'd use
TokenString for the key and the actual Token for the value.

Brian

On Jan 25, 10:42 am, PamelaFoxcr...@gmail.com wrote:
I have a class like this

Public Class Class1
Public Class Token
Public TokenString As String
Public TokenValue As Double
End Class
End Class

in my code I fill an array of these classes like this

Dim Tokens As New ArrayList

Dim token As Class1.Token= New Class1.Token
token.TokenString= "APPEAL"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "APPEALING"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "APPLAUD"
token.TokenValue = 2.0
Tokens.Add(token)
token.TokenString = "ATTRACTIVE"
token.TokenValue = 2.0
Tokens.Add(token)

Now what I want to do is to retreive the TokenValue for the token
string "Attractive"

Can anyone tell me how to do this?

Thanks
Jan 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nick | last post by:
Hi - apologies if these have been asked before. I've been looking around, but can't find answers to my question. Anyway... I'm an experienced programmer with JSP, ASP, and ASP.NET and am...
3
by: alexhong2001 | last post by:
When design a class, should always make it "derivable" as a base class? Is there really a situation that the designed class not "derivable"? When should make a member "protected"? Only when...
1
by: Invalidlastname | last post by:
Hi, I have some questions regarding to use vs.net to generate xsd from database tables then generate classes, not typed datasets, from the xsd. Basically I want to have some light-weigh classes...
1
by: jason | last post by:
Hello everyone, I have some general questions about the DataTable object, and how it works. Moderately new to C#, I have plenty of texts describing the language, but not so much to reference...
3
by: Phil Lee | last post by:
Hi, I have a few questions regarding web services in .NET 2 1) Why, when I run code analysis do I get a source controlled files named {guid}/codeanalysislog.xml...
8
by: NH | last post by:
Hi, I'm looking for some opinions and advice on designing ASP.Net apps. Let me explain my approach to how I currently design and hopefully you can give me some advice. I create systems for a...
7
by: alternativa | last post by:
Hello, I have a few questions concerning classes. 1) Why some people use default constructos, i.e constructors with no parameters? To me it doesn't make any sense, is there something I should...
9
by: Setash | last post by:
I've got a tiny bit of coding background, but its not the most extensive. That said, I'm trying to wrap my head around python and have a couple questions with classes and functions. Two...
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.