Adventure Game Studio

Community => General Discussion => Topic started by: stuh505 on Sat 10/07/2004 01:21:31

Title: C# anyone?
Post by: stuh505 on Sat 10/07/2004 01:21:31
ive been using c# and am extremely impressed with it's capabilities...which blow away C++, java, and everything else I've touched.

however, I can't figure out how to use arrays of non-primary types.  does anyone know how to do this?  example of what im trying to do:

public class Game{
public Piece[] arrPieces = new Piece[32]; //array of each piece in the game

public static void Main(){
arrPieces[1] = new Piece(this, "Pawn", 21, 2, 2, 1);
}
}

but it says an object reference is needed for the nonstatic 'Game.arrPieces'
Title: Re: C# anyone?
Post by: Alynn on Sat 10/07/2004 13:40:55
While C# isn't my specialty never really touched it... I know OOP... and with the compiler error,  and from what I know of Java (which I know isn't C#) the compiler has no idea what the class Piece is... I don't know if C# has import (I'm sure it probably has something similar) try

import class.path.Piece;

At wherever it happens to go in C#.... Thats all I can think of off the top of my head... and not really knowing the language....
Title: Re: C# anyone?
Post by: edmundito on Sat 10/07/2004 17:39:25
C# is ok... it sure beats Visual Basic... a lot.

Here's a few things I should check.

First, I'm not sure if you can initialize the array outside of a method. maybe you need to do it in main or in the constructor.

arrPieces[1] = new Piece(this, "Pawn", 21, 2, 2, 1);
Also, I'm not sure if you can use the contructor that way. You might have to do something like:
arrPieces[1].Add(this, "Pawn", 21, 2, 2, 1);

I'm looking around online for an asnwer, though, but people keep making dumb exmaples with ints and whanot!  :(
Title: Re: C# anyone?
Post by: stuh505 on Sat 10/07/2004 21:50:37
Alynn,  I wasn't showing you all of my code I am just trying to get the syntax...piece is my own class.

netmonkey: no, the array is not a class with functions.  also, it is perfecly acceptable to initialize data outside of a method...however...it IS inside a method so i don't know what you're point is.  perhaps you don't know that initialize means putting data into, perhaps you were talking about declaring the data type?  you can declare outside a method fine.
Title: Re: C# anyone?
Post by: edmundito on Sat 10/07/2004 22:27:28
oh, well, I believe that in C++, which I'm more familiar with, you can't initialize variables in the class declaration.. they have to be intitalized at least in the class constructor. My point was that maybe there was some funky error going on there.

I know what your problem is, though, because I just replicated the error you made. You need to stick a static next to the array declaration, like this:
public static Piece[] arrPieces =  <etc>

This is what the C# help says about your error:
"A variable declared outside of a method needs to have the same static declaration as the method in which it is used."

So, if you call a non static method or use a non static method declared outside a static method, the compiler will complain.  I hope some Java/C#/OOP expert volunteers and can explain why this makes sense.
Title: Re: C# anyone?
Post by: LGM on Sat 10/07/2004 22:28:21
What exactly IS C# anyway? an easier version of C++?
Title: Re: C# anyone?
Post by: Pumaman on Sat 10/07/2004 23:26:00
Yes, you can't access non-static variables from within a static method - it doesn't make sense to do so.

Because there is only ever one instance of a static method, it is not associated with an instance of the class so it wouldn't know which object's variable to access.

LGM: C# is a new programming language, designed to take the best bits of Java and C++ and combine them into something more elegant and easy to use.
Title: Re: C# anyone?
Post by: Ryam BaCo on Sun 11/07/2004 02:14:18
Quote from: Pumaman on Sat 10/07/2004 23:26:00
YLGM: C# is a new programming language, designed to take the best bits of Java and C++ and combine them into something more elegant and easy to use.
although these java-guys try to convince c#-programmers by using some c#-specific stuff in the new jdk 1.5. .NET vs. sun ;)
c++ is still faster than c#

why don't you use an arraylist instead of dumb, lame arrays???

ArrayList ar=new ArrayList();
ar.Add(new Piece...blablabla
Title: Re: C# anyone?
Post by: LGM on Sun 11/07/2004 05:19:04
Well.. What's J# then/?
Title: Re: C# anyone?
Post by: edmundito on Sun 11/07/2004 11:38:36
J# is the official name for Microsoft's .net thingy that has the same exact syntax as Java, so you can port your Java-written programs easily to the .net environment. It's called J# because of the whole Sun Microsystems Vs. Microsoft Lawsuit and I think microsoft couldn't call it Java.
Title: Re: C# anyone?
Post by: Scorpiorus on Sun 11/07/2004 15:34:20
Quote from: Pumaman on Sat 10/07/2004 23:26:00Because there is only ever one instance of a static method, it is not associated with an instance of the class so it wouldn't know which object's variable to access.
Quotebut it says an object reference is needed for the nonstatic 'Game.arrPieces'

stuh505: And thus the compiler asks you to provide a reference to an instance of the Game class so that it will know the variable of which object to access. Not sure about the syntax in C# but anyways:

public class Game{
public Piece[] arrPieces = new Piece[32]; //array of each piece in the game

public static void Main(Game &InstanceRef){
InstanceRef.arrPieces[1] = new Piece(this, "Pawn", 21, 2, 2, 1);
}
}