CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    [RESOLVED] VS2010/.Net4 Need help setting up a structure

    Hello!

    I'm having trouble with the FieldData[] part.

    Here is a code segment:

    Code:
     public struct dataObj
            {
                string Delimiter;
                string[] FieldData = new string[128];
                int NumberOfFields;
                string TxString;
                int TransmitInterval;
            }
            public dataObj MyDataObj = new dataObj
    I need to be able to use the structure like this:
    Code:
    MyDataObj.Delimiter = ",";
    MyDataObj.FieldData[0] = "555";
    MyDataObj.FieldData[1] = "543";
    ...
    ...
    MyDataObj.FieldData[n] = "xxx";
    MyDataObj.NumberOfFields = 10;
    Can someone show me how to do this?
    I can do this using a VisualBasic method, but I am trying to avoid this if possible.
    Thanks!

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VS2010/.Net4 Need help setting up a structure

    So what is the problem you are having? Hard to help if you don't tell us what kind of problem you are having.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    Re: VS2010/.Net4 Need help setting up a structure

    Sorry about that!

    I'm getting an error even before I try to compile/execute.
    'cannot have instance field initializers in structs'

    This line of code gives the error, however, when outside of the structure, it works just fine.

    Code:
    string[] FieldData = new string[128];

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VS2010/.Net4 Need help setting up a structure

    You should create a class rather than a structure. Then create an instance of the class at what ever level you need to use it.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: VS2010/.Net4 Need help setting up a structure

    I've never had the need to do this, so I don't have a wealth of knowlege to help you out, but I did run accross what seems to be an interesting page covering your topic.

    http://www.developerfusion.com/artic...-structs-in-c/

  6. #6
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    Re: VS2010/.Net4 Need help setting up a structure

    DataMiser,

    I'm just an old hack trying to make it in the .Net world!

    That was the solution!

    Thanks for your help!

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