Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 09-11-09   #1 (permalink)
Overclocker
 
ZHoob2004's Avatar
 
amd ati

Join Date: Mar 2007
Location: Tucson, AZ
Posts: 741

Rep: 53 ZHoob2004 is acknowledged by some
Unique Rep: 50
Folding Team Rank: 1102
Trader Rating: 0
Default C# arraylist get/set

For my programming class I need to modify an arraylist of "Button" objects from another class. I tried using get/set accessors but it tells me that an object reference is required. Any Ideas?

Code:
// variable declarations from the game1 class
// creates an arraylist "buttonlist" to hold object type Button as defined in the Button class

ArrayList buttonlist = new ArrayList();

public ArrayList ListButton
{ 
     get { return buttonlist; } 
     set { buttonlist = value; } 
}
Code:
// excerpt of code from mainMenu class
// attemps to add the button mainMenu1 to the public list ListButton, a part of the game1 class

Button mainMenu1;
mainMenu1 = new Button(new Vector2(50, 400), Game1.Content.Load<Texture2D>(@"Button"));
Game1.ListButton.add(mainMenu1);
I need ButtonList to be accessible from other classes and I'm trying to use ListButtons and get/set accessors to do so
__________________
My case mod project: Millennium Falcon

System: Asmodeus
CPU
Phenom 9600
Motherboard
DFI LP Jr 790GX-M2RS
Memory
4GB Geil DDR2 1066
Graphics Card
ASUS EAH4870 1GB
Hard Drive
160GB, 1000GB 7200rpm
Power Supply
Silverstone 600W modular
Case
Caseless!
OS
Windows 7 RC
Monitor
Asus 21.5" 2ms

Last edited by ZHoob2004 : 09-11-09 at 04:17 PM
ZHoob2004 is offline I fold for Overclock.net   Reply With Quote
Old 09-11-09   #2 (permalink)
AMD Overclocker
 
decompiled's Avatar
 
amd nvidia

Join Date: Feb 2006
Location: Redsox Nation
Posts: 705

Rep: 78 decompiled is acknowledged by some
Unique Rep: 68
Hardware Reviews: 9
Trader Rating: 0
Default

Can you post all relevant code?

System: Slow Poke
CPU
AMD X2 4400
Motherboard
MSI K8N Diamond +
Memory
3gb TCC5
Graphics Card
eVGA 7900GS
Hard Drive
74gb Raptor + Raid 1 640's
Sound Card
Audigy SE
Power Supply
OCZ 600 ModXstream
Case
Antec 1200
CPU cooling
Stock AMD
GPU cooling
Stock eVGA
OS
Windows 7 FTW Edition
Monitor
Dell 1905FP
decompiled is offline   Reply With Quote
Old 09-11-09   #3 (permalink)
Overclocker
 
ZHoob2004's Avatar
 
amd ati

Join Date: Mar 2007
Location: Tucson, AZ
Posts: 741

Rep: 53 ZHoob2004 is acknowledged by some
Unique Rep: 50
Folding Team Rank: 1102
Trader Rating: 0
Default

k OP updated with all applicable code
__________________
My case mod project: Millennium Falcon

System: Asmodeus
CPU
Phenom 9600
Motherboard
DFI LP Jr 790GX-M2RS
Memory
4GB Geil DDR2 1066
Graphics Card
ASUS EAH4870 1GB
Hard Drive
160GB, 1000GB 7200rpm
Power Supply
Silverstone 600W modular
Case
Caseless!
OS
Windows 7 RC
Monitor
Asus 21.5" 2ms
ZHoob2004 is offline I fold for Overclock.net   Reply With Quote
Old 09-12-09   #4 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Nov 2007
Location: Surrey, BC, Canada
Posts: 272

Rep: 14 serge2k Unknown
Unique Rep: 14
Trader Rating: 1
Default

well I don't know very much C# but I put together your code in VS and the only problem I could find was that the add method should be capitalized.
__________________
System: The Black Box
CPU
E6750
Motherboard
Asus P5N-E SLI
Memory
2GB DDR-2 800 MHz
Graphics Card
EVGA 8800 GTS 640
Hard Drive
2x250GB Seagate in Raid 0, 2x1TB WD Caviar Blacks
Sound Card
integrated
Power Supply
Antec Earthwatts 500W
Case
Antec Sonata 3
CPU cooling
Xigmatek HDT S1283
GPU cooling
stock
OS
Windows 7 Ultimate x64
Monitor
22" LG
serge2k is offline   Reply With Quote
Old 09-12-09   #5 (permalink)
News Fiend
 
kyotejones's Avatar
 
intel nvidia

Join Date: Mar 2007
Posts: 222

Rep: 32 kyotejones is acknowledged by some
Unique Rep: 29
Trader Rating: 0
Default

public varible?

Code:
public ArrayList buttonlist = new ArrayList();
__________________
System: SCUD
CPU
Pentium C2D 2.4GHz
Motherboard
ASUS P5N32-E SLI
Memory
2x1Gb Balistix
Graphics Card
GeForce 7950 GT
Sound Card
SupremeFX (ADI 1988b)
Power Supply
ENERMAX Liberty 620W
Case
RAIDMAX SMILODON ATX
CPU cooling
ARCTIC COOLING Freezer 7
GPU cooling
Passive Heat Sink + CPU Fan
OS
Win XP Pro
Monitor
2x20" Samsung SyncMaster
kyotejones is offline   Reply With Quote
Old 09-12-09   #6 (permalink)
Overclocker
 
ZHoob2004's Avatar
 
amd ati

Join Date: Mar 2007
Location: Tucson, AZ
Posts: 741

Rep: 53 ZHoob2004 is acknowledged by some
Unique Rep: 50
Folding Team Rank: 1102
Trader Rating: 0
Default

