PC Review


Reply
Thread Tools Rate Thread

Access data types in collections by reference

 
 
Mitja Semolic
Guest
Posts: n/a
 
      22nd Jul 2004
// Example

public struct SomeData
{
public int X;
public int Y;

public SomeData(int x, int y)
{ ... }
}

....

Hashtable table = new Hashtable();
SomeData sd = new SomeData(1, 2)
table.Add(1, sd);


// this of course does not work, cause is passed by value
// but is there a way, or I need a class to encapsulate data types (stupid
solution)

((SomeData)table[1]).X = 2;


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Jul 2004
Mitja Semolic <(E-Mail Removed)> wrote:
> // Example
>
> public struct SomeData
> {
> public int X;
> public int Y;
>
> public SomeData(int x, int y)
> { ... }
> }
>
> ...
>
> Hashtable table = new Hashtable();
> SomeData sd = new SomeData(1, 2)
> table.Add(1, sd);
>
>
> // this of course does not work, cause is passed by value
> // but is there a way, or I need a class to encapsulate data types (stupid
> solution)
>
> ((SomeData)table[1]).X = 2;


Well, you can do:

SomeData sd = (SomeData)table[1];
sd.X=2;
table[1]=sd;

I would seriously suggest considering using a class instead though. I
find the number of times when it's a good idea to define my own value
types is very small indeed.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Nullable types, nulls, reference & value types question. Roman Wagner Microsoft C# .NET 6 27th May 2009 09:39 PM
Re: Nullable types, nulls, reference & value types question. Göran Andersson Microsoft C# .NET 0 27th May 2009 11:57 AM
grouping together user defined data types and collections of those types Andy B. Microsoft VB .NET 2 27th Feb 2009 12:08 PM
Array Objects pointers, value types reference types?? plau011@hotmail.com Microsoft VB .NET 1 10th Feb 2006 12:32 AM
A list of all types that are collections of another types Crirus Microsoft VB .NET 3 13th Nov 2003 06:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:17 PM.