Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I was debugging a code and I got it to 1 error.
It was missing a main method, so I did static void Main(); in the beginning of the code.
The error says "ffbCS.Program.Main() must declare a body because it is not marked abstract, extern, or partial"
Below is my code
----------------------------------------------------------------------------------

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.ComponentModel;
using System.Device;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;

namespace ffbCS
{
    class Program
    {
        static void Main();



	public sealed class DirectInputWrapper
	{
		#region ForceType
		/// <summary>
		/// This enumeration simply provides a shorthand way to reference
		/// the custom forces we create -- it's not a base part of DirectX,
		/// though, so you can use a completely different method.
		/// </summary>
		public enum ForceType
		{
			VeryBriefJolt,
			BriefJolt,
			LowRumble,
			HardRumble
		}
		#endregion
		
		//this class lets us send FF commands to up to two game pads,
		//but your own class could support as many as you want
		private static Device device1;
		private static Device device2;
		
		private static Dictionary<forcetype,> P1Forces;
		private static Dictionary<forcetype,> P2Forces;
		
		#region Initialize
		/// <summary>
		/// Initialize DirectInput
		/// </summary>
		public static void Initialize( System.Windows.Forms.Control Parent )
		{
			if ( device1 != null )
			{
				device1.Dispose();
				device1 = null;
			}

			if ( device2 != null )
			{
				device2.Dispose();
				device2 = null;
			}

			foreach ( DeviceInstance instance in Manager.GetDevices( DeviceClass.GameControl,
			EnumDevicesFlags.AttachedOnly ) )
			{
				if ( device1 == null )
				device1 = new Device( instance.InstanceGuid );
				else if ( device2 == null )
				device2 = new Device( instance.InstanceGuid );
			}

			DisposeForces();
			P1Forces = new Dictionary<forcetype,>();
			P2Forces = new Dictionary<forcetype,>();
			
			InitializeDevice( Parent, device1 );
			InitializeDevice( Parent, device2 );
		}
		#endregion
		
		#region InitializeDevice
		private static void InitializeDevice( System.Windows.Forms.Control Parent, Device Dev )
		{
			if ( Dev == null )
			return;
	
			Dev.SetDataFormat( DeviceDataFormat.Joystick );
			Dev.SetCooperativeLevel( Parent, CooperativeLevelFlags.Background |
			CooperativeLevelFlags.Exclusive );
			Dev.Properties.AxisModeAbsolute = true;
			Dev.Properties.AutoCenter = false;
			Dev.Acquire();

			int[] axis = null;

			// Enumerate any axes
			foreach ( DeviceObjectInstance doi in Dev.Objects )
			{
				if ( ( doi.ObjectId & (int)DeviceObjectTypeFlags.Axis ) != 0 )
				{
					// We found an axis, set the range to a max of 10,000
					Dev.Properties.SetRange( ParameterHow.ById,
					doi.ObjectId, new InputRange( -5000, 5000 ) );
				}

				int[] temp;
	
				// Get info about first two FF axii on the device
				if ( ( doi.Flags & (int)ObjectInstanceFlags.Actuator ) != 0 )
				{
					if ( axis != null )
					{
						temp = new int[axis.Length + 1];
						axis.CopyTo( temp, 0 );
						axis = temp;
					}
					else
					{
						axis = new int[1];
					}

					// Store the offset of each axis.
					axis[axis.Length - 1] = doi.Offset;
					if ( axis.Length == 2 )
					{
						break;
					}
				}
			}

			Dictionary<forcetype,> forces;
			
			if ( Dev == device1 )
			forces = P1Forces;
			else
			forces = P2Forces;

			try
			{
				if ( axis != null )
				{
					forces.Add( ForceType.VeryBriefJolt,
					InitializeForce( Dev, EffectType.ConstantForce, axis,
					6000, EffectFlags.ObjectOffsets | EffectFlags.Spherical, 150000 ) );
					forces.Add( ForceType.BriefJolt,
					InitializeForce( Dev, EffectType.ConstantForce, axis,
					10000, EffectFlags.ObjectOffsets | EffectFlags.Spherical, 250000 ) );
					forces.Add( ForceType.LowRumble,
					InitializeForce( Dev, EffectType.ConstantForce, axis,
					2000, EffectFlags.ObjectOffsets | EffectFlags.Cartesian, 900000 ) );
					forces.Add( ForceType.HardRumble,
					InitializeForce( Dev, EffectType.ConstantForce, axis,
					10000, EffectFlags.ObjectOffsets | EffectFlags.Spherical, 2000000 ) );
				}
			}
			catch ( Exception e ) 
			{
				System.Windows.Forms.MessageBox.Show( "Could not initalize force feedback:\n\n" + e );
			}
		}
		#endregion
		
