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
71 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 34

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 34

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

628 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