Home › Forums › Programming › A bit of help for C#
- This topic has 16 replies, 4 voices, and was last updated 12 years, 5 months ago by Anonymous.
-
AuthorPosts
-
-
01/07/2012 at 9:44 am #8486AnonymousInactive
Hi all,
Im trying to make a game in C# using the TAO Framework from a book i bought a few days ago (C# Game Programming by Daniel Schuller). Im stuck on coding the games loop/fast games loop, some of the errors say that im missing an Assembly reference, can anyone please help me out and give me a few pointers?Thanks
-
01/07/2012 at 1:15 pm #48663AnonymousInactive
Can you post the actual error message(s) you’re getting?
Usually assembly reference errors are caused when the code contains ‘using’ that doesn’t exist e.g. ‘using System.Blah.Forms’.
You may need to add the assembly reference manually to your project. If you’re using Visual Studio then find the "References" folder underneath your solution name, right click on it, and pick "Add Reference".
Alternatively, as TAO is a Mono framework (had to google that :P) there may be a version issue where the framework the book is using is not the same as the latest Mono framework and the missing namespace has been refactored with a different name or removed from the framework.
-
01/07/2012 at 11:16 pm #48664AnonymousInactive
Can you post the actual error message(s) you’re getting?
Usually assembly reference errors are caused when the code contains ‘using’ that doesn’t exist e.g. ‘using System.Blah.Forms’.
You may need to add the assembly reference manually to your project. If you’re using Visual Studio then find the "References" folder underneath your solution name, right click on it, and pick "Add Reference".
Alternatively, as TAO is a Mono framework (had to google that :P) there may be a version issue where the framework the book is using is not the same as the latest Mono framework and the missing namespace has been refactored with a different name or removed from the framework.[/quote:713ced4b5b]
Ive just been doing C# programming for a few weeks so could you please give me an example of what a assembly reference would look like?
-
02/07/2012 at 9:13 am #48665AnonymousInactive
Ive just been doing C# programming for a few weeks so could you please give me an example of what a assembly reference would look like?[/quote:8890cfd546]
Are you getting an error message that reads something like this:
Type or namespace <X> could not be found. Are you missing an assembly reference?[/quote:8890cfd546]
Assemblies are collections of code files and resources that are grouped together in a logical unit (generally related by functionality). They’re compiled into *.dll files and then you can add references to these code libraries to your projects and reuse the functionality.
They have names like "System.IO" or "System.Data.Sql".
At the very top of your C# code file there should be a series of "using" lines (if you have used C++ at all the syntax could be likened to #include for your header files)
[code:1:8890cfd546]
using System.IO;
using System.Web;
[/code:1:8890cfd546]What IDE are you using? If you are using Visual Studio then you would be able to see what references are already attached to your project by viewing the References folder:
(as an example)
-
03/07/2012 at 5:58 pm #48669AnonymousInactive
Ive just been doing C# programming for a few weeks so could you please give me an example of what a assembly reference would look like?[/quote:b015b747ea]
Are you getting an error message that reads something like this:
Type or namespace <X> could not be found. Are you missing an assembly reference?[/quote:b015b747ea]
Assemblies are collections of code files and resources that are grouped together in a logical unit (generally related by functionality). They’re compiled into *.dll files and then you can add references to these code libraries to your projects and reuse the functionality.
They have names like "System.IO" or "System.Data.Sql".
At the very top of your C# code file there should be a series of "using" lines (if you have used C++ at all the syntax could be likened to #include for your header files)
[code:1:b015b747ea]
using System.IO;
using System.Web;
[/code:1:b015b747ea]What IDE are you using? [/quote:b015b747ea]
I have been getting that error and im using Visual Studio Express. I tried to find whats wrong with the coding but i cant fix it. I think i’ll just upload the code to show you.
[code:1:b015b747ea]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace GameLoop
{
[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr hWnd;
public Int32 msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}public class FastGamesLoop
{
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(
out Message msg,
IntPtr hWnd,
uint messageFilterMin,
uint messageFilterMax,
uint flags);
}
public class FastLoop
{
PreciseTimer_Timer = new PreciseTimer();
public delegate void LoopCallback(double elapsedTime);
LoopCallback_callback;public FastLoop (LoopCallback callback)
{
_callback = callback;
Application.Idle +=new EventHandler(Application_Idle);
}void ApplicationEnterIdle(object sender, EventArgs e)
{
while(IsAppStillIdle())
{
_callback(_timer.GetElapsedTime());
}
}private bool IsAppStillIdle()
{
Message msg;
return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}private bool PeekMessage(out Message msg,IntPtr intPtr,int p,int p_2,int p_3)
{
throw new NotImplementedException();
}public LoopCallback _callback { get; set; }
public EventHandler Application_Idle { get; set; }}}
[/code:1:b015b747ea]Thats just the first bit of code(FastLoop). Im also having the same issue with the PreciseTimer part of the program also showing the same error. The PreciseTimer code will be shown:
[code:1:b015b747ea]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace GameLoop
{
public class PresciseTimer
{
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("kerne132")]
private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("kerne132")]
private static extern bool QueryPerformanceCounter(ref long PerformanceCount);
long_ticksPerSecond = 0;
long_previousElapsedTime = 0;
public PresciseTimer()
{
QueryPerformanceFrequency(ref_ticksPerSecond);
GetElapsedTime(); //Get rid of first rubbish results
}private void QueryPerformanceFrequency(long ref_ticksPerSecond)
{
throw new NotImplementedException();
}
public double GetElapsedTime()
{
long time = 0;
QueryPerformanceCounter(ref time);
double elapsedTime = (double) (time- _previousElapsedTime) /(double)_ticksPerSecond;
_previousElapsedTime = time;
return elapsedTime;
}public long ref_ticksPerSecond { get; set; }
public long _previousElapsedTime { get; set; }
public double _ticksPerSecond { get; set; }}
}
[/code:1:b015b747ea]If you need to first part of the code which is just the original GameLoop code just ask and i’ll upload it.
-
03/07/2012 at 6:30 pm #48670AnonymousInactive
Could you post the error message(s) that you’re getting too please?
-
03/07/2012 at 6:32 pm #48671AnonymousInactive
If that’s copied verbatim then it might be the spelling of the class PresciseTimer. Also in Fastloop
[code:1:f30b45c693]PreciseTimer_Timer = new PreciseTimer()[/code:1:f30b45c693]
Should be
[code:1:f30b45c693]PreciseTimer _Timer = new PreciseTimer()[/code:1:f30b45c693]
-
04/07/2012 at 9:48 am #48674AnonymousInactive
You’ve also got typos:
[code:1:34546c0fab]public class PresciseTimer [/code:1:34546c0fab]but you are trying to reference
[code:1:34546c0fab]PreciseTimer_Timer = new PreciseTimer(); [/code:1:34546c0fab]Spelling inconsistency of Precise vs Prescise
-
04/07/2012 at 2:26 pm #48676AnonymousInactive
Could you post the error message(s) that you’re getting too please?[/quote:c590f2f2d4]
Theyre 9 errors altogether in the two codes.
Ill start with the FastLoop:1. The type or namespace name ‘PreciseTimer_Timer’ could not be found(are you missing a using directive or assembly reference?)
2. Invalid token ‘=’ in class, struct, or interface member declaration
3. Method must have a return type.
Thats for this part of the code [code:1:c590f2f2d4]PreciseTimer()[/code:1:c590f2f2d4]
4. Invalid Token ‘;’in class, struct, or interface member declaration
[code:1:c590f2f2d4]LoopCallback_callback;[/code:1:c590f2f2d4]
5. The name ‘_timer’ does not exist in current context
[code:1:c590f2f2d4]_callback(_timer.GetElapsedTime())[/code:1:c590f2f2d4]Now onto the PreciseTimer:
1. The type or namespace name ‘long_ticksPerSecond’ could not be found(are you missing a using directive or an assembly reference?)
2. Invalid token ‘=’in class, struct, or interface member declaration
3. The type or namespace name ‘long_previousElapsedTime’ could not be found(are you missing a using directive or an assembly reference?)
4. Invalid token ‘=’ in class, struct, or interface member declarationHopefully this will help. I changed some of the spelling mistakes in the code by the way.
-
04/07/2012 at 5:51 pm #48679AnonymousInactive
Could you post the error message(s) that you’re getting too please?[/quote:9862d7ca01]
Theyre 9 errors altogether in the two codes.
Ill start with the FastLoop:1. The type or namespace name ‘PreciseTimer_Timer’ could not be found(are you missing a using directive or assembly reference?)
2. Invalid token ‘=’ in class, struct, or interface member declaration
3. Method must have a return type.
Thats for this part of the code [code:1:9862d7ca01]PreciseTimer()[/code:1:9862d7ca01]
4. Invalid Token ‘;’in class, struct, or interface member declaration
[code:1:9862d7ca01]LoopCallback_callback;[/code:1:9862d7ca01]
5. The name ‘_timer’ does not exist in current context
[code:1:9862d7ca01]_callback(_timer.GetElapsedTime())[/code:1:9862d7ca01]Now onto the PreciseTimer:
1. The type or namespace name ‘long_ticksPerSecond’ could not be found(are you missing a using directive or an assembly reference?)
2. Invalid token ‘=’in class, struct, or interface member declaration
3. The type or namespace name ‘long_previousElapsedTime’ could not be found(are you missing a using directive or an assembly reference?)
4. Invalid token ‘=’ in class, struct, or interface member declarationHopefully this will help. I changed some of the spelling mistakes in the code by the way.[/quote:9862d7ca01]
As the 2 lads pointed out above:
[code:1:9862d7ca01]PreciseTimer_Timer[/code:1:9862d7ca01]EDIT Based on this error
[code:1:9862d7ca01]_callback(_timer.GetElapsedTime())[/code:1:9862d7ca01]Should be:
[code:1:9862d7ca01]PreciseTimer _timer[/code:1:9862d7ca01]
This is the reason you are getting this error message:
[code:1:9862d7ca01]The type or namespace name ‘PreciseTimer_Timer’ could not be found(are you missing a using directive or assembly reference?) [/code:1:9862d7ca01].
What’s happening is the compiler is going through the source file and is not able to find out what to do with this line of code – it can’t find a type (a class) with the information on how to create an object of PreciseTimer_Timer basically.
The knock-on effect of that is this error:
[code:1:9862d7ca01]Invalid token ‘=’ in class, struct, or interface member declaration[/code:1:9862d7ca01]
Most of those other error messages look like the result of typos in the code too and you’re getting the same error messages for the same reason.
I would imagine this:
[code:1:9862d7ca01]LoopCallback_callback;[/code:1:9862d7ca01]Should be:
[code:1:9862d7ca01]LoopCallback _callback;[/code:1:9862d7ca01]This:
[code:1:9862d7ca01]long_ticksPerSecond;[/code:1:9862d7ca01]Should be:
[code:1:9862d7ca01]long _ticksPerSecond;[/code:1:9862d7ca01]This:
[code:1:9862d7ca01]long_previousElapsedTime;[/code:1:9862d7ca01]Should be:
[code:1:9862d7ca01]long _previousElapsedTime;[/code:1:9862d7ca01]Have a lash at those and see how you get on.
-
04/07/2012 at 11:42 pm #48680AnonymousInactive
As the 2 lads pointed out above:
[code:1:d97b4417cd]PreciseTimer_Timer[/code:1:d97b4417cd]EDIT Based on this error
[code:1:d97b4417cd]_callback(_timer.GetElapsedTime())[/code:1:d97b4417cd]Should be:
[code:1:d97b4417cd]PreciseTimer _timer[/code:1:d97b4417cd]
This is the reason you are getting this error message:
[code:1:d97b4417cd]The type or namespace name ‘PreciseTimer_Timer’ could not be found(are you missing a using directive or assembly reference?) [/code:1:d97b4417cd].
What’s happening is the compiler is going through the source file and is not able to find out what to do with this line of code – it can’t find a type (a class) with the information on how to create an object of PreciseTimer_Timer basically.
The knock-on effect of that is this error:
[code:1:d97b4417cd]Invalid token ‘=’ in class, struct, or interface member declaration[/code:1:d97b4417cd]
Most of those other error messages look like the result of typos in the code too and you’re getting the same error messages for the same reason.
I would imagine this:
[code:1:d97b4417cd]LoopCallback_callback;[/code:1:d97b4417cd]
Should be:
[code:1:d97b4417cd]LoopCallback _callback;[/code:1:d97b4417cd]
This:
[code:1:d97b4417cd]long_ticksPerSecond;[/code:1:d97b4417cd]
Should be:
[code:1:d97b4417cd]long _ticksPerSecond;[/code:1:d97b4417cd]
This:
[code:1:d97b4417cd]long_previousElapsedTime;[/code:1:d97b4417cd]
Should be:
[code:1:d97b4417cd]long _previousElapsedTime;[/code:1:d97b4417cd]Have a lash at those and see how you get on.[/quote:d97b4417cd]
I’ve changed every thing you said to change and a lot of the errors are gone but theres still a few still there.
On the FastLoop program:
1. [code:1:d97b4417cd]_callback = callback;[/code:1:d97b4417cd]
(This member is defined more then once) Error: Ambiguity between ‘GameLoop.FastLoop._callback’ and ‘GameLoop.FastLoop._callback’2. [code:1:d97b4417cd] PreciseTimer _timer;[/code:1:d97b4417cd]
The variable’_timer’ is declared but never used
(This isnt an error but a warning so I dont know if it actually effects the overall running of the program)3. [code:1:d97b4417cd]public LoopCallback _callback { get; set; }[/code:1:d97b4417cd]
Error is on the [code:1:d97b4417cd]_callback[/code:1:d97b4417cd]
The type ‘GameLoop.FastLoop’ already contains a definition for ‘_callback’Now the PreciseTimer:
1. [code:1:d97b4417cd](time_previousElapsedTime)[/code:1:d97b4417cd]
The name ‘timer_previousElapsedTime’ does not exist in the current context2. [code:1:d97b4417cd]/(double) _ticksPerSecond;[/code:1:d97b4417cd]
(This member is defined more then once) Error: Ambiguity between ‘GameLoop.PreciseTimer._ticksPerSecond’ and ‘GameLoop.PreciseTimer._ticksPerSecond’3. [code:1:d97b4417cd]_previousElapsedTime = time;[/code:1:d97b4417cd]
The name ‘_PreviousElapsedTime’ does not exist in the current context4. [code:1:d97b4417cd]public long _previousElapsedTime { get; set; }[/code:1:d97b4417cd]
The Type ‘GameLoop.PreciseTimer’ already contains a definition for ‘_previousElapsedTimer’5. [code:1:d97b4417cd]public double _ticksPerSecond { get; set; }}[/code:1:d97b4417cd]
The Type ‘GameLoop.PreciseTimer’ already contains a definition for ‘_ticksPerSecond’Sorry for all of this, I’ve just been doing C# programming for the best part of a week and im probably making a lot of rookie mistakes.
-
05/07/2012 at 5:18 am #48681AnonymousInactive
On the FastLoop program:
1. Code:
_callback = callback;(This member is defined more then once) Error: Ambiguity between ‘GameLoop.FastLoop._callback’ and ‘GameLoop.FastLoop._callback
‘[/quote:d9f158336b]
This is because the code contains:
[code:1:d9f158336b]
public class FastLoop
{
…
LoopCallback _callback;
…
[/code:1:d9f158336b]and
[code:1:d9f158336b]
public LoopCallback _callback { get; set; }
[/code:1:d9f158336b]These are both technically member variables of the FastLoop class but the second one is called a Property in C# and you use it as a public way for setting / getting the value of a private variable. So the compiler sees these as the same thing basically as they both have the same names. Try to re-write them in this way:
[code:1:d9f158336b]
private LoopCallback _callback;public LoopCallback Callback {
get
{
return _callback;
};set
{
_callback = value;
};
}
[/code:1:d9f158336b]A better explanation of what properties are is here CSharp Station.
Recompile your code after making that change, and anywhere you get an error about _callback, change that line of code to Callback.
These 2 errors are caused for the same reason as the one above and you can fix them in the same way.
[code:1:d9f158336b]
4. Code:
public long _previousElapsedTime { get; set; }The Type ‘GameLoop.PreciseTimer’ already contains a definition for ‘_previousElapsedTimer’
5. Code:
public double _ticksPerSecond { get; set; }}The Type ‘GameLoop.PreciseTimer’ already contains a definition for ‘_ticksPerSecond'[/code:1:d9f158336b]
I’m not sure about this one is but I’ll keep thinking about it. Should it be time – previousElapsedTime? The error suggests that the compiler can’t find what time_previousElapsedTime refers to.
[code:1:d9f158336b]
1. Code:
(time_previousElapsedTime)The name ‘timer_previousElapsedTime’ does not exist in the current context
[/code:1:d9f158336b]Sorry for all of this, I’ve just been doing C# programming for the best part of a week and im probably making a lot of rookie mistakes.[/quote:d9f158336b]
No problem, I remember all the times I pulled my hair out when trying to figure out errors in the code in programming books when I was first starting too.
If you get any more errors, post them here.
What I would strongly recommend is that if there is a way for you to provide feedback to the publisher or the author himself about these errors you’re finding you should really do it. Some authors / publishers will create an errata section on their site with fixes for these mistakes – at least the good ones should!
-
05/07/2012 at 2:17 pm #48682AnonymousInactive
If you get any more errors, post them here.
[/quote:0ea1676e7f]This hopefully will be the last error il have to post.
[code:1:0ea1676e7f]-previousElapsedTime = time;[/code:1:0ea1676e7f]
long PreciseTimer.previousElapsedTimer
Error:
The left-hand side of an assignment must be a variable, property or indexerHopefully youl be able to help because this amount of errors is frustrating me. Can you explain to me what a Stub is because its been coming up quite a lot and it seems to be fixing some of the code?
What I would strongly recommend is that if there is a way for you to provide feedback to the publisher or the author himself about these errors you’re finding you should really do it. Some authors / publishers will create an errata section on their site with fixes for these mistakes – at least the good ones should![/quote:0ea1676e7f]
Ill try to find the authors/ publishers website and repeat these errors.
Thanks a lot for all the help. -
05/07/2012 at 3:39 pm #48683AnonymousInactive
Try and change that to
[code:1:ea260167d1]_previousElapsedTime = time;[/code:1:ea260167d1]
Prefixing a variable with a – turns it into an expression, and you can’t assign a value to an expression.
-
06/07/2012 at 11:50 am #48686AnonymousInactive
Thanks everyone for your help
-
11/07/2012 at 11:37 am #48699AnonymousInactive
If you get any more errors, post them here.
[/quote:a79b389761]
Sorry to bother you again but i was wondering if you could help me out again? Im working on a program that will show a face on a screen but i came to an error.
This is the code for one of the classes that is effected but the error.
[code:1:a79b389761]
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;
using Tao.OpenGl;
using Tao.DevIl;namespace IGameObject
{
public partial class Form1 : Form
{
FastLoop _fastLoop;
bool _fullscreen = false;
StateSystem _system = new StateSystem();
TextureManager _textureManager = new TextureManager();
public Form1()
{
_fastLoop = new FastLoop(GameLoop);InitializeComponent();
_openGLControl.InitializeContexts();// Initialize DevIL
Il.ilInit();
Ilu.iluInit();
Ilut.ilutInit();
Ilut.ilutRenderer(Ilut.ILUT_OPENGL);// Load textures
_textureManager.LoadTexture("face", "face.tif");
_textureManager.LoadTexture("face_alpha", "face_alpha.tif");// Initialize states
_system.AddState("splash", new SplashScreenState(_system));
_system.AddState("title_menu", new TitleMenuState());
_system.AddState("sprite_test", new DrawSpriteState(_textureManager));// Select the start state
_system.ChangeState("sprite_test");if (_fullscreen)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}
else
{
ClientSize = new Size(1280, 720);
}}
void GameLoop(double elapsedTime)
{
_system.Update(elapsedTime);
_system.Render();
_openGLControl.Refresh();}
protected override void OnClientSizeChanged(EventArgs e)
{
base.OnClientSizeChanged(e);
Gl.glViewport(0, 0, this.ClientSize.Width, this.ClientSize.Height);
Setup2DGraphics(ClientSize.Width, ClientSize.Height);
}private void Setup2DGraphics(double width, double height)
{
double halfWidth = width / 2;
double halfHeight = height / 2;
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Gl.glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -100, 100);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();}
private void _openGLControl_Load(object sender, EventArgs e)
{}
private void _openGLControl_Load_1(object sender, EventArgs e)
{}
}
}
[/code:1:a79b389761]
The error is ‘IGameObject.DrawSpriteState’ does not contain a constructor that takes 1 arguments
and it effects this line of code [code:1:a79b389761] _system.AddState("sprite_test", new DrawSpriteState(_textureManager)); [/code:1:a79b389761]Hopefully youl be able to show me where i went wrong?
-
11/07/2012 at 8:51 pm #48702AnonymousInactive
…
The error is ‘IGameObject.DrawSpriteState’ does not contain a constructor that takes 1 arguments and it effects this line of code:
[code:1:ab89faf861] _system.AddState("sprite_test", new DrawSpriteState(_textureManager));[/code:1:ab89faf861]
[/quote:ab89faf861]Okay, so what that error means is that the code is incorrectly trying to create a new instance of the DrawSpriteState() object. And why it is incorrect is because the code for the DrawSpriteState() constructor either takes 0 arguments or takes more than 1 argument.
Without me being able to see the code for the DrawSpriteState() object I’m afraid I can’t tell you what the correct line of code would be. However, there is a quick way for you to find out:
Right click on the DrawSpriteState in
[code:1:ab89faf861] _system.AddState("sprite_test", new DrawSpriteState(_textureManager));[/code:1:ab89faf861]And then select the "Go To Definition" option as in the example picture below.
-
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.