Jump to content

i am confused with arguments

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

I am now moving into methods, which now i have got how to do it, but when it comes to ref, out and copy i am a little confused on my little example, i have the code below it should display what ever you input X variable but in the end it comes out to be 56,49 etc which is not right especially when i am typing 1 or something else, what am i not getting or doing wrong? thank you in advance:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{


static void Main()
{
int x;
method1(out x);
Console.Read();

}

static void method1(out int x)
{

Console.WriteLine("Please enter first number");
x = Console.Read();
Console.WriteLine("you entered " + x);
Console.Read();
Console.WriteLine("thank you");
Console.Read();


}
}
}
}

#2
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Ok now i added a second variable and it turned out worse here is the code below : (


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{


static void Main()
{
int a;
int x;

method1(out x, out a);
Console.Read();

}

static void method1(out int x, out int a)
{

Console.WriteLine("Please enter first number");
x = Console.Read();
Console.WriteLine("you entered " + x);
Console.Read();
Console.WriteLine("Please enter second number");
a = Console.Read();
Console.WriteLine("you entered " + a);
Console.WriteLine("thank you");
Console.Read();


}
}
}

#3
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Ok now after further testing using the string variable type instead of int, it worked fine with the method I created but sub from int to string, whats the deal, something is fishy about that and i cant put my finger on it, any help please?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{


static void Main()
{

string name;

method1(out name);
Console.Read();

}

static void method1(out string name)
{
Console.WriteLine("Please enter your first name");
name = Console.ReadLine();
Console.WriteLine("you entered " + name);
Console.Read();


}
}
}

#4
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
The method is working properly but returning the ASCII value of your int (thats the way the Console.Read method works)...4 is 52, 0 is 48, w is 87, etc. If you want the actual character you inputed your best off just using the ReadLine method and converting it into an int;)

#5
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello Gaylo,

Thanks for the input, but i have to ask where is the exact readline, every time i put the x = console.readline it says cannot implicitly convert input string to int. what do i do to resolve this? can you input the correct information in my example to help better my understand?

#6
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
Sorry about that I meant that you should still use the read method but then convert to an int or cast the variable to one. ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{


static void Main()
{
int x;
method1(out x);
char c = (char)x;
Console.WriteLine("You Entered:" + c);
Console.ReadKey();
Console.WriteLine("thank you");
Console.ReadKey();
}

static void method1(out int x)
{

Console.WriteLine("Please enter first number");
x = Console.Read();

}
}
}
I moved a couple of things around but its all the same technically except I cast the variable as an char. You could also use this code (both methods work with char or int if you prefer) but its not as neat and tidy:)
char c = Convert.ToInt16(x);
Hope this clears it up for you

PS...You should use the code tags in your posts. This makes it much easier to read;)

#7
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello Gaylo,

Thanks for the info, so its sorta like converting string to an int example int =int32.parse(string), is that correct? except with char i am using readkey and converting/declaring the int to a char at the top of the code. Also about method, you can insert the method anywhere in the Main method as long as you have the correct spelling of the method correct? for example method1 could go after all the other code like this:

static void Main()
{
int x;
char c = (char)x;
Console.WriteLine("You Entered:" + c);
Console.ReadKey();
Console.WriteLine("thank you");
Console.ReadKey();
method1(out x);
}
or does method have to be on top, and how would you do that if i wanted to move the method1? Also sorry but i forgot how to use code insert, is it [code..] and then the ending of the code [/code...]?

#8
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
You need the method above the casting of the variable because it is an out method so the variable isn't instantiated until the method is run. The order does matter. The code tags are available on the top toolbar under the # symbol. It looked like you have them right anyhow. Casting the variable as a type is just used to perform functions on the variable if it is not the right type while converting the data type will change it for all future uses of the variable.

#9
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Thanks again gaylo565 for helping me understand more on methods, the only question left now is code tags, how does that go again to input your code in forums?

#10
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
just put the code in between the tags and it separates it out for easier reading. Begin with the [code...] tag (minus the ...) and then your code; after that the [/code...] tag to end your code.

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Or, use:
[NOPARSE]
[HIGHLIGHT=csharp]
code here
[/HIGHLIGHT]
[/NOPARSE]
Thıs adds syntax hıghlıghtıng.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums