User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » C# question HALP!!! I CAN HAS ERROR MESSAGES?!? Page [1]  
spöokyjon

18617 Posts
user info
edit post

Planet[] planetArray = new Planet[5];

planetArray[0] = new Planet(10, 21, 100, 100, 5, 5, System.Drawing.Brushes.AliceBlue);



The second line is giving me several errors:

Error 1 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
Error 2 Invalid token '=' in class, struct, or interface member declaration
Error 3 Method must have a return type
Error 4 Type expected
Error 5 Invalid token ')' in class, struct, or interface member declaration

What am I doing wrong?

4/16/2008 9:27:19 AM

qntmfred
retired
40729 Posts
user info
edit post

putting this in chit chat, for one

4/16/2008 9:31:37 AM

quagmire02
All American
44225 Posts
user info
edit post

^

4/16/2008 9:31:50 AM

spöokyjon

18617 Posts
user info
edit post

I put it in tech talk and nobody responded.

4/16/2008 9:34:15 AM

synapse
play so hard
60939 Posts
user info
edit post

all they do in this section is post gifs of dancing fruit.

4/16/2008 9:35:17 AM

spöokyjon

18617 Posts
user info
edit post

Plus it's in chit chat so people can rickroll it and post tittyballs and whistle for a cab and when it came near.

But hopefully one person will be nice enough to help me with my noobishness.

4/16/2008 9:35:24 AM

Oeuvre
All American
6651 Posts
user info
edit post

4/16/2008 9:36:28 AM

qntmfred
retired
40729 Posts
user info
edit post

i dunno man, it compiles ok for me

[Edited on April 16, 2008 at 9:40 AM. Reason : prolly a syntax error in a surrounding line. forgot a ) or something]

4/16/2008 9:39:48 AM

pttyndal
WINGS!!!!!
35217 Posts
user info
edit post

That's what I was thinking but in the other thread he said that these work. So I'm assuming he was just replacing the code above.

Quote :
"Planet testPlanet = new Planet(10, 21, 100, 100, 5, 5, System.Drawing.Brushes.AliceBlue);

Planet planet2 = new Planet(20, 50, 300, 300, 2, 3, System.Drawing.Brushes.BlanchedAlmond);"

4/16/2008 9:43:48 AM

spöokyjon

18617 Posts
user info
edit post

Yeah...basically, the constructors not using the array work fine. If I insert the one with the array
planetArray[0] = new Planet(10, 21, 100, 100, 5, 5, System.Drawing.Brushes.AliceBlue);
that line, specifically, is the one which has the errors.

4/16/2008 9:48:34 AM

spöokyjon

18617 Posts
user info
edit post

THESE PRETZELS ARE MAKING ME THIRSTY

4/16/2008 10:06:43 AM

qntmfred
retired
40729 Posts
user info
edit post

like i said, it compiles just fine for me. post the entire code or something.

4/16/2008 10:07:50 AM

BigMan157
no u
103354 Posts
user info
edit post

THIS SHALL BE PRECEDENT

IF THE MODS CAN CREATE DUPLICATE THREADS

THEN BY GOLLY

SO CAN I

