Hi,

Am trying to use pointers on structures in c#.

A separate class file. (the class file contains only strucutres, and the class has been removed.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct strEmployee : INotifyPropertyChanged
        {
            [MarshalAs(UnmanagedType.Struct)]
            int _EmpNo;

            public int EmpNo
            {
                get { return _EmpNo; }
                set
                {
                    _EmpNo = value;
                    PropertyValueChanged("EmpNo");
                }
            }
}

In my MainWindow.XAML.cs, i try to give a pointer to strEmployee structure, i get a compile time error 'Cannot take the address of, get the size of, or declare a pointer to a managed type.'
on line strEmployee* pStructEmp in

unsafe
{
  strEmployee* pStructEmp;
}

The build option on project properties has been checked for Allow unsafe code.

any help appreciated, thanks in advance.

May I ask, why you're using pointers? I don't see any need for them. If you really want to replicate pointer functionality, use the "ref" keyword.

Otherwise, you're going to have to use the Marshaller to convert your struct/class to an IntPtr.

Again though, I recommend reviewing your requirements. Check you really do need to use pointers.

Ketsuekiame,
thanks for the reply,
though, there is a need to use pointer, in order to improve the performance of accessing data, thats how the real time scenario is. there might be millions of data assigned to specific properties of nested structures, accessing them shud be as fast as it could be.
how do i go abt with the IntPtr..?

Arunkumars, .NET treats classes and other certain types as Reference Types. These are handled in very much the same way as Pointers in C++

What you're actually trying to do is, in my opinion, very bad.

Please take a look at this link: Parameter Types

You should switch your structs to classes and take advantage of the fact that they will be treated as pointers but still appear to be value types.
By removing this layer and trying to convert everything in to pointers, I can almost guarantee you will lose performance as the instruction calls to do so are not cheap...

EDIT: Also, by looking at EmpNo I assume you're doing something with an Employee system and the database therein. You will find that the processing time of the application is massively over-shadowed by the access times of the database, if you really were to call in that much information.

thanks,
though, we have been using classes, the performance is a bit low, so thought about going to a low level of c# programming.
Am using employee here, just as a proof of concept, its not the exact stuff am working on, the database is how ever a XML file.
the above said error, does not seem to have a common answer anywhere on web.
am just checking if the performance would be more.
'Parameter Types' link is a nice 1, am just going through, please let me know if there is a way to solve the above said error. Thanks in advance.

If performance is low, I severely doubt it is caused by *passing* data, but more due to whatever algorithm you're processing it with. Use a profiler, there are several you can use.

All classes in C# are passed by reference, there is no performance gain to be had, in marshalling them to pointers. It is in fact, *more work* for the processor.

Taking a step back -- what exactly are you trying to do here? If you want absolute performance than managed code is not the answer. The ability to control struct layouts is present for interop reasons. For example see this thread.

You can instantiate structs faster than classes sure but they're also allocated with different memory. Theres a handful of other differences but everyone posting on this thread is absolutely correct. Please provide us with a little more backround on the situation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.