CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2009
    Posts
    34

    Baffled by Structures

    Coming from a VB6 background, I'm trying to get a handle on VS 2010 C#.

    Online, I located a file called MSFL.cs that contains structure definitions in a STATIC class container.

    One of the struct definitions is the following (partial):

    [StructLayout( LayoutKind.Sequential, CharSet = CharSet.Ansi )]
    public static struct MSFLSecurityInfo
    {
    public static uint dwTotalSize; // Structure size
    public static IntPtr hSecurity; // Security handle
    [MarshalAs( UnmanagedType.ByValTStr, SizeConst = MSFL_MAX_NAME_LENGTH + 1 )]
    public static String szName; // Security name
    [MarshalAs( UnmanagedType.ByValTStr, SizeConst = MSFL_MAX_SYMBOL_LENGTH + 1 )]
    public static String szSymbol;

    . . .


    I have a main .cs called MainWindow.xaml.cs (VS created) that I want to create a reference variable of type struct (MSFLSecurityInfo).

    First, I tried doing this:

    MSFL.MSFLSecurityInfo SecInfo = new MSFL.MSFLSecurityInfo();

    Intellisense provided the dropdown lists perfectly indicating that the IDE recognized the existence of the MSFL class and the structure definitions within it.

    Then I tried to assign a value to one of the fields in struct SecInfo.

    SecInfo.szName = "Some string...";

    Intellisense did not detect any members for SecInfo and I was unable to do anything with it.


    NEXT:

    Knowing that the structure definition is in a STATIC CLASS, I tried the other approach shown below.

    MSFL.MSFLSecurityInfo SecInfo;

    SecInfo.szName = "Some string...";

    This did not work either, and intellisense did not detect struct members for SecInfo.


    NEXT I DID THIS:

    At the structure definition, I added the STATIC keyword to the structure name as well as the members themselves within the structure definition. Then I tried the above again and it still does not work.


    I'M BAFFLED!

    I would have thought that creating a structure reference of type structure that is declared in another .cs file would be a breeze.

    What am I doing wrong?

    I've attached the MSFL.cs file.

    Thanks.

    Webbiz
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2005
    Posts
    198

    Re: Baffled by Structures

    You don't create instances to access static members (you also don't need to do this in VB, but VB, being what it is, strangely allows it).

    You need to access the static fields in the following manner, just using the struct name as the qualifier:
    MSFLSecurityInfo.szName = "some string";
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  3. #3
    Join Date
    May 2009
    Posts
    34

    Re: Baffled by Structures

    That was one of the ways I tried to do it.

    Anyway, I discovered the problem.

    You cannot assign values to structure members from within...

    public partial class MainWindow : Window
    {
    }

    You can declare the variable.

    In order to assign the values, a method must be created within class MainWindow and done within the method.

    As I am new to C#, it did not occur to me. Apparently MainWindow is the form container class.

    I discovered this by adding a button on the form and putting the assignment code within the button_click to test. Yep, it worked.

    Thanks for replying.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured