C# - 500 pts. - cannot cast Hashtable item to object class ?

Posted on 2007-04-07
Last Modified: 2012-06-27
please review my previous post first...

question:     Every time i try to do the following,

Placement placement = (Placement)tableDataIndx[name];
if (placement == null)
         throw new CannotReadName(name);

for some reason placement always equals 'null'  every time i step through the code, and the exception is thrown every time.     the Hashtable  tableDataIndx   seems to properly contain all the string 'names' for key  and the Placement 'placement' objects for value  as such :

key: DOUG'NUL',  value: namespace.DataFileReader+Placement
key: JEFF'NUL',  value: namespace.DataFileReader+Placement
key: TODD'NUL',  value: namespace.DataFileReader+Placement

The above 'NUL' after the names just means there was a null end character after the string name when the console writes the line out to a file.   I realize that Java's and C#'s object-based collections (Maps) return null when an element is not found.    But in this case, this seems to work fine:    tableDataIndx[name];     but then the cast to Placement:     (Placement)tableDataIndx[name];  sets the   Placement placement ==  null

In the Hashtable tableDataIndx
string name are the keys.   object class Placement placement are the values which just represent number locations of the data when read from a buffer

??    Please help.


Question by:lblinc
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
  • 7
  • 5
LVL 16

Expert Comment

ID: 18869731
Try to llok at tableDataIndx in the object explorer. Compare your "name" with the stored one. Look which type the objects have.

Author Comment

ID: 18870114
AlexNek,     The "name" key in the Hashtable  tableDataIndx all look like this:


so..  they all have a null char or chars at the end after the "Name"  even though I attempted to use  string name = new string(chars).Trim();
as follows:  

                const int TEXT_LEN_MAX = 4;
                char[] chars = new char[TEXT_LEN_MAX];
                Encoding.Default.GetDecoder().GetChars(buf, 0, TEXT_FIELD_LEN, chars, 0);
                string name = new string(chars).Trim();

Now, I'm trying to pass a param for "Name"  which looks like these below,  but without the null characters at the end:


So..    you think that's the problem ??   If so,  how do i fix it ??    Thanks!

Author Comment

ID: 18870124
Basically, I'm trying to do this:

Placement placement = (Placement)tableDataIndx[name];

where the name key is just a local param that is passed to the Hashtable tableDataIndx, but it looks like this fornmat:   "DOUG"   "BILL"  etc..    without any null characters.     and placement is not able to be set because it must not be recognizing the key name in that format ??   why does it seem so easy in java, but a pain in the arse in C# ?    ;(
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!

LVL 16

Expert Comment

ID: 18870153
>The "name" key in the Hashtable  tableDataIndx all look like this: "DOUG\0"..
It is a little  bit strange for me but pay attention that the key when you assing variable to the map must be the same when you get it from.
I wonder why const int TEXT_LEN_MAX = 4;
and chars = "DOUG\0", where is 5 symbols;


Author Comment

ID: 18870621
Sorry.  That's my error when i was writing the example data for this post..  it's actually..


or ..


So, why is  "DON\0"   possibly not the same as "DON"  if the end character is just a null character anyway ??    Why is that last character considered when looking up any key if it is null ?       I tried to use trim to get rid of the NUL characters..   any ideas ??  

Author Comment

ID: 18870668
Alex ~    I think i just need to figure out how to get rid of the null characters in the  "name"  strings..   all of the strings are either 1, 2 or 3 characters long... but never more than 4 chars total...     so they can be different depending on length of each name.

In Java,    name.trim()   returns a copy of the string, with leading and trailing whitespace omitted.     I'm sure that the  Trim()  function in C# is supposed to do the same thing,  so I wonder why I can still see the null characters after using..   new String(name).Trim()       when viewing the hashtable objects in C#, but not in java...     new String(name).trim();

LVL 16

Expert Comment

ID: 18870671
trim is good only for spaces, I think.
Could you try to replace this string
string name = new string(chars).Trim();
to this one
string name = chars;
LVL 16

Expert Comment

ID: 18870722
No, It is wrong. I don't know the best way but here is ones.
        char[] chars = new char[4];
        chars[0] = 'A';
        chars[1] = 'B';

            StringBuilder sTemp = new StringBuilder();

            sTemp = sTemp.Append(chars);
            int Len = 0;
            for (int i = 0; i < sTemp.Length; i++ )
                char Ch = sTemp[i];
                if (Ch > 0)
            sTemp.Length = Len;

            string key = sTemp.ToString();
LVL 16

Accepted Solution

AlexNek earned 500 total points
ID: 18870727
The problem is that char[] in your sample is the array with fixed length and in in string copied not symbols up to 0, otherwise all 4 with 0.

Author Comment

ID: 18872753
I cannot believe what a pain in the arse this is when using C#..     I'm trying things like   .TrimEnd(new char[] {'\0'});     and I still can't get rid of the nulls.    Now the line looks like this after trying more tricks ...    gettign worse..      "JEF\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\a"

This is unbelievable..  to think you have to write an entirely separate function just to remove nulls from a  char array  in order to get the  correct string out of it.

Can this really be?

Author Comment

ID: 18872765
I'm sure I am being dense, and missing something in the framework libraries, but I don't see why i have to resort to some crude method(s) to parse out some (possibly) null terminated strings from a file, and am wondering if there is a smarter way to do this.

So, how do I get these out of there and into some strings in a graceful manner??    If I pass these to String(), it does not strip off the trailing nulls.  Even if I call TrimEnd, it does not seem to be stripping the nulls off.

Author Comment

ID: 18872838
problem solved.

Oh my..  that was a nightmare!  I had to use this..       name.TrimEnd('\0', '\a');       to finally resolve the issue.    

thanks for your input.


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

627 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