Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Hashtable Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aperrien@gmail.com - 20 Jan 2008 12:59 GMT
Hello all,
I'm learning C# from a C/C++ background, and I came across an odd
problem.

I have a hashtable that holds a string and a struct like so:

private Hashtable MyTable = new Hashtable();

public class MyData
{
    public long int x,y,number;

    public Tile( long int xx, long int yy, long int num )
    {
        x = xx;
        y = yy;
        number = num;

    }
}

MyData DataPoint;

MyData.Add[SomeString,DataPoint];

----- More code ----

foreach(DictionaryEntry DataPoint in MyTable)
{

    MyData D = (MyData)DataPoint.Value;

    D.x = 10;
}

When I try to access the members of D, I get errors. Why?
Can't I use a hashtable to store struct or class elements like a C++
map?
Jon Skeet [C# MVP] - 20 Jan 2008 13:10 GMT
> I'm learning C# from a C/C++ background, and I came across an odd
> problem.
>
> I have a hashtable that holds a string and a struct like so:

<snip>

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Bill Butler - 20 Jan 2008 16:02 GMT
> Hello all,
> I'm learning C# from a C/C++ background, and I came across an odd
> problem.
>
> I have a hashtable that holds a string and a struct like so:

From your definition of MyData below, you have a class, not a struct.
class and struct have very different behavior in C#.
You will most likely want to keep this a class.

> private Hashtable MyTable = new Hashtable();
>
[quoted text clipped - 12 lines]
>
> MyData DataPoint;

DataPoint is an unassigned reference (null)
Try
MyData DataPoint = new MyData(0,0,0);

> MyData.Add[SomeString,DataPoint];
>
[quoted text clipped - 4 lines]
>
> MyData D = (MyData)DataPoint.Value;

This is probably where it is bombing.
- DataPoint is null
- MyData has no Value members

> D.x = 10;
> }
>
> When I try to access the members of D, I get errors. Why?
> Can't I use a hashtable to store struct or class elements like a C++
> map?

It is generally a bad Idea to have public fields in your classes.
Try making the field private and adding public properties for set/get
functionality (if needed)

Many new C# developers that come from a C++ background trip all over
structs.
They should not be treated as lightweight classes the way they generally
are in C++.
Structs are very different critters from classes in C# and they have
many subtle gotchas.

I recommend sticking with classes until you understand all of the
differences.

   Good luck
   Bill

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.