lblinc
asked on
C# - 500 pts. - cannot cast Hashtable item to object class ?
please review my previous post first...
https://www.experts-exchange.com/questions/22493165/java-to-C-less-than-40-lines-of-code-500-points.html?cid=239&anchorAnswerId=18863514#a18863514
question: Every time i try to do the following,
Placement placement = (Placement)tableDataIndx[n ame];
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+P lacement
key: JEFF'NUL', value: namespace.DataFileReader+P lacement
key: TODD'NUL', value: namespace.DataFileReader+P lacement
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[n ame]; 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.
https://www.experts-exchange.com/questions/22493165/java-to-C-less-than-40-lines-of-code-500-points.html?cid=239&anchorAnswerId=18863514#a18863514
question: Every time i try to do the following,
Placement placement = (Placement)tableDataIndx[n
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+P
key: JEFF'NUL', value: namespace.DataFileReader+P
key: TODD'NUL', value: namespace.DataFileReader+P
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[n
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.
Try to llok at tableDataIndx in the object explorer. Compare your "name" with the stored one. Look which type the objects have.
ASKER
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.GetDecode r().GetCha rs(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!
"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.GetDecode
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!
ASKER
Basically, I'm trying to do this:
Placement placement = (Placement)tableDataIndx[n ame];
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# ? ;(
Placement placement = (Placement)tableDataIndx[n
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# ? ;(
>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;
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;
ASKER
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 ??
"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 ??
ASKER
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();
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;
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)
{
Len++;
}
else
{
break;
}
}
sTemp.Length = Len;
string key = sTemp.ToString();
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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER
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.
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.
ASKER
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.
Oh my.. that was a nightmare! I had to use this.. name.TrimEnd('\0', '\a'); to finally resolve the issue.
thanks for your input.