		#region DisposeForces
		public static void DisposeForces()
		{
			if ( P1Forces != null )
			{
				foreach ( EffectObject o in P1Forces.Values )
				o.Dispose();
				P1Forces = null;
			}
		
			if ( P2Forces != null )
			{
				foreach ( EffectObject o in P2Forces.Values )
				o.Dispose();
				P2Forces = null;
			}
		}
		
#endregion

#region SendForce
		public static void SendForce( ForceType Type, bool IsPlayer1 )
		{
			Dictionary<forcetype,> forces;
		
			if ( IsPlayer1 )
			forces = P1Forces;
			else
			forces = P2Forces;
			
			if ( forces == null )
			return;
			if ( !forces.ContainsKey( Type ) )
			return;
			
			EffectObject force = forces[Type];
			force.Start( 1 );
		}
#endregion

#region InitializeForce
		public static EffectObject InitializeForce( Device Dev, EffectType Type,
		int[] Axis, int Magnitude, EffectFlags Flags, int Duration )
		{
			EffectObject eo = null;
			Effect e;
			
			foreach ( EffectInformation ei in Dev.GetEffects( EffectType.All ) )
			{
				if ( DInputHelper.GetTypeCode( ei.EffectType ) == (int)Type )
				{
					e = new Effect();
					e.SetDirection( new int[Axis.Length] );
					e.SetAxes( new int[1] ); 
//this is the offending line in the Microsoft examples
//setting axes to 2 causes the dreaded "Value does not fall within expected range" error
//this is evidently a bug in Managed DirectX, at least affecting some game pads,
//and this is the only workaround I've found.
//I have not been able to successfully load FFE files in Managed DirectX
//due to this same problem (presumably, in the inner initialization code
//when loading from FFE, it is trying to load two axes there, as well).

//This problem exists with all verys of Managed DirectX, as far as I can tell,
//at least up through March 2008 when this example was written.

					e.EffectType = Type;
					e.ConditionStruct = new Condition[Axis.Length];
					e.Duration = Duration;
					e.Gain = 10000;
					e.Constant = new ConstantForce();
					e.Constant.Magnitude = Magnitude;
					e.SamplePeriod = 0;
					e.TriggerButton = (int)Microsoft.DirectX.DirectInput.Button.NoTrigger;
					e.TriggerRepeatInterval = (int)DI.Infinite;
					e.Flags = Flags;
					e.UsesEnvelope = false;

// Create the effect, using the passed in guid.
					eo = new EffectObject( ei.EffectGuid, e, Dev );
				}
			}

			return eo;
		}
#endregion
	}
    }
}
Posted
Updated 19-Feb-15 19:08pm
v2

The answer lies in the error. The Main() method doesn't have a body.
A method must have a body unless you have declared this inside an interface or it is marked abstract, extern, or partial.

Try replacing
C#
static void Main();

with
C#
static void Main(){}

Hopefully, this resolves your problem.
If I misunderstood something, please let me know with little more details :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Feb-15 2:10am    
Correct, a 5.
—SA
Suvendu Shekhar Giri 20-Feb-15 2:22am    
Thanks :)
TheRealSteveJudge 20-Feb-15 3:11am    
5*
Suvendu Shekhar Giri 20-Feb-15 3:17am    
Thanks :)
Main() is the starting point of program.. and all methods or references should come inside main() i.e this should be your code..
C#
namespace ffbCS
{
    class Program
    {
        static void Main()
        {
               //All Methods and References comes here
        }
     }
}
 
Share this answer
 
Comments
TheRealSteveJudge 20-Feb-15 3:13am    
A method cannot contain other methods. 1*
Bangsang 22-Feb-15 14:26pm    
I did
Public static void Main()
{}
and it works. I thought I have to put all the codes inside the {}? You don't have to do that?
manak chand 23-Feb-15 3:35am    
That is right.. but you need to call other method via this .. in case of console application... but I think issue was in Main method declaration.. Thanks..
This is what you should do
C#
class Program
{
   public static void Main()
   {
          DirectInputWrapper inst = new DirectInputWrapper();
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900