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
69 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 76
What!!!??? 5 61
Call to SQL server times out 5 55
Intermittent OleDbConnection Error 20 53
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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