Solved

null value question

Posted on 2003-12-09
6
492 Views
Last Modified: 2010-04-16
i have a function like so:

public void myFunction(ref ProgressBar progBar)
{
  ...
}

i want the progBar argument to be optional, and then be able to test to see if they want to use a progBar or not inside the function, so i tried something like:

public void myFunction()
{
  myFunction(null);
}

public void myFunction(ref ProgressBar progBar)
{
  if (progBar != null)
  {
    ...
  }
  else
  {
    ...
  }
}

i know this sort of thing would have worked in C++, but this doesn't compile in C#, saying the best overloaded method doesn't work with the null value, cannot convert from ProgressBar to null.  what is the best way to acheive what I'm trying to do?

thanks,

justin
0
Comment
Question by:newstandard
6 Comments
 
LVL 7

Expert Comment

by:jj819430
ID: 9905346
yeah, C-sharp is different from C++ in that way. But another way to do it, if I understand you correctly, is to use a boolean value.

I think you are saying if they want a progress bar.

so set
bool progBar = false; //as a start. You can set to true if you want the default to have the bar.

public void myFunction(ref ProgressBar progBar)
{
  if (progBar)
  {
    ...
  }
  else
  {
    ...
  }
}
0
 
LVL 3

Accepted Solution

by:
magnus23 earned 250 total points
ID: 9905683
HI newstandard !
Following code works perfectly in C#, all you have to do is add ref when parameter in a function call. This will not work if you pass null itself, but with object that is set to null it works fine.

private void Form1_Load(object sender, System.EventArgs e)
{
    ProgressBar pr=null;
    myFunction(ref pr);
}
public void myFunction( ref ProgressBar progBar)
{
      
      if (progBar != null)
      {
            MessageBox.Show("object is not null");
      }
      else
      {
            MessageBox.Show("object is  null");
      }
}

Good Luck!
0
 

Author Comment

by:newstandard
ID: 9905959
thanks
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Expert Comment

by:razziel
ID: 9908767
Ok, your doing something wrong because c# does support overloading.

Theres an error in your code, i didnt test but im positive thats your problem. ProgressBar is a reference-type, so you dont need the ref keyword, since what gets passed is a reference itself.
Try it, im sure itll compile

Hope it helps
0
 

Expert Comment

by:razziel
ID: 9908839
just loose the ref keyword from your method and you can then call myFunction(null) directly

btw, there should be an edit option after submiting :)
0
 

Author Comment

by:newstandard
ID: 9911983
interesting - thanks for the tip
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question