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
64 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 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