|
|
|
#1 (permalink) | ||||||||||
|
Overclocker
![]() |
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);
__________________
My case mod project: Millennium Falcon
Last edited by ZHoob2004 : 09-11-09 at 04:17 PM |
||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
AMD Overclocker
![]() |
Can you post all relevant code?
|
|||||||||||||
|
|
|
|
|
#3 (permalink) | ||||||||||
|
Overclocker
![]() |
k OP updated with all applicable code
__________________
My case mod project: Millennium Falcon
|
||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
New to Overclock.net
|
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.
__________________
|
|||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||
|
News Fiend
![]() |
public varible?
__________________Code:
public ArrayList buttonlist = new ArrayList();
|
||||||||||||
|
|
|
|
|
#6 (permalink) | ||||||||||
|
Overclocker
![]() |
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
|
||||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||
|
News Fiend
![]() |
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);
|
||||||||||||
|
|
|
|
|
#8 (permalink) |
|
Case Modder
![]() |
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
|
|
|
|
|
|
#9 (permalink) | ||||||||||
|
Overclocker
![]() |
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
|
||||||||||
|
|
|
|
|
#10 (permalink) | |||||||||||||
|
Overclocker
![]() |
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]);
Last edited by MagicBox : 09-12-09 at 04:45 PM |
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|