Solved

null value question

Posted on 2003-12-09
6
491 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Expert Comment

by:razziel
Comment Utility
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
Comment Utility
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
Comment Utility
interesting - thanks for the tip
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now