dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: "Struct" Problem...

  1. #1
    Join Date
    Nov 2009
    Posts
    6

    "Struct" Problem...

    Hello everyone,

    I registered here after several hours of googling to find an answer to my problem.

    I am fairly new to programming, as I started my studies only 2 months ago. I just started working C# in VS, and Im stuck in a project.

    So here goes, I believe its quite simple but I just cant seem to find how to do it.

    What Im trying to do, is add user input to a struct array I created in my console application.

    I will show you the code I got so far, and explain a little bit more.

    Code:
      
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace CGS
    {
        struct piece
        {
            string year;
            string piece_ID;
            string piece_title;
            string piece_artist;
            float estimated_value;
            float sold_price;
            char piece_status;
        }
    
        struct artist
        {
            string artist_ID;
            string artist_name;
            string curator_ID;
        }
    
        struct curator
        {
            string curator_ID;
            string curator_name;
            float commission_total;
        }
    
            class Program
    
        {
            piece[] myPiece = new piece[10];
            artist[] myArtist = new artist[10];
            curator[] myCurator = new curator[10];
               
            static void Main(string[] args)
            
                
    
        {
            string menuTitle = "Main.\n";
            string menuItem;
            string menuChoice;
                       
    
                menuTitle = menuTitle.PadLeft(53);
                Console.WriteLine(menuTitle);
    
                menuItem = "(1) Add Curator";
                menuItem = menuItem.PadLeft(40);
                Console.WriteLine(menuItem);
           
                menuChoice = Console.ReadLine();
                menuChoice = menuChoice.Trim();
                menuChoice = menuChoice.ToUpper();
    
                switch (menuChoice.ToUpper ())
                {
                
                      
                    
                    case "1":
    So what Im trying to do here, is get some user input to add a "Curator ID" to the array and then some more details, such as name etc.

    And this is where I am stuck, I tried a lot of different things but nothing seemed to work so far, probably just some syntax somewhere.

    So any help would be VERRRYYYY appreciated, I couldnt thank you enough.

    I am using VS 2005 v.2.0

    Thank you !!!

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: "Struct" Problem...

    Why you don't uses classes?
    Structs are out-dated and only available for compatibility purposes.

  3. #3
    Join Date
    Nov 2009
    Posts
    6

    Re: "Struct" Problem...

    Because my teacher requires me to use structs...

    So if anyone can help me out, I'd really appreciate it !

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: "Struct" Problem...

    Quote Originally Posted by MNovy View Post
    Why you don't uses classes?
    Structs are out-dated and only available for compatibility purposes.
    That's not true, structs have a place outside of interop.

  5. #5
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: "Struct" Problem...

    Quote Originally Posted by BigEd781 View Post
    That's not true, structs have a place outside of interop.
    Examples, please?

  6. #6
    Join Date
    Aug 2005
    Posts
    198

    Re: "Struct" Problem...

    Structs certainly aren't outdated - int, double, bool, DateTime are all structs.
    However, you almost never want your own types to be structs. The main issue is that you end up working with copies of the object where you expect a reference.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  7. #7
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: "Struct" Problem...

    Quote Originally Posted by Kirouac View Post
    Hello everyone,

    And this is where I am stuck, I tried a lot of different things but nothing seemed to work so far, probably just some syntax somewhere.

    So any help would be VERRRYYYY appreciated, I couldnt thank you enough.

    I am using VS 2005 v.2.0

    Thank you !!!
    What is the error you are getting and can you put in code that gives the error?

  8. #8
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: "Struct" Problem...

    The code snipet seems to be uncomplete. Otherwise, your code is in static Main() method, but fields myPiece, myArtist, myCurator are instance fields. But until you post more verbose description of the issue, I'm affraid no one can help you.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: "Struct" Problem...

    Quote Originally Posted by MNovy View Post
    Why you don't uses classes?
    Structs are out-dated and only available for compatibility purposes.
    When I want a simple data structure with value type semantics.

  10. #10
    Join Date
    Nov 2009
    Posts
    6

    Re: "Struct" Problem...

    What I am trying to do, is to get the user input to save into the struct... I know I'm not making myself very clear but I'm kinda lost here.

    Here's what I tried for case 1. ( my first menu item )

    Code:
      case "1":
                        Console.WriteLine("Enter the new Curator ID.\n.");
                        Console.ReadLine();
                        myCurator[cnt_curator].curator_ID; 
                        Console.ReadLine();
                        Console.WriteLine("Now enter curator name.\n.");
                        myCurator[cnt_curator].curator_name;
                        info = Console.ReadLine();
                        Console.Write("Thank you. Now press any key to quit.\n.");
                        Console.ReadKey();
                        cnt_curator++;
                        break;

  11. #11
    Join Date
    Nov 2009
    Posts
    6

    Re: "Struct" Problem...

    And these are the errors I get :

    Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    Error 2 An object reference is required for the nonstatic field, method, or property 'CGS.Program.myCurator'

    Error 3 The name 'cnt_curator' does not exist in the current context

    Error 4 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    Error 5 An object reference is required for the nonstatic field, method, or property 'CGS.Program.myCurator'

    Error 6 The name 'cnt_curator' does not exist in the current context

    Error 7 The name 'info' does not exist in the current context

    Error 8 The name 'cnt_curator' does not exist in the current context

  12. #12
    Join Date
    Jun 2008
    Posts
    2,477

    Re: "Struct" Problem...

    You have lines like this which do nothing at all:

    Code:
    myCurator[cnt_curator].curator_ID;
    If you want to assign a value you will need an equal sign and something to the right of it.

    Your errors stem from the fact that you are declaring instance level arrays in your program class and trying to use them from a static method (main). This will not work because the 'main' method can be called without an instance of your Program class, in which case your arrays have not been created. You can likely just declare the arrays inside of the main method, but if you need to access them from other static methods you will need to declare the arrays as 'static' as well.

  13. #13
    Join Date
    Nov 2009
    Posts
    6

    Re: "Struct" Problem...

    I managed it myself, thanks anyway.

    I just had to put public before the variable in my struct and a few other quite simple things.

    Thanks anyway... -__-

  14. #14
    Join Date
    Jun 2008
    Posts
    2,477

    Re: "Struct" Problem...

    Well, no, you definitely had to make your arrays static as well.

  15. #15
    Join Date
    Nov 2009
    Posts
    6

    Re: "Struct" Problem...

    That's what I mean by "a few other things".

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)