p.s. maybe something like Planet[] planetArray = new Array[5]; will fix it? (i have no idea about C#)

4/16/2008 10:14:47 AM

392
Suspended
2488 Posts
user info
edit post

Quote :
"i have no idea about C#"

yeah me either -- I was gonna say that "new Planet[5]" should be "new Planet(5)",

but apparently c# does use brackets for array size declarations -- whatever

4/16/2008 10:18:49 AM

pttyndal
WINGS!!!!!
35217 Posts
user info
edit post

Quote :
"SomeClass[] arrSC = new SomeClass[44];
for (int i = 0; i < 44; i++)
{
arrSC[i] = new SomeClass();
}"


apparently that works. maybe it doesn't like the idea of having the position hard coded in. try setting it to like int i=5 and then doing planetArray[i].....

4/16/2008 10:28:55 AM

qntmfred
retired
40729 Posts
user info
edit post

i'm tellin ya, it works fine. you shouldn't need to initialize via loop or any such nonsensery

4/16/2008 10:31:43 AM

spöokyjon

18617 Posts
user info
edit post

Full code:
(Disclaimer, as in the other thread--I haven't done any programming in years, and I'm learning C# for funsies all from the built-in documentation; this code is probably ghetto as all hell)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tehgraphhhixzss
{
public partial class planets : Form
{
private System.Windows.Forms.Timer timer1;

public planets()
{
InitializeComponent();

this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer, true);
}

public class Planet
{
double mass, radius, x, y, dx, dy = 0;

System.Drawing.Brush planetColor;

public Planet()
{
//nothing
}
public Planet(double m, double r, double xpos, double ypos, double deltax, double deltay, System.Drawing.Brush pColor)
{
mass = m;
radius = r;
x = xpos;
y = ypos;
dx = deltax;
dy = deltay;
planetColor = pColor;
}

public void Draw(System.Drawing.Graphics graphicsContext)
{
System.Drawing.Rectangle planetRect =
new System.Drawing.Rectangle(
(int)(x - radius), (int)(y - radius), (int)(radius), (int)(radius));
graphicsContext.FillEllipse(planetColor, planetRect);
// graphicsContext.DrawEllipse(penColor, planetRect);
}

public void Update()
{
x += dx;
y += dy;
}

public double getX()
{
return x;
}
public double getY()
{
return y;
}
}


int timesDrawn = 0;

Planet[] planetArray = new Planet[2];

Planet planet1 = new Planet(10, 21, 100, 100, 5, 5, System.Drawing.Brushes.AliceBlue);

Planet planet2 = new Planet(20, 50, 300, 300, 2, 3, System.Drawing.Brushes.BlanchedAlmond);

//the following line won't compile
//planetArray[1] = new Planet(10, 21, 100, 100, 5, 5, System.Drawing.Brushes.AliceBlue);


protected override void OnPaint(PaintEventArgs e)
{
planet1.Draw(e.Graphics);
planet2.Draw(e.Graphics);
timesDrawn++;
}


private void timer1_Tick(object sender, System.EventArgs e)
{
planet1.Update();
planet2.Update();
this.Invalidate();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void label1_Click(object sender, EventArgs e)
{
// label1.Text = label1.Text + "!";
}
}
}

4/16/2008 10:32:51 AM

Rat
Suspended
5724 Posts
user info
edit post

don't use an array. ever. they are dumb

do this...

make an object called Planet (sounds like you have one)

then make a List<Planet> myPlanetList and start adding them to the list

myPlanetList.add(_someNewPlanetIjustCreated);

4/16/2008 10:33:59 AM

IRSeriousCat
All American
6092 Posts
user info
edit post

attention jon morgan,

i do indeed believe i know the answer to your question.

feel free to PM me.

also, are you coding for fun now?

4/16/2008 10:34:23 AM

qntmfred
retired
40729 Posts
user info
edit post

yes, use Lists instead.

move that line to the constructor and it'll work

4/16/2008 10:42:31 AM

Rat
Suspended
5724 Posts
user info
edit post

yeh thats b/c arrays are declared this way:

Array planetarray = new Array[100];


change it too

List<Planet> myPlanetList = new List<Planet();
Planet p = new Planet(20, 50, 300, 300, 2, 3, System.Drawing.Brushes.BlanchedAlmond);
myPlanetList.add(p);


get my drift???

4/16/2008 10:42:41 AM

spöokyjon

18617 Posts
user info
edit post

               Planet planet1 = new Planet(10, 21, 100, 100, 5, 5, System.Drawing.Brushes.AliceBlue);

Planet planet2 = new Planet(20, 50, 300, 300, 2, 3, System.Drawing.Brushes.BlanchedAlmond);

List myPlanetList = new List();

myPlanetList.Add(planet1);
myPlanetList.Add(planet2);


Error 1 Invalid token '(' in class, struct, or interface member declaration
Error 2 Invalid token ')' in class, struct, or interface member declaration
Error 3 Invalid token '(' in class, struct, or interface member declaration
Error 4 Invalid token ')' in class, struct, or interface member declaratio



HOW COULD THIS HAPPEN TO MEEEEEEEEEEEEEEEEEEE

4/16/2008 10:54:58 AM

Oeuvre
All American
6651 Posts
user info
edit post

reinstall .NET

4/16/2008 10:55:45 AM

Rat
Suspended
5724 Posts
user info
edit post

List<Planet> myPlanetList = new List<Planet>();


copy past that motherfcker ver batim

[Edited on April 16, 2008 at 10:57 AM. Reason : .]

4/16/2008 10:56:33 AM

pttyndal
WINGS!!!!!
35217 Posts
user info
edit post

lol. C# hates you

4/16/2008 10:56:49 AM

spöokyjon

18617 Posts
user info
edit post

I can literally copy the following example from MSDN documentation:

        List dinosaurs = new List();

Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus");


And it gives me the same error.

4/16/2008 10:58:20 AM

Rat
Suspended
5724 Posts
user info
edit post

lolz. listen to me or qntmfred or you lose.

4/16/2008 11:01:16 AM

qntmfred
retired
40729 Posts
user info
edit post

you can't do planetArray[0] or planetList.Add() out in the middle of the class, it has to be within a method or constructor.

4/16/2008 11:10:55 AM

spöokyjon

18617 Posts
user info
edit post

^ Gotcha. I think that was the main cause of all my woes.

4/16/2008 11:19:19 AM

pttyndal
WINGS!!!!!
35217 Posts
user info
edit post

haha. I didn't even notice that they were hanging out all alone. oh and qntmfred for tww owner.

4/16/2008 11:21:40 AM

spöokyjon

18617 Posts
user info
edit post

Okay, second (third? fifth??) retarded question:

where can I put the list declaration that isn't just in the body of the class where it's accessible by all the relevant methods? IOW, I was just declaring the individual objects in the body of the class and then using them in timer1_Tick() and Update(). Where should they go now?

I should have called this thread HOLD MY HAND, BITCHES.

4/16/2008 11:28:48 AM

Rat
Suspended
5724 Posts
user info
edit post

you would want to declare it outside the methods and constructor then, i do it above the constructors usually.

make it public if you want to be able to access it from anywhere

make it private if you are only using it in your 1 class there

4/16/2008 11:32:31 AM

qntmfred
retired
40729 Posts
user info
edit post

you can put those objects out in the middle of the class (declare it). but once you start doing something with it, you gotta put it in a method

public class Example
{

private int i = 0; // this is a declaration of the field i. we also optionally initialize the value to 0

Example()
{
i = 3; // the variable i is within the scope of the constructor, so we can access it here
int a = 7; // this variable is only within the scope of the constructor. other methods can't get to it
}
}


[Edited on April 16, 2008 at 11:34 AM. Reason : .]

4/16/2008 11:33:06 AM

spöokyjon

18617 Posts
user info
edit post

Thank you very much guys, I now have it running.

Rat and qntmfred, I owe you guys a handjob.

4/16/2008 11:41:02 AM

qntmfred
retired
40729 Posts
user info
edit post

yay!

wait, wat?

4/16/2008 11:43:48 AM

Rat
Suspended
5724 Posts
user info
edit post

just not at the same time

4/16/2008 11:45:29 AM

pttyndal
WINGS!!!!!
35217 Posts
user info
edit post

4/16/2008 11:46:47 AM

qntmfred
retired
40729 Posts
user info
edit post

^^ see, i was thinking the opposite. either both at once or i'm out

4/16/2008 11:51:43 AM

Rat
Suspended
5724 Posts
user info
edit post

cscirclejrek

4/16/2008 11:52:58 AM

LadyWolff
All American
2286 Posts
user info
edit post

BEST PROGRAMMING THREAD EVAR!!!

That said, boy do I need to look up some C# and C++ syntax crap.
You'd think I'd get it with java vut there's a few lines in there I'm like...do wtf?

5/10/2008 10:08:37 PM

qntmfred
retired
40729 Posts
user info
edit post

such as ?

5/10/2008 11:47:03 PM

colter
All American
8022 Posts
user info
edit post

what the hell language are you speaking?



HiGh ScHoOl ViRgIn?

5/10/2008 11:55:43 PM

Walter
All American
7770 Posts
user info
edit post

i got a C- in Java

i r not very good at programming

5/10/2008 11:57:36 PM

spöokyjon

18617 Posts
user info
edit post

I CAN HAS MORE PROBLEMS
/message_topic.aspx?topic=526919

5/20/2008 12:34:56 PM

 Message Boards » Chit Chat » C# question HALP!!! I CAN HAS ERROR MESSAGES?!? Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.