Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some help with a simple program 3

Status
Not open for further replies.

MichealC4

Programmer
Jun 26, 2003
457
Hello people. I hope someone can answer my questions.

I'm writing a simple program that right now pulls from a text file and displays it in a textbox. The user then clicks a button after a few seconds saying they agree to the above text. That works great, no problem there. But because I've only been using C# for my second day now, I'm a bit of a greenie to it. I do however have experience in other languages.

So, here's what I am looking to do.

Parameters for the following:
- Choose file name
- Choose timelimit for the button to display
- Choose whether a name field will be on or off

Details:
File name will be in the same directory as the excutable which will be called in a login script from Active Directory. Should be no problem there.
The timelimit shouldn't be a problem either. I just want to call the program (which is GUI, by the way), set a timelimit based on who is viewing the agreement, and off we go.
The name field carries a bit with it. The purpose is knowing who accepted the agreement when. Of course, this will have to write to a file on the network the time, the name, and the computer id.

The problem I am having is getting the switch to handle the parameters. So far, it looks like C# does parameters a bit differently. Maybe I'm just imagining things. Here's what it'd look like:

myexe.exe -file=abc.txt -time=6 -name=on
myexe.exe -file=xyz.txt -time=3 -name=off
myexe.exe -file=tuv.txt -time=0 -name=on

So, even if the time is disabled, the name may still have to be entered. Also, I need it to validate the name in realtime. I don't want them to get around it by using spaces or some such, they need to format their name like John, Doe or John,Doe or something to that effect. There are a few other things that need to be done with the program such as popping up another window for the user to logoff after they are done. But that'll come later. Thanks!

----------------------------
"Will work for bandwidth" - Thinkgeek T-shirt
 
You can use this example:
Code:
static void Main(string[] args)
{
        string fname=null;
        int time=-1;
        string name=null;
	Process p = Process.GetCurrentProcess();
	string appName = p.ProcessName;
	string appVersion = p.MainModule.FileVersionInfo.ProductVersion;
	string appPath = Path.GetDirectoryName(p.MainModule.FileName);
	if (ValidateArguments(args) )
	{
	  
	 //
	}
	else
	{
	  //
	}
}
private bool ValidateArguments(string [] args)
{
        bool hasArg = false;
	foreach(string arg in args)
	{
	        hasArg = true;
		string [] keyval = arg.ToLower().Split('=');
                switch (keyval[0])
                {
                  case "-faname":
                       if (keyval[1]!=null)
                       {
                          fname=appPath+Path.PathSeparator + keyval[1];
                       }
                  break;
                  case "-time":
                   if (keyval[1]!=null)
		       {
		            time=Convert.ToInt32(keyval[1]);
                       }
                  break;
                  case "-name"
                     if (keyval[1]!=null)
		     	{
		     	    name =keyval[1];
                       }
                  break;
                }
	}

}
obislavu
 
Thanks!

Now I'm having some trouble, when I go to design view, I get this: line 29 col 24: ident expected

The code in question (starting at line 29) is this:
Code:
public MainForm(fname, time, name)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			// Read the ethics file
			// Shamelessly borrowed from Microsoft
			// [URL unfurl="true"]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiostreamreaderclassreadtoendtopic.asp[/URL]
			using (StreamReader sr = new StreamReader(fname)) 
            {
                agree_body.Text = sr.ReadToEnd();
            }
			
			agree_time.Interval = time;
			agree_time.Enabled = true;

----------------------------
"Will work for bandwidth" - Thinkgeek T-shirt
 
StreamReader is in the System.IO namespace - do you have a using statement for it at the top of your code?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Yes, I do. I didn't have a problem until I tried to pass the arguments to MainForm, so my guess is I screwed up something there. Google was of no help.

----------------------------
"Will work for bandwidth" - Thinkgeek T-shirt
 
you need to declare the argument type

ie.

public void MainForm(string fname, string time, string name)
 
That worked! Now when I try to build it, I get these errors:

e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(70,7): error CS0246: The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)
e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(71,24): error CS0246: The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?)
e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(72,27): error CS0246: The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?)
e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(73,46): error CS0246: The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?)
e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(75,11): error CS0120: An object reference is required for the nonstatic field, method, or property 'userethics.MainForm.ValidateArguments(string[])'
e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(207,27): error CS0103: The name 'fname' does not exist in the class or namespace 'userethics.MainForm'
e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(214,23): error CS0103: The name 'time' does not exist in the class or namespace 'userethics.MainForm'
e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(215,23): error CS0103: The name 'time' does not exist in the class or namespace 'userethics.MainForm'
e:\Documents and Settings\Micheal\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(222,24): error CS0103: The name 'name' does not exist in the class or namespace 'userethics.MainForm'

Build complete -- 9 errors, 0 warnings

Any ideas?

----------------------------
"Will work for bandwidth" - Thinkgeek T-shirt
 
The Process class is in the System.Diagnostics class.

Some of the remaining errors will be cleared up by adding that using statement.

In order to save some time, you can type "C# Error" and the error code into Google's Microsoft page , and you'll get some good hits as to how to fix the problem.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the help so far. I've gotten it down to just 2 errors during compile now.

------ Build started: Project: userethics Configuration: Debug ------
Performing main compilation...


c:\Documents and Settings\svcottm\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(91,11): error CS0120: An object reference is required for the nonstatic field, method, or property 'userethics.MainForm.ValidateArguments(string[])'
c:\Documents and Settings\svcottm\My Documents\SharpDevelop Projects\userethics\userethics\MainForm.cs(231,16): error CS0161: 'userethics.MainForm.ValidateArguments(string[])': not all code paths return a value

Build complete -- 2 errors, 0 warnings

Here's lines 91-95:
Code:
if (ValidateArguments(args))
    		{      
     			// run the program passing the arguments
     			Application.Run(new MainForm(fname, time, name));
    		}

Here's lines 231-235:
Code:
private bool ValidateArguments(string[] args)
		{
        	bool hasArg = false;
    
        	foreach(string arg in args)

I've searched on with nothing helpful.

----------------------------
"Will work for bandwidth" - Thinkgeek T-shirt
 
MainForm.cs(91,11)
The numbers are the line & column of where it thinks the error is. In this case, it's the call to ValidateArguments.

Unless ValidateArguments is in the same class, you'll need to tell it which instance of which class to use. In other words, preface it with a variable which is the name of the class where the method is located. Something like:
Code:
if (!SomeInstanceVariable.ValidateArguments(args))
{
  // Do something because it returned false
}

MainForm.cs(231,16): error CS0161: 'userethics.MainForm.ValidateArguments(string[])': not all code paths return a value
Exactly what it says it is. You are returning from the method, without assigning a value to the return value of the method.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I think I understand the concept of line numbers. ;) Otherwise I wouldn't have pasted the lines I did ... As for the first error, thanks, I'll look in to that. As for the second error, I think I know how to read as well. ;) However, I need to know how to fix it, not what it means. And google didn't turn up much either.

Anybody else?

----------------------------
"Will work for bandwidth" - Thinkgeek T-shirt
 
TechieMichael, no need to get snippy. Chip told you exactly what you need to do to fix the problem. Make sure all exits from the ValidateArguments method return a value.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

I was not born cynical - I earned my cynicism through careful observation of the world around me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top