Help Me With C#

Help Me With C#

Post just about everything that isn't directly related to Spring here!

Moderator: Moderators

Post Reply
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Help Me With C#

Post by SinbadEV »

I am Trying to Create an array of a class I have defined, but it keeps throwing errors etc.

THis is My Class

Code: Select all

        class BlockType
        {
            public String blockName;
            public String blockImage;
            public Color blockColor;
            public int blockStickyness;//0=None, 1=Landed
            public BlockType()
            {
                blockName="Blank";
                blockImage = null;
                blockColor=Color.White;
                blockStickyness = 0;
            }
            public BlockType(String blockName, Color blockColor, String blockImage, int blockStickyness)
            {
            }
                public BlockType(BlockType previousBlockType)
            {
                blockName=previousBlockType.blockName;
                blockImage = previousBlockType.blockImage;
                blockColor=previousBlockType.blockColor;
                blockStickyness =previousBlockType.blockStickyness;
            }
Which Compiles, but I can't create an array of instances,

Code: Select all

BlockType block0 = new BlockType(); // Works
BlockType block1 = new BlockType("RedVitamin",Color.Red,"Vitamin",1); // Works
BlockType block2 = new BlockType(block1); // Doesn't Work
BlockType[] blockTemplates = new BlockType[4];//Works
but then
blockTemplates[1] = new BlockType(); // Doesn't
The syntax errors I get are:

Code: Select all

Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
Invalid token '=' in class, struct, or interface member declaration
Class, struct, or interface method must have a return type
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Code: Select all

BlockType[] blockTemplates = new BlockType[4];//Works 
but then 
blockTemplates[1] = new BlockType(); // Doesn't
This is teh equivilant of

Code: Select all

int* a = new int[4];

a[1] = new int;
You've already created a set of blocks in the first line and what you're doing in the second is illogical. You're assuming that the items in the array are behaving as if it was int** a not int* a, thus the second statement is not needed.

In C#, anything more complex than a structure is passed around as a refrence not a value, and as such models the behaviour of C++ pointer than a C++ value, but with garbage collection added in.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Post by SinbadEV »

What I am trying to do is create an array of objects and then replace one of the objects with ones with new values in theory using the copy constructor on a new object of that type aka:

the second member objects of the blockTemplate array should be replaced by an "new" object created using one of the BlockType member contructor methods (aka using the copy contructor)

that's what I was trying to do at least... I'm not sure why I can't but I'm getting the picture, perhaps I'll just add a method to set all the member variables instead
User avatar
rattle
Damned Developer
Posts: 8278
Joined: 01 Jun 2006, 13:15

Post by rattle »

Shouldn't blockTemplates[1][1] = new BlockType(); work?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

no because that would require the array be 2 dimensional which it isnt.


You have two options:

Code: Select all

BlockType[] blockTemplates = new BlockType[4];
BlockType b = new BlockType();
blockTemplates[1] = b;
OR

implement a reset or wipe type method in the Block class.

OR

store them using a different method.

By the way what purpose does the array serve in your code?
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

BlockType[] blockTemplates = new BlockType[4];
BlockType b = new BlockType();
blockTemplates[1] = b;
If that works, then
BlockType[] blockTemplates = new BlockType[4];
blockTemplates[1] = new BlockType();
Should work fine too.
In fact it compiles just fine for me, so i dont think the error you showed is about that piece of code...
For reference, I'm using vs2005
User avatar
RogerN
Posts: 238
Joined: 24 Jul 2006, 23:29

Post by RogerN »

I didn't see any glaring syntax errors in your code, so I tried to compile it as-is. It compiled without any errors or warnings. I'm using Visual Studio .NET 2003.

EDIT: Actually, that's not *entirely* true... I had to add an extra '}' to close off your class declaration. I assumed that was a copy and paste error, though, rather than an actual error in your code.
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Re: Help Me With C#

Post by ILMTitan »

SinbadEV wrote:

