Welcome to Dream.In.Code
Become a C# Expert!

Join 150,407 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 948 people online right now. Registration is fast and FREE... Join Now!




Function: Returning a blank textbox

 
Reply to this topicStart new topic

Function: Returning a blank textbox

Soyaku
18 Mar, 2008 - 02:48 PM
Post #1

New D.I.C Head
*

Joined: 28 Feb, 2008
Posts: 42


My Contributions
CODE
        //Commission method
        private double comMethod(double sales)
        {
            double commission;

            //If statement for commission when greater or equal than quota
            if (sales >= quota)
            {
                commission = comRate * sales;
                return commission;
            }
            //Blank value for commission if less than quota
            else
            {
                return "";
            }
        }


That is the segment to my code.

What I'm trying to do is return a blank text box instead of 0. I've tried converting a double variable to a string, but it says I cannot implicitly do that and I know no other way to do it other than to change the type of function. (Trying to avoid doing that because of the immense size of the program.)
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 02:51 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Try returning null, like


csharp

//Commission method
private double comMethod(double sales)
{
double commission;

//If statement for commission when greater or equal than quota
if (sales >= quota)
{
commission = comRate * sales;
return commission;
}
//Blank value for commission if less than quota
else
{
return null;
}
}

User is offlineProfile CardPM
+Quote Post

Soyaku
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 02:58 PM
Post #3

New D.I.C Head
*

Joined: 28 Feb, 2008
Posts: 42


My Contributions
QUOTE(PsychoCoder @ 18 Mar, 2008 - 03:51 PM) *

Try returning null, like



It just said "Cannot convert null to 'double' because it is a value type.

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:00 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well null is not going to work because it is a value type. You need something to convert to double, but anything of no value is converted to zero because that is what nothing is in a double. 0 means nothing when you are talking doubles. But in terms of your method here, 0 would be fine. You are saying that if they don't meet their quota that they get no commission. Hence they get nothing, nada... ZERO!

smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:04 PM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Ok, you don't have to change your function at all, keep it the way it is but return 0


csharp

//Commission method
private double comMethod(double sales)
{
double commission;

//If statement for commission when greater or equal than quota
if (sales >= quota)
{
commission = comRate * sales;
return commission;
}
//Blank value for commission if less than quota
else
{
return 0;
}
}



Then before putting the value into your TextBox check it's value, if it's 0 don't put anything into the textbox, like so


csharp

double myVar = comMethod(100.55)
if(!(myVar = 0))
{
textBox1.Text = myVar.ToString();
}
else
{
textBox1.Text = string.Empty();
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:04 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Now you are thinking what I am thinking Psycho. wink2.gif
User is offlineProfile CardPM
+Quote Post

Soyaku
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:05 PM
Post #7

New D.I.C Head
*

Joined: 28 Feb, 2008
Posts: 42


My Contributions
QUOTE(Martyr2 @ 18 Mar, 2008 - 04:00 PM) *

Well null is not going to work because it is a value type. You need something to convert to double, but anything of no value is converted to zero because that is what nothing is in a double. 0 means nothing when you are talking doubles. But in terms of your method here, 0 would be fine. You are saying that if they don't meet their quota that they get no commission. Hence they get nothing, nada... ZERO!

smile.gif



I'm starting to think that that would be a better idea.

Would null work for a void function?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:06 PM
Post #8

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well of course Marty, great minds think alike wink2.gif

A void doesn't return a value, so I don't think it will work for your requirements.

This post has been edited by PsychoCoder: 18 Mar, 2008 - 03:06 PM
User is offlineProfile CardPM
+Quote Post

Soyaku
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:07 PM
Post #9

New D.I.C Head
*

Joined: 28 Feb, 2008
Posts: 42


My Contributions
You guys respond really fast!

I appreciate all of the help, I'll try those and let you know how it goes. smile.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:08 PM
Post #10

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
QUOTE(Soyaku @ 18 Mar, 2008 - 04:05 PM) *

QUOTE(Martyr2 @ 18 Mar, 2008 - 04:00 PM) *

Well null is not going to work because it is a value type. You need something to convert to double, but anything of no value is converted to zero because that is what nothing is in a double. 0 means nothing when you are talking doubles. But in terms of your method here, 0 would be fine. You are saying that if they don't meet their quota that they get no commission. Hence they get nothing, nada... ZERO!

smile.gif



I'm starting to think that that would be a better idea.

Would null work for a void function?


Well what I am saying to you is that for the context you have setup here you want it to return 0. You are saying that they get no commission. Returning null wouldn't be right. I would leave it just the way it is and return 0 indicating they get no commission and do as Psycho suggested, in the calling function check if the commission was zero and blank out the box accordingly.

smile.gif
User is offlineProfile CardPM
+Quote Post

Soyaku
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:15 PM
Post #11

New D.I.C Head
*

Joined: 28 Feb, 2008
Posts: 42


My Contributions
QUOTE(PsychoCoder @ 18 Mar, 2008 - 04:04 PM) *




CODE
                //Test Box Displays
                totalBox.Text = totalPay.ToString("N2");
                //If statement for commission if value is 0
                if (commission == 0)
                {
                    //Will be blank
                    comBox.Text = "";
                }
                else
                {
                    //Will have value
                    comBox.Text = commission.ToString("N2");
                }


Well, it worked!

I have to say, that the solution to this problem was very easy, yet I did not even think to do it since I was using it in a function.

Thank you both for your help!


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Function: Returning A Blank Textbox
18 Mar, 2008 - 03:24 PM
Post #12

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The DIC mentors do it again!" *HIIIIIGH FIVEEEE* wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month