Solved

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

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

http://www.experts-exchange.com/Microsoft/Development/.NET/Visual_CSharp/Q_22493165.html?cid=239#a18863514

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.

   

0
Comment
Question by:lblinc
  • 7
  • 5
12 Comments
 
LVL 16

Expert Comment

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

Author Comment

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

"DOUG\0"
"JOE\0\0"
"SUE\0\0"
"JEFF\0"

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:


"DOUG"
"JOE"
"SUE"
"JEFF"

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

Author Comment

by:lblinc
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# ?    ;(
0
 
LVL 16

Expert Comment

by:AlexNek
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;

0
 

Author Comment

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

"DON\0"
"JOE\0"
"SUE\0"
"JEF\0"

or ..

"AL\0\0"
"JO\0\0"


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 ??  
0
 

Author Comment

by:lblinc
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();

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:AlexNek
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;
0
 
LVL 16

Expert Comment

by:AlexNek
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)
                {
                    Len++;
                }
                else
                {
                    break;
                }
            }
            sTemp.Length = Len;

            string key = sTemp.ToString();
0
 
LVL 16

Accepted Solution

by:
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.
0
 

Author Comment

by:lblinc
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?
0
 

Author Comment

by:lblinc
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.
0
 

Author Comment

by:lblinc
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.

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

9 Experts available now in Live!

Get 1:1 Help Now