Solved

Using C# (with .Net Framework 4.0), how would you extract the value in a hashtable by matching on of its values?

Posted on 2016-11-15
7
67 Views
Last Modified: 2016-11-15
In my C# application using .Net Framework 4.0,  I have the following hash table:

Hashtable hashtable = new Hashtable();
hashtable["<"] = 76;
hashtable["("] = 77;
hashtable["&"] = 80;
 

My program executes the following statement:

string[] readText = File.ReadAllLines(filePath, Encoding.UTF8);

the first 2 string arrays are as follows:

[0]    "\0\0\0&"
[1]    "\0\0\0&"

If I execute the following statement

char value = readText[0][3];     then I get  ->  value3| 38 '&'

Do you know how I can take the value in readText[0][3], which is an ampersand (&), and look up this value
in the hashtable I have created to find a match on "&" and retrieve the corresponding value 80 from the hashtable and store the result in an int variable titled strRecordSize for future reference in my application?
0
Comment
Question by:zimmer9
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41888126
Using this expression will return the value in hashtable for '&', hashtable[value.ToString()]
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 41888138
If   readText[0][3] = '& then

char Value = readText[0][3];
string strValue = Value.ToString();
int hashLookup =  hashtable [strValue;

Should give you  80
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41888145
@Neil, you are missing the closing ] in your statement, int hashLookup =  hashtable [strValue; should be int hashLookup =  hashtable [strValue];
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Neil Russell
ID: 41888151
Oops, well spotted Fernando.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 41888177
You could also use a valid loop mechanism -

Using For...Each Loop -
using System;
using System.Collections;

namespace EE_Q28983198
{
	class Program
	{
		static void Main(string[] args)
		{
			var hashtable = new Hashtable() { {"<", 76}, {"(", 77}, {"&", 80} };
			var readText = new[] { "\0\0\0&", "\0\0\0&" };
			var value = -1;
			foreach (DictionaryEntry pair in hashtable)
			{
				if (pair.Key.ToString().Equals(readText[0][3].ToString()))
				{
					value = (int)pair.Value;
					break;
				}
			}

			if (value != -1)
				Console.WriteLine("{0} found in hashtable; value = {1}", readText[0][3], value);
			Console.ReadLine();
		}
	}
}

Open in new window

Using LINQ:
using System;
using System.Collections;
using System.Linq;

namespace EE_Q28983198
{
	class Program
	{
		static void Main(string[] args)
		{
			var hashtable = new Hashtable() { {"<", 76}, {"(", 77}, {"&", 80} };
			var readText = new[] { "\0\0\0&", "\0\0\0&" };
			var values = (from DictionaryEntry pair in hashtable where pair.Key.ToString().Equals(readText[0][3].ToString()) select pair.Value);
			foreach (var value in values)
				Console.WriteLine("{0} found in hashtable; value = {1}", readText[0][3], value);
			Console.ReadLine();
		}
	}
}

Open in new window

Each of which produce the following output -Capture.JPG
-saige-
0
 

Author Comment

by:zimmer9
ID: 41888180
if (readText[0][3] == '&')
{
       char value = readText[0][3];    
       string strValue = value.ToString();
       int hashLookup = hashtable[strValue];
}


Error  Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41888190
You are missing your cast:
if (readText[0][3] == '&')
{
	char value = readText[0][3];
	string strValue = value.ToString();
	int hashLookup = (int)hashtable[strValue];
}

Open in new window


-saige-
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question