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
48 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 62

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 62

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 37

Expert Comment

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

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 32

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now