473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to pass a null and another value into a function

Ok, I keep running into this problem

In my aspx I am calling a simple function.

But the problem is, that the function is either, string, integer, etc.

If there's null's in the data, the function explodes because it's set to a string or integer, etc.

How can I make the function accept both a null, and another data type so that I can

write some if then's to do different things if it is null or another data type.

Here is the code to give you an idea

Expand|Select|Wrap|Line Numbers
  1.  
  2. .aspx code
  3.  
  4. <%# GetRatingFormat(Container.DataItem("Rating") %>
  5.  
  6.  
  7. .vb code
  8.  
  9. Function GetRatingFormat(ByVal UserRating As Decimal) As String
  10.  
  11.         If IsNothing(UserRating) Or IsDBNull(UserRating) Then
  12.             Return "Not Yet Rated"
  13.         Else
  14.             Return Format(UserRating, "0.0") & "%"
  15.         End If
  16.  
  17.  
  18.     End Function
  19.  
  20.  
  21.  
UserRating needs to accept both null and decimal

any help would be greatly appreciated
Oct 19 '10 #1
9 2389
danp129
323 Expert 256MB
Try declaring UserRating as Object.
Oct 20 '10 #2
Hi Dan,

thanks for the reply. that's how I had it originally. The object was dropping the value of the decimal and making it a 0.0
Oct 20 '10 #3
danp129
323 Expert 256MB
Format requires a numeric data type and it was likely being passed a string. This should work.

Expand|Select|Wrap|Line Numbers
  1.     Function GetRatingFormat(ByVal userRating As Object) As String
  2.         Dim decimalRating As Decimal = Nothing
  3.  
  4.         If TypeOf userRating Is Decimal Then
  5.             decimalRating = DirectCast(userRating, Decimal)
  6.         ElseIf IsNumeric(userRating) Then
  7.             decimalRating = CDec(userRating)
  8.         End If
  9.  
  10.         If decimalRating = Nothing Then
  11.             Return "Not Yet Rated"
  12.         Else
  13.             Return Format(decimalRating, "0.0") & "%"
  14.         End If
  15.  
  16.     End Function
  17.  
Oct 21 '10 #4
Works great!

I don't really understand what's going on in your code, but it works. Was hoping there was a generic way of doing it in here "(ByVal userRating As Object"

in case I want to do other functions with both null and datatype.

But I can work with this :)

Your help is much appreciated!

Thank you
Oct 21 '10 #5
danp129
323 Expert 256MB
You can use overloads if you want.

Expand|Select|Wrap|Line Numbers
  1.      Overloads Function GetRatingFormat(ByVal userRating As Object) As String
  2.           ' Your code here
  3.      End Function
  4.  
  5.      Overloads Function GetRatingFormat(ByVal userRating As String) As String
  6.           ' Your code here
  7.      End Function
  8.  
  9.      Overloads Function GetRatingFormat(ByVal userRating As Decimal) As String
  10.           ' Your code here
  11.      End Function
  12.  
  13. ' etc...
Oct 22 '10 #6
Frinavale
9,735 Expert Mod 8TB
I would have gone with Overloading the method or even using Optional parameters. There are quite a few ways to solve this problem :)

-Frinny
Oct 22 '10 #7
wouldn't happen to have an example code of Optional Parameters?
Oct 22 '10 #8
Frinavale
9,735 Expert Mod 8TB
Something like this

Expand|Select|Wrap|Line Numbers
  1. Function GetRatingFormat(Optional ByVal userRating  As Object = Nothing, Optional ByVal userRatingDec As Decimal = 0, Optional ByVal  userRatingStr As String = "")
  2.   'In this function you will have to validate each
  3.   'parameter to determine which one has bee provided
  4. End Function
After typing out the example...I don't like this recommendation any more. The overloaded version is your best approach in my opinion.

-Frinny
Oct 25 '10 #9
OK!!!!! thanks for the input Frinny and everyone else!
Oct 25 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Huskier | last post by:
Hi all: I want to pass a class-member function to pthread_create, and my code is in the following, but I don't know how to pass myobj.thread_function to pthread_create function. #include...
7
by: Uwe Range | last post by:
Hi to all, just ran into a problem: I wrote a function which works with several values collected on a form. I thought it would be no problem, if the fields were empty, because I made the...
6
by: yezi | last post by:
Hi: How to code for operation between 2 structures? The structure's defination is : struct sockaddr_in my_addr; I need call a subfunction and pass the value in my_addr to the subfuncition...
4
by: tom | last post by:
Hi Experts, I want to pass the selectedDate value from my calender form to another web form or a web user control. Could you please show me how to do this? Thanks in advance.
0
by: Mahesh_Bote | last post by:
i have one stored procedure. i wants to pass NULL to one of the parameter. so i tried Params.Add(System.DBNull.Value) (here params is Arralist) can anybody tell, how to pass NULL to the...
44
by: sam_cit | last post by:
Hi Everyone, I tried the following program unit in Microsoft Visual c++ 6.0 and the program caused unexpected behavior, #include <stdio.h> #include <string.h> int main() {
2
by: guy.gorodish | last post by:
hi, i have couple of qeustions: 1. I have built a interop of c dll. In this dll i have a function that expect a pointer to a struct. I want to pass a struct from my c# interop into this c...
9
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried...
3
JustRun
by: JustRun | last post by:
Hi, I have a method which receive a Guid param, How to call it passing null to this param? public void AddNewRecord(Guid _userId) { } // Call it here:
9
by: nicnac | last post by:
Hi, I'm self learning javascript - so any pointers are welcomed!! I have an issue passing a form and array from one function to another. I tried many variations ! I can't get this to work...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.