473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Reference to a struct inside its definition

Im starting to learn C#, and I made a simple stack class. Inside the
stack class I had the following:

class StackElement
{
object info;
StackElement below;
}

Everything works fine cause "below" is just a reference. But, what if
I want to do it with a struct?. Since struct is a value type, how can
I put a reference to itself inside its declaration? I tried something
like:

struct StackElement
{
object info;
ref StackElement below;
}
and
struct StackElement
{
object info;
StackElement *below;
}

but it didn´t work. So how can I do it?

Oct 8 '07 #1
1 1822
az**********@gmail.com wrote:
Thanks a lot for answering it.
Im coming from C, and I am trying to convert some codes to C# just 4
fun. Since there are many structs pointing to each other and to
theriselves, I was wondering how could I do it in C#. That's why I was
coding this stack.
Well, just as that's not a great way to implement a stack in C#, it
wouldn't really be a great way to implement it in C.

But the more general issue here is that you've got a data type that you
expect to be stored as a reference on a regular basis, making it more
appropriate as a reference type than a value type.

It's very important, if you're going to know C#, to understand the
difference between reference types and value types. They look very
similar in the language, but have very specific, different behaviors.

Generally speaking, classes are reference types, while structs and
primitive types are value types.
So, Should I do it using classes only? You said something about
allowing the struct to be boxed. How can I do it, so that I can use an
object type to refer the struct?
The language does it automatically. If you assign a value type to a
variable that's of type object, or pass it as a parameter of type
object, etc. the compiler will generate code that creates an object
instance that contains the value type you've used.

If you later assign that object back to a value type, the compiler will
again generate the code necessary to get the value type data out of the
object instance and copy it to the value type instance.

One thing to keep in mind here is that when the value type is boxed, you
get a whole new instance. If you box the same value instance multiple
times, you get multiple instances, each with a new copy of the value type.

Again, if you need to reference a specific instance of a piece of data,
you really should be using a reference type.
What about using this System.IntPtr type?? I don't know what it does,
but maybe it can help me
Probably not. It is sort of like a "void *", but the issue here is that
you don't have a type that is easily referenced, because it's a value
type. Value types are meant to be passed around intact, not referenced.
You can do thing that will cause them to be referenced, but IMHO it is
better to just use a reference type in the first place, if you want to
be able to reference the data.

Pete
Oct 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: uny ternally | last post by:
I was experimenting in Visual C++ and ran into the following problem. I have the struct listed below. I also have a function that passes a variable of the struct type by reference and set the...
20
by: Elliot Marks | last post by:
If a struct or its members are passed to a function, must it be declared globally? #include <stdio.h> struct mystruct{ int a; int b; }; int structfunc(struct mystruct foo);
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
1
by: Mikko Penkkimäki | last post by:
I have a struct in C#-project and C++-project. I use the struct in C# but it has to be filled in C++. I know that arrays work like this: In C++ code: struct teststruct { char str; float...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
5
by: Ninan | last post by:
given a struct typedef struct { int a; int b; } Logstruct; How can I add a new member to the struct typedef struct {
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
3
by: stefven blonqhern | last post by:
Hello, something of a C++ beginner here with a (possibly) (really) bad question: I want to pass a reference of an object from one of the object's member functions. Is this possible? so...
2
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.