Code: Select all

        class BlockType
        {
            public String blockName;
            public String blockImage;
            public Color blockColor;
            public int blockStickyness;//0=None, 1=Landed
            public BlockType()
            {
                blockName="Blank";
                blockImage = null;
                blockColor=Color.White;
                blockStickyness = 0;
            }
            public BlockType(String blockName, Color blockColor, String blockImage, int blockStickyness)
            {
            }
                public BlockType(BlockType previousBlockType)
            {
                blockName=previousBlockType.blockName;
                blockImage = previousBlockType.blockImage;
                blockColor=previousBlockType.blockColor;
                blockStickyness =previousBlockType.blockStickyness;
            }
I think the problem is with the public BlockType(String blockName, Color blockColor, String blockImage, int blockStickyness){} because you do not initilaize any of your fields.

So when you

Code: Select all

BlockType block1 = new BlockType("RedVitamin",Color.Red,"Vitamin",1); // Works
BlockType block2 = new BlockType(block1); // Doesn't Work 
in the new BlockType(block1), you are assigning from fields that have not been initilized. I don't know C#, but it is possible that that causes an exception or whatever.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Post by SinbadEV »

just so you know

Code: Select all

        BlockType[] blockTemplates = new BlockType[4]; 
        blockTemplates[1] = block2; 
doesn't work and it won't compile for me to test it

and when you say:
in the new BlockType(block1), you are assigning from fields that have not been initilized.
I don't know what you mean, those fields should be inicialised eith the contructor:

Code: Select all

            public BlockType()
            {
                blockName="Blank";
                blockImage = null;
                blockColor=Color.White;
                blockStickyness = 0;
            } 
I think AF said it right but I don't know how to fix it.

and in response to jsconnsen; if I comment out the " blockTemplates[1] = block2; [/code]" part it compiles and the errors go away.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Post by SinbadEV »

Oh and to answer what I'm doing, I'm making a falling Block game, but abstracted to the point that I can implement Tetris or Dr.Mario or Columns with a preference file., for example(not done because I'm still working out kinks):

Game Deffinition: Dr. Mario-Like Game,

Code: Select all

Game{
gameName=Dr. What's His Face
 

blockImages {
1:
blockImageName="Vitamin"
Default="Vitamin.bmp"
UpLink="VitaminU.bmp"
DownLink="VitaminD.bmp"
LeftLink="VitaminL.bmp"
RightLink="VitaminR.bmp"
//Others don't need to be defined in this game
2:
blockImageName="Virus"
Default="" //could have unique images for each Virus but not worth bothering with it yet
}
 
blockTypes {
0:
blockName="Blank"
1:
blockName="RedVirus"
selfStick="All"
fieldStick="None"
blockImages="Virus"
color="Red"
2:
blockName="YellowVirus"
selfStick="All"
fieldStick="None"
blockImages="Virus"
color="Yellow"
3:
blockName="BlueVirus"
selfStick="All"
fieldStick="None"
blockImages="Virus"
color="Blue"
4:
blockName="RedVitamin"
selfStick="All"
fieldStick="None"
blockImages="Vitamin"
color="Red"
5:
blockName="YellowVitamin"
selfStick="All"
fieldStick="None"
blockImages="Vitamin"
color="Yellow"
6:
blockName="BlueVitamin"
selfStick="All"
fieldStick="None"
blockImages="Vitamin"
color="Blue"
}
 
pieceMasks {
1:
pieceName="MultiVitamin"
int X=2
int Y=2
id[0]=Blank
id[1]=randomVitamin()
id[2]=randomVitamin()
positionMask[0] = (1,2,0,0)
positionMask[1] = (1,0,2,0)
rotationLinks[0] = (Key1:(1,1,1),Key2:(1,1,2))
rotationLinks[1] = (Key1:(0,1,1),Key2:(0,1,2))
}
 
randomVitamin()
{
blockTypes[random from 3,4,5]
//if there were more vitamins, let's say green or purple, we could make it so that for early levels you got certain colours and later levels got more
}
 

randomVirus()
{
blockTypes[random from 3,4,5]
//if there were more vitamins, let's say green or purple, we could make it so that for early levels you got certain colours and later levels got more
}

 
fieldInit()
{
X=10
Y=20
//Infect
if (Level<8) {
InfectionHeight=Level+10
}
else {
InfectionHeight=18
}
 
if (Level<95) {
infectionDensity=Level+5
}
else
{
infectionDensity=100; //%
}
 
 
for (a=0, a<((infectionHeight*X)/100)*infectionDensity, a++)
{
b=random from 0 to X
c=random from 0 to infectionHeight
if (field[b][c]==0)
{
blockTypes[random from 1,2,3]
} else {
a--
}
}
}
 
clearPatterns
{
1:
Line(1,4)
Points = ((Level*10)+(Blocks*10)+(Blocks[1]*100))*Chain
2:
Line(4)
3:
Line(2,5)
4:
Line(5)
5:
Line(3,6)
6:
Line(6)
}
 
}
Except Prettier and probably XML istead of Bastard Psudo-C++
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Post by SinbadEV »

Okay, let's break this down further.

Code: Select all

        int[] intTemplates = new int[4]; //no errors
        intTemplates[1]=1; //errors

I must be thinking about something wrong here.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Post by SinbadEV »

Never mind... apparently you can create an object array as a global but you can't access it's members outside of main... I guess I'm the n00b.
Post Reply

Return to “Off Topic Discussion”