2 Replies - 1108 Views - Last Post: 14 February 2012 - 03:11 PM Rate Topic: -----

#1 Mystic   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 28-August 07

Creating Array of type Struct <name> + struct positioning

Posted 12 February 2012 - 06:38 PM

Hi guys, as per my title I am needed to

Quote

store an array of type STeamMember(//this is the name of the struct) with a maximum size of 11 players


I have tried to do this and have read some posts from MSDN and from DIC but so far have failed, I know that one can put an array inside a struct but according to my instructions that isnt what i need to do.

Instructions are:

Quote

Question 1 - GUI
Recreate the user interface exactly as in the image above. Marks will be deducted if you deviate from this design. Also supply the necessary code for the following:
The “Exit” button must close the application.
(5)
Question 2 - STeamMember
Create a struct called STeamMember. This struct must store the name and position of a single team member (both as strings). No other data may be stored in this struct.
(5)
Question 3
Create a container class called CTeam. This class needs to store an array of type STeamMember with a maximum size of 11 players. The structure of your class must follow the UML class diagram below:
CTeam
+ this[int _Index]
+ this[string _Name]
+ MaxSize
---------------------------------
+ Add(STeamMember _Player)
+ Remove(string _Name)


Both of the indexers need to return an instance of STeamMember from the array within this class. You must implement all of the public methods and properties that are shown in this UML class diagram. You may make use of your own private methods or properties if necessary (although this should not be the case). Please note that team members may not have the same name. Also provide the necessary code for the following:


I have done Question 1 and added another class called CTeam.

As my understanding goes, the struct has to sit in the windows form while all my other code that does the heavy lifting has to sit in the CTeam class. The struct pushes data into CTeam when storing (adding/deleting) and pulls data from CTeam for displaying data. Is my understanding correct?

Now i semi understand that a class can become a quasi array, and judging by the instructions my struct should be too (The indexers would help me with using CTeam as an "array" by using
CTeam team = new CTeam
and then
console.writeline(team [index])
. However i completely fail to understand what they mean by my array should of the type struct and how i am supposed to retrieve certain elements at certain positions within this newfangled array type (arraylist and string array makes sense with [index] and GetValue[index]).

Here is my code:

Windows form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Soccer_Team__Struct_
{
    
    public struct STeamMember
    {

        String sName, sPosition;
        public STeamMember(String _Name, String _Position)
        {
            this.sName = _Name;
            this.sPosition = _Position;
        }

        public String Name //is this necessary? Copied an Int example from textbook, not sure if needed.
        {
            get { return sName; }
            set {sName = q[3];} //error. was testing with CTeam q = new CTeam
        }
        
    }

    public partial class frmSoccerTeam : Form
    {
        CTeam team = new CTeam();
        
        public frmSoccerTeam()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            STeamMember test = new STeamMember(txtName.ToString(), "s");
            test.Name = "sd";
            team.Add(txtName.ToString());
            lblNum.Text = team.MaxSize.ToString();
        }



    }
}






CTeam Class

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Drawing;

namespace Soccer_Team__Struct_
{
    class CTeam
    {
        STeamMember [] array = new STeamMember[11];
        
        public int MaxSize
        {
            get
            {
                return array.Length;
            }
        }

        public String this[int _Index]
        {
            get
            {
                if (_Index >= 1 && _Index <= 11)
                {
                    return array.GetValue(_Index).ToString();
                }
                else
                {
                    return "Index Error.";
                }
                
            }
        }

        Boolean bTest = false;
        
        public String this[string _Name]
        {
            
            set
            {
                for (int i = 0; i < MaxSize; i++)
                {
                    if (_Name.CompareTo(array.GetValue(i).ToString()) == 0)
                    {
                        bTest = true;
                    }
                }

                if (bTest == true || MaxSize == 11) //test for duplicate and less than 11
                {
                    //do nothing
                   


                }
                else
                {
                    array [MaxSize + 1] = _Name; //adding?
                }
            }
        }

        public void Add(String g)
        {
            
        }


    }
}



Sorry for the long post and thanks very much in advance. I feel that once i manage to figure out how to use a struct as an array, I would be able to get a working program. Currently this is a real road block I've hit.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating Array of type Struct <name> + struct positioning

#2 Mystic   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 28-August 07

Re: Creating Array of type Struct <name> + struct positioning

Posted 14 February 2012 - 02:20 PM

Bump? I know I'm going to be scolded for that :D.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Creating Array of type Struct <name> + struct positioning

Posted 14 February 2012 - 03:11 PM

I guess I don't understand the problem.

Quote

. However i completely fail to understand what they mean by my array should of the type struct and how i am supposed to retrieve certain elements at certain positions within this newfangled array type (arraylist and string array makes sense with [index] and GetValue[index]).


It's better if you don't think of it as an array. It's a class, with a special property that lets you fetch things with a special parameter (called an index).

public String this[int _Index]


is just a fancy way of saying

public string GetIndex(int index)


They do the same thing, you can just invoke them differently.

So, now what specifically is your problem? What exception, or what line of code are you stuck on?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1