C# ArrayList help please

HavocXphere

Honorary Master
Joined
Oct 19, 2007
Messages
33,153
Reaction score
1,297
Location
Europe
EDIT: Nevermind. Problem solved.

I'm busy writing a Steam Specials monitoring app. Visual C# 2010 Express.

I've painted myself into a bit of a corner with my attempt at OO.:o

2 classes: GameList contains an ArrayList of class SteamGame. The relevant code fragments:

Code:
public class SteamGame
    {
        public string GameName = "", GameURL ="", NewPrice = "", OldPrice = "";

        private string SHA1Hash = "";
        public string SHA1()
        {
            //If its not available yet then calculate it
            if (SHA1Hash == "")
<snip>

Code:
public class GameList
    {
        public ArrayList _dataList = new ArrayList();
        public string SHA1()
        {
            //If its not available yet then calculate it
            if (SHA1Hash == "")
            {
                SHA1Hash = BitConverter.ToString(SHA1Managed.Create().ComputeHash(Encoding.Default.GetBytes(GameName+GameURL+OldPrice+NewPrice))).Replace("-", "");                
            }
            //Return the hash
            return SHA1Hash;
        }

        public SteamGame (string GameName, string GameURL, string NewPrice, string OldPrice)
        {
            this.GameName = GameName;
            this.GameURL = GameURL;
            this.NewPrice = NewPrice;
            this.OldPrice = OldPrice;
        }

And me trying to utilize them:
Code:
//Create MyList
            GameList MyList = new GameList();
            
            //Add a game
            MyList.AddGame("MyGame","MyURL","MyOldPrice","MyNewPrice");
           
            SteamGame MyGame = (SteamGame)MyList._dataList[0];

That all works as expected. I can copy something out of the ArrayList (Very last line in code). But I want to access the SHA1 method while its still in the ArrayList & it doesn't let me do that. I want to:
Code:
string SHA = (SteamGame)MyList._dataList[0].SHA1();

The compiler doesn't like that::(
Code:
'object' does not contain a definition for 'SHA1' and no extension method 'SHA1' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
I'm thinking there is some problem with the (SteamGame) casting. Any help would be appreciated.
 
Last edited:
1. Use generic List<T> unless you absolutely have to use ArrayList for some obscure reason.
2. Change the line to the following (note extra parentheses):

Code:
string SHA = ((SteamGame) MyList._dataList[0]).SHA1();

Your code is trying to cast the result of MyList._dataList[0].SHA1() as a SteamGame. You have to cast the value obtained from the ArrayList to a SteamGame, then call SHA1() on that, as per my example.
 
1. Use generic List<T> unless you absolutely have to use ArrayList for some obscure reason.
I had trouble accessing the lists via index originally, but thats sorted now so I don't need the ArrayList anymore.

Your code is trying to cast the result of MyList._dataList[0].SHA1() as a SteamGame. You have to cast the value obtained from the ArrayList to a SteamGame, then call SHA1() on that, as per my example.
Ah. Makes sense. Thanks.

The app is finished now: http://mybroadband.co.za/vb/showthread.php?229146-Steam-Special-Monitor :)
 
You really shouldn't target the 4.0 Framework for a simple WinForms app. Looks good however...
Any specific reason? To me it looked like that is the fashionable thing to do atm & appears to be the default on that IDE.:whistle:

What would be the correct thing to use? WPF Application?
 
[*]You're restricting you're applications audience. (4.0 isn't installed on my home box yet for example)
Yeah that is annoying. Maybe I'll port it. Should be trivial.

It's the default because you're using Visual Studio 2010. But you can target earlier frameworks as well.
Just checked. Express edition = No choice. Or at least thats how I read it. Not sure what they mean by the last sentence:
Express Editions of Visual Studio do not allow the choice of .NET Framework versions or profiles when initially creating a project. You can later retarget the project to any installed .NET Framework version.

For WPF, does the user need to install anything or will it run on all boxes?
 
Found this, might help you...
Thats the page the quote above came from. Doesn't really matter too much though...I'm mainly interested in writing code I need to make my life easier not for an audience.

AFAIK you need at least .NET 3.0 or better. Again that'll depend on which Framework version you develop against...
Thanks

Different Q though: In a Form_Load method, a call of Application.Exit(); has zero effect. Any idea why that is? I ended up jury-rigging it w/ a timer which is just nasty.
 
That's weird it should work :confused:
Just checked it again & its working now.:o:o:o

But I'm certain there was a problem. Was def bound as the rest of the code in there worked. It must be something in the windows code itself because I had the same problem under Delphi 6 too and there are other people complaining about the same thing.

That means if the user launches your application nothing will happed....:sick:
Special circumstances. Its intended to be launched at windows startup & not bother the user if there is nothing to report.
 
Nice app. Reasons to compile for lesser versions (2.0 comes to mind) -

Most PCs will have Fx 2.0 installed. Vista comes with 2.0 preinstalled - W7 has 3.5. 2.0 has Generics, so you won't lose much in the translation.
Don't use WPF unless you intend to port to Silverlight. - I'm talking to beginners here - once you know WPF, go with the flow. Bear in mind WPF requires Fx 3.0 and above.

Oh, and for those in the know, C# 2.0 ports across to Mono. ;)

Reading the thread, you can 'downgrade' your project once created. I'll check this at home, where my son (13yrs) is using Express 2010 to learn C#.

Kudos to dequadin for (a) nice clean answer(s) that was a pleasure to read.
 
Last edited:
Don't use WPF unless you intend to port to Silverlight. - I'm talking to beginners here - once you know WPF, go with the flow.
Why so? I find WPF powerful from a dynamic UI perspective - it's MUCH easier to dynamically load controls to the window that with normal WinForms. You can just serialize the objects to XML, transform with XSLT to the appropriate XAML and viola - instant WPF form.

I agree with flow - wonderful and powerful to add controls and to structure them on the form.

Oh, and for those in the know, C# 2.0 ports across to Mono. ;)
Yup - this will make it runnable in *nix environments, which includes OSX...

...where my son (13yrs) is using Express 2010 to learn C#.
Nice, getting him into it at such a young age! Back then I only had Turbo Pascal to keep me company... Wish something like .NET was available way back then. :(
 
Why so? I find WPF powerful from a dynamic UI perspective - it's MUCH easier to dynamically load controls to the window that with normal WinForms. You can just serialize the objects to XML, transform with XSLT to the appropriate XAML and viola - instant WPF form.
Good for that, but for a simple display as with the OP app I think it's a bit much. Besides which WPF is not ideal (if even possible) on Fx 2.0.

Nice, getting him into it at such a young age! Back then I only had Turbo Pascal to keep me company... Wish something like .NET was available way back then. :(
Same here. Don't be too hard on good old Pascal, you learn a lot when all you have to work with are arrays and loops.
Yeah, I have him working on his science project for next year. This year's school winner was a flash game - and a simple one at that. We're gonna kill them next year. ;)
 
I tried porting it to WPF. Figured it would be easy because all the main code is in a class. But the listview behaves differently in .NET and WPF.

Reading the thread, you can 'downgrade' your project once created. I'll check this at home, where my son (13yrs) is using Express 2010 to learn C#.
I'd appreciate that for future reference. Not sure its possible though....even the bloody console apps demand v4.0.

Reasons to compile for lesser versions (2.0 comes to mind) -
Its back to uni for me tomorrow, so this particular app is on hold for now.

Oh, and source is over here if anyone cares & official released as free for any purpose. (Forgive the noob grade code quality)

FarligOpptreden said:
It's nice to read another nice, technical dequadin investigation again...
Raithlin said:
Kudos to dequadin for (a) nice clean answer(s) that was a pleasure to read.
Indeed.
 
Ok, so I converted and compiled for Fx 2.0 using VS2008 - had to remove the LINQ references (unused). I can't use it as is though, because I'm behind a proxy. Still, it runs on the 2.0 framework quite happily.

I'll gladly upload the source if you want, Havoc.
 
Source uploaded: http://www.quickshare.co.za/files/3aop5xdb/Steam_Monitor_1.zip.html

Havoc, I changed the way your code looks (best practices and such), but I haven't touched the logic. Not bad for a newb. ;) If I run it through Code Analysis now I only get 2 warnings - strong key and a resources setting - started with 26 (no globalization warnings enabled).

Changelog:
  • Removed LINQ references
  • Separated classes into own files
  • Changed Public accessors to Internal
  • Encapsulated public variables within properties
  • Applied best naming practices

Why did I do this? I was bored, and I figured it might help you with your next project. Nothing wrong with your coding skills though - and the more your practice the better you'll get!
 
Last edited:
Not bad for a newb. ;) If I run it through Code Analysis now I only get 2 warnings - strong key and a resources setting - started with 26 (no globalization warnings enabled).

Changelog:
  • Removed LINQ references
  • Separated classes into own files
  • Changed Public accessors to Internal
  • Encapsulated public variables within properties
  • Applied best naming practices

Why did I do this? I was bored, and I figured it might help you with your next project. Nothing wrong with your coding skills though - and the more your practice the better you'll get!
Thanks. Learned quite a bit from that.
 
Top
Sign up to the MyBroadband newsletter
X