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
Solved

null value question

Posted on 2003-12-09
6
493 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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