8 Replies - 1469 Views - Last Post: 10 January 2010 - 01:38 PM Rate Topic: -----

#1 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

C# How Do I Use Voids?

Posted 09 January 2010 - 09:00 PM

How do I use voids? coz I this code that i want it to activate when I press it but senice its a void I don't know what to do. Heres the code
		void RefreshAll(TabControl tabControl)
		{
			foreach (TabPage tp in tabControl.TabPages)
			{
				foreach (Control c in tp.Controls)
				{
					if (c.GetType().ToString() == "System.Windows.Forms.WebBrowser")
					{
						WebBrowser wb = (WebBrowser)c;
						wb.Navigate(textBox1.Text);
					}
				}
			}
		}


Is This A Good Question/Topic? 0
  • +

Replies To: C# How Do I Use Voids?

#2 zombie_chan51   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 16-March 08

Re: C# How Do I Use Voids?

Posted 09 January 2010 - 10:12 PM

void just mean the method won't return anything.

to use this just call the method.
Was This Post Helpful? 0
  • +
  • -

#3 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

Re: C# How Do I Use Voids?

Posted 10 January 2010 - 12:41 AM

View Postdrjoe51, on 9 Jan, 2010 - 09:12 PM, said:

void just mean the method won't return anything.

to use this just call the method.


I have no Idea what your talking about
Was This Post Helpful? 0
  • +
  • -

#4 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: C# How Do I Use Voids?

Posted 10 January 2010 - 01:02 AM

Void is a Key-Word of c/C++ and C# (dont exactly know about Java Though). Used to specify the return type of any function/Method/Sub-routine. all the functions or methods must return some value to the calling statement. For that You have to specify the type of Value to be returned(example int, string, char or even class etc).. But If you dont want to return any value you must use void as Return Type so that You may not use any return statement.
Was This Post Helpful? 0
  • +
  • -

#5 Core   User is offline

  • using System.Linq;
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Re: C# How Do I Use Voids?

Posted 10 January 2010 - 02:10 AM

There are functions that return a value and those that do not return a value. The void keyword means that the function won't return a value, therefore it will only execute a set of actions specified inside of it. A function with a return type (for example, int) will return a value and should be assigned to a variable to get the result.

To show this to you on an example, take a look at this. Here is a function that does not return a value:

void DoSomething()
{
	 int x = 4;
	 int y = 5;
	 int z = x * y;
}



This function will multiply the two values and this is going to be it. You could call this function from your program through DoSomething();. What if the developer wanted to get the obtained value? Then a function with a return type is used.

int DoSomething()
{
	 int x = 4;
	 int y = 5;
	 int z = x * y;

	 return z;
}



Now as you see, there is no void keyword and the function has an int return type. Also, there is the return keyword that shows what value is going to be returned. This time, to get the value from this function, you will need to assign it, so you would use something like int price = DoSomething();.
Was This Post Helpful? 0
  • +
  • -

#6 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

Re: C# How Do I Use Voids?

Posted 10 January 2010 - 02:19 AM

View Postrocky_upadhaya, on 10 Jan, 2010 - 12:02 AM, said:

Void is a Key-Word of c/C++ and C# (dont exactly know about Java Though). Used to specify the return type of any function/Method/Sub-routine. all the functions or methods must return some value to the calling statement. For that You have to specify the type of Value to be returned(example int, string, char or even class etc).. But If you dont want to return any value you must use void as Return Type so that You may not use any return statement.


And How am I suppost to turn that into a int? It wont let me int those type of things
Was This Post Helpful? 0
  • +
  • -

#7 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: C# How Do I Use Voids?

Posted 10 January 2010 - 03:45 AM

public void Main()
{
   //do something here
   //you cannot return anything here.
}

private int add()
{
	//You Must some Integer value here.
   /* like*/   return 7;
   //you can return 0 if you dont want to return something significant.
  //you cannot return 7.7; or return "hello "; here
}

public string name()
{
	 return "hello"; //this is possible
}

public float num()
{
		 return 7.7;	// this is also possible
}


This post has been edited by rocky_upadhaya: 10 January 2010 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 finaiized   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Re: C# How Do I Use Voids?

Posted 10 January 2010 - 11:05 AM

Just remember void doesn't return something, and if you add a return type in front of it instead (like string Hello()) then it does.
Use void if you just want to run something without wanting the result to it (for example, maybe creating a new window) or use a return type if you want the result (usually used with overloads).

Here's an overloaded example with the use of return:
int addThis(int i, int j) {
int k=i+j;
return k;
}


When calling it, I'd use:
addThis(3,4);


I would get the value of k returned. This would be useful if I wanted to do this:
int ki = addThis(3, 4);


See how I assigned the return value 'k' to int ki?

This post has been edited by finaiized: 10 January 2010 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#9 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: C# How Do I Use Voids?

Posted 10 January 2010 - 01:38 PM

If you want to use void as your return type and still get something out of the method, you can do it in two ways. The first way is to pass an object in.

public class Totals
{
	 public int points
	{ get; set;
}



Then, you pass it in and it will be modified.

public class Program
{
	public static void Main()
	{
		Totals myTotal = new Totals()
		UpdateTotals(myTotal);
	}

	private void UpdateTotals(Totals total)
	{
		points += 10;
	}
}



The second way is for value types. In such a case, you decide whether the value has been set or not by the time you pass it in. If it hasn't been set, use out. Otherwise use ref.

public class Program
{
	public static void Main()
	{
		 int myTotal; // notice it's not assigned
		 UpdateTotals(out myTotal);
	}

	private void UpdateTotals(out int total)
	{
		total += 10;
	}
}



Both ways do not return anything. Is this what you mean?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1