11 Replies - 2047 Views - Last Post: 01 February 2010 - 06:33 PM Rate Topic: -----

#1 Dean90  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-February 10

How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 01:34 PM

Hi everyone, this is my first post here and I'm also new to this forum. I've searched on google for about 2 hours now with no results :(

Basicly what I am trying to do is, I have several methods in my c# source such as

public static void jump(int x, int y)
{
blablablbalbla
blabla
}



So my question is, can I make an .Ini file or a .txt file and in this text file type something like

jump(110,220)
jump(120,230)
jump(130,240)

and then make my C# application, read the .ini/.txt and execute/call these methods?

I appreciate any help :)

Is This A Good Question/Topic? 0
  • +

Replies To: How do I read methods in .ini/.txt files and execute them?

#2 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 01:38 PM

OK. This is tricky to answer because yes, you can. However, it's a bad approach.

What you should do instead is read the INI file and parse it. When you find a string that should invoke a method, then just call the method in code.
Was This Post Helpful? 0
  • +
  • -

#3 Sailing_Nut  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 29-January 10

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 01:44 PM

There are several ways you can handle this. I'll start from the easies but least flexible way to more flexible and more difficult.

First off, if you only need to call the jump method, you can just put a list of value pairs into a text file and parse them out. (I can explain a method to do this if you want.) You can then just call the jump method with the values. This is easy because you know that you're only going to be calling jump()

If you have one of several methods that you need to call, you san just read the text representation of the method to call and use a switch statement to call the proper method. For example, if you have methods jump, move, and slide, you could wtire code something like this:

switch(methodName)
{
	case"jump":
		jump(x,y);
		break;
	case"move":
		move(x,y);
		break;
	case"slide":
		slide(x,y);
		break;
}



That way is a bit bture force, but fairly easy to implement. (Again parsing code was omitted.)

Now, for the big bang, if you need to arbitrarilly call any method without proir knowledge, you'll have to use reflection to dynamically look up method names and execute them on an object based on the text. I really don't have any experience here short of knowing that reflection is what you need to use.

Good luck!
Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 01:44 PM

MentalFloss has got the point... Your ini file should look something like this:

x=110;y=220
x=120;y=230
x=130;y=240

or like this:

110;220
120;230
130;240

then read the file line by line, split every line into two pieces using the ; delimiter, then parse each value into a proper type and pass the parameters to a jump method.
Was This Post Helpful? 0
  • +
  • -

#5 Dean90  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-February 10

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 01:55 PM

View PostFlashM, on 1 Feb, 2010 - 12:44 PM, said:

MentalFloss has got the point... Your ini file should look something like this:

x=110;y=220
x=120;y=230
x=130;y=240

or like this:

110;220
120;230
130;240

then read the file line by line, split every line into two pieces using the ; delimiter, then parse each value into a proper type and pass the parameters to a jump method.


This would work if I were to only use the jump method, but I want to be able to read and execute different methods from the .ini file.

More like this

jump(110,220)
jump(120,230)
jump(130,240)
Eatpotatoes
TalkToMan(5)
Wash

The code is totaly random but you get my concept :)
My idea is more like making the Ini file into a script that would then get executed by my C# application.

Thanks alot guys for the help !

View PostSailing_Nut, on 1 Feb, 2010 - 12:44 PM, said:

There are several ways you can handle this. I'll start from the easies but least flexible way to more flexible and more difficult.

First off, if you only need to call the jump method, you can just put a list of value pairs into a text file and parse them out. (I can explain a method to do this if you want.) You can then just call the jump method with the values. This is easy because you know that you're only going to be calling jump()

If you have one of several methods that you need to call, you san just read the text representation of the method to call and use a switch statement to call the proper method. For example, if you have methods jump, move, and slide, you could wtire code something like this:

switch(methodName)
{
	case"jump":
		jump(x,y);
		break;
	case"move":
		move(x,y);
		break;
	case"slide":
		slide(x,y);
		break;
}



That way is a bit bture force, but fairly easy to implement. (Again parsing code was omitted.)

Now, for the big bang, if you need to arbitrarilly call any method without proir knowledge, you'll have to use reflection to dynamically look up method names and execute them on an object based on the text. I really don't have any experience here short of knowing that reflection is what you need to use.

Good luck!


Yea I have also thought about the switch statement method but it would be too ineffective and slow since I am thinking about endless amounts of methods. I am searching for some professional effective way :P It's pretty much out of my league and that is why I am here asking for help :)

This post has been edited by Dean90: 01 February 2010 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 01:59 PM

Consider using an XML structured file to describe all the thing that need to be done.
Was This Post Helpful? 0
  • +
  • -

#7 Dean90  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-February 10

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 02:11 PM

View PostFlashM, on 1 Feb, 2010 - 12:59 PM, said:

Consider using an XML structured file to describe all the thing that need to be done.


There has to be other ways that are smoother then that.
Was This Post Helpful? 0
  • +
  • -

#8 Sailing_Nut  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 29-January 10

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 02:13 PM

Given that you are looking to execute arbitrary methods and in essence create a script of commands to run, you will need to use reflection to be able to call a method based on a text name.

You will need to parse out the method name and then any parameters to be passed to the method.

I agree with the others that said XML would be a better way to go than a flat text or ini file. It would eminate your need to parse because the file would in essence give you the context you desire.

Here's a sample of what an XML file might look like

<MethodList>
	<Method>
		<Name>Jump</Name>
		<Parameters>
			<Parameter>
				<Name>X</Name>
				<Type>int</Type>
				<Value>110</Value>
			</Parameter>
			<Parameter>
				<Name>Y</Name>
				<Type>int</Type>
				<Value>200</Value>
			</Parameter>
		</Parameters>
	</Method>
	<Method>
		<Name>Jump</Name>
		<Parameters>
			<Parameter>
				<Name>X</Name>
				<Type>int</Type>
				<Value>20</Value>
			</Parameter>
			<Parameter>
				<Name>Y</Name>
				<Type>int</Type>
				<Value>67</Value>
			</Parameter>
		</Parameters>
	</Method>
	<Method>
		<Name>Eatpotatoes</Name>
		<Parameters/>
	</Method>
	<Method>
		<Name>TalkToMan</Name>
		<Parameters>
			<Parameter>
				<Name>Time</Name>
				<Type>int</Type>
				<Value>5</Value>
			</Parameter>
		</Parameters>
	</Method>
	<Method>
		<Name>Wash</Name>
		<Parameters/>
	</Method>
</MethodList>



The XML file is quite verbose, but it saves your coding. If you want a more compact file, the coding to parse it will go up.

In the end it's yours to decide the tradeoffs

This post has been edited by Sailing_Nut: 01 February 2010 - 02:14 PM

Was This Post Helpful? 1
  • +
  • -

#9 Dean90  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-February 10

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 02:23 PM

I guess I have to learn how to use reflection, wow this is going to be a pain in the ass haha.

But thanks for the help guys :)

Edit: I'm actually considering XML, I guess I will just have to see :)

This post has been edited by Dean90: 01 February 2010 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 02:41 PM

You will need to do something like this:

class Program
{
	public static void Do(string msg)
	{
		Console.WriteLine("Hello " + msg);
		Console.ReadLine();
	}

	public static void InvokeMethod(string method_name, string msg)
	{
		Type type = typeof(Program);
		MethodInfo method = type.GetMethod(method_name, BindingFlags.Public | BindingFlags.Static);
		method.Invoke(null, new object[] { msg });
	}

	static void Main(string[] args)
	{
		InvokeMethod("Do", "John");
	}
}





Hope this helps...

This post has been edited by FlashM: 01 February 2010 - 02:42 PM

Was This Post Helpful? 1
  • +
  • -

#11 Dean90  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-February 10

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 04:10 PM

View PostFlashM, on 1 Feb, 2010 - 01:41 PM, said:

You will need to do something like this:

class Program
{
	public static void Do(string msg)
	{
		Console.WriteLine("Hello " + msg);
		Console.ReadLine();
	}

	public static void InvokeMethod(string method_name, string msg)
	{
		Type type = typeof(Program);
		MethodInfo method = type.GetMethod(method_name, BindingFlags.Public | BindingFlags.Static);
		method.Invoke(null, new object[] { msg });
	}

	static void Main(string[] args)
	{
		InvokeMethod("Do", "John");
	}
}





Hope this helps...



Thanks that did help alot :)
Was This Post Helpful? 0
  • +
  • -

#12 Dean90  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-February 10

Re: How do I read methods in .ini/.txt files and execute them?

Posted 01 February 2010 - 06:33 PM

Thanks guys, I made the code and it works great :)

This post has been edited by Dean90: 01 February 2010 - 06:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1