I tried a public variable but c# isn't allowed to do that. you have to use the get/set stuff and it won't work.
__________________
My case mod project: Millennium Falcon

System: Asmodeus
CPU
Phenom 9600
Motherboard
DFI LP Jr 790GX-M2RS
Memory
4GB Geil DDR2 1066
Graphics Card
ASUS EAH4870 1GB
Hard Drive
160GB, 1000GB 7200rpm
Power Supply
Silverstone 600W modular
Case
Caseless!
OS
Windows 7 RC
Monitor
Asus 21.5" 2ms
ZHoob2004 is offline I fold for Overclock.net   Reply With Quote
Old 09-12-09   #7 (permalink)
News Fiend
 
kyotejones's Avatar
 
intel nvidia

Join Date: Mar 2007
Posts: 222

Rep: 32 kyotejones is acknowledged by some
Unique Rep: 29
Trader Rating: 0
Default

Code:
using System;
using System.Collections;

namespace consoletmp
{
    ///<summary>
    /// Simple Example
    ///</summary>
    public class storage
    {
        //Attributes
        private ArrayList aryList;

        //Constructors
        public storage()
        {
            aryList = new ArrayList();
        }

        public storage(ArrayList aryList)
        {
            this.aryList = aryList;
        }

        //Behaviors
        public void addToList(object tmpObj)
        {
            aryList.Add(tmpObj);
        }

        public void delFrmList(int aryIndex)
        {
            //aryIndex is the location in the array
            //you could get fancy pantsy and do it by matching an object but im too tired
            aryList.RemoveAt(aryIndex);
        }

        //Properties
        public ArrayList AryList
        {
            get { return aryList; }
        }

    }//end Class
}//end namespace

Then when your want to add stuff to it.
Code:
Class01 tempClass = new Class01();
tempClass.addToList(ButtonObject);
you can then pass that object "tempClass" around to other objects or functions. As for the public option yes you can set variables as public variables. Is it safe? depends on the program but you can do it in c#.
__________________
System: SCUD
CPU
Pentium C2D 2.4GHz
Motherboard
ASUS P5N32-E SLI
Memory
2x1Gb Balistix
Graphics Card
GeForce 7950 GT
Sound Card
SupremeFX (ADI 1988b)
Power Supply
ENERMAX Liberty 620W
Case
RAIDMAX SMILODON ATX
CPU cooling
ARCTIC COOLING Freezer 7
GPU cooling
Passive Heat Sink + CPU Fan
OS
Win XP Pro
Monitor
2x20" Samsung SyncMaster
kyotejones is offline   Reply With Quote
Old 09-12-09   #8 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Heh? This compiles for me:

Code:
    public class Game
    {
        ArrayList buttonlist = new ArrayList();

        public ArrayList ListButton
        {
            get { return buttonlist; }
            set { buttonlist = value; }
        }
    }

    public class mainMenu
    {
        void SomeFunction()
        {
            Game Game1 = new Game();
            Game1.ListButton.Add("Works for me!");
        }
    }
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 09-12-09   #9 (permalink)
Overclocker
 
ZHoob2004's Avatar
 
amd ati

Join Date: Mar 2007
Location: Tucson, AZ
Posts: 741

Rep: 53 ZHoob2004 is acknowledged by some
Unique Rep: 50
Folding Team Rank: 1102
Trader Rating: 0
Default

I'll try out all the responses once I'm back to school on monday. +rep to everyone if it works.
__________________
My case mod project: Millennium Falcon

System: Asmodeus
CPU
Phenom 9600
Motherboard
DFI LP Jr 790GX-M2RS
Memory
4GB Geil DDR2 1066
Graphics Card
ASUS EAH4870 1GB
Hard Drive
160GB, 1000GB 7200rpm
Power Supply
Silverstone 600W modular
Case
Caseless!
OS
Windows 7 RC
Monitor
Asus 21.5" 2ms
ZHoob2004 is offline I fold for Overclock.net   Reply With Quote
Old 09-12-09   #10 (permalink)
Overclocker
 
intel ati

Join Date: Jun 2008
Posts: 979

Rep: 84 MagicBox is acknowledged by some
Unique Rep: 67
Folding Team Rank: 361
Trader Rating: 0
Default

I'm cringing at the highly dirty coding style :P

If your object creates another object it's regarded the owner. You may not allow any object to overwrite or delete the first object's object.

Code:
public class Storage
{
  private ArrayList _array = new ArrayList(); // use underscore to indicate private field

  public ArrayList List
  {
    get { return _array; } // do not implement setter as to avoid outside to overwrite the object's array instance.
  }

  /* Shortcut getter/setter to the array */
  public object this[int index]
  {
    get { return _array[index]; }
    set { _array[index] = value; }
  }
}
..
..
Storage store = new Storage();
store.List.Add("Hello");
store.List.Add("World");

Console.Write("{0} {1}", store[0], store[1]);
Enjoy

System: The Box
CPU
i7 920 D0 4.33GHz HT On (21x206)(1.39v)
Motherboard
Foxconn Bloodrage
Memory
6GB OCZ Blade 7-8-7 2GHz (1650MHz, 6-6-6)
Graphics Card
5970 (XFire) or GT395
Hard Drive
4x Seagate 160GB Raid 5
Sound Card
On-board Realtek
Power Supply
SilverStone 850W
Case
Mountain Mods U2-UFO
CPU cooling
HeatKiller 3.0 (Feser 360 Rad)
GPU cooling
Stock
OS
Vista-64
Monitor
Samsung 244T

Last edited by MagicBox : 09-12-09 at 04:45 PM
MagicBox is offline I fold for Overclock.net   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 06:24 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.13723 seconds with 8 queries