?
Solved

null value question

Posted on 2003-12-09
6
Medium Priority
?
507 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 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