Solved

null value question

Posted on 2003-12-09
6
494 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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