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

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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try to llok at tableDataIndx in the object explorer. Compare your "name" with the stored one. Look which type the objects have.
lblincAuthor Commented:
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!
lblincAuthor Commented:
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# ?    ;(
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

>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;

lblincAuthor Commented:
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 ??  
lblincAuthor Commented:
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();

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;
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();
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lblincAuthor Commented:
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?
lblincAuthor Commented:
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.
lblincAuthor Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.