Solved

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

Posted on 2007-04-07
12
404 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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 …

837 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