Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to check within multiple strings for which one is null and then set a value "0.0" for the null one ? I am getting values from SQLite database is there any way to avoid using if else if else for every string?

What I have tried:

I tried using if else if else for every string but I want a good way to do that thanks for any help
Posted
Updated 29-May-23 9:20am
Comments
[no name] 29-May-23 13:06pm    
Your "data objects" should have "properties" / members that you can iterate over and handle based on "type". (Which requires "code")

Another solution is to provide the field(s) with default values: SQLite Default Constraint - Tutlane[^]
If you do that, then a NULL insert will be replaced with the default by the DB engine.
 
Share this answer
 
Comments
brahim farhat (AAD) 30-May-23 12:29pm    
Hello thanks for answering when database is empty and and the app doing calculations i got null point exception if i set the value to default "0.0" will that help ?
brahim farhat (AAD) 4-Jun-23 10:25am    
This solution is not helping because the app doing calculations with the rows which are not exist.. i solved a part of problem with
total()
instead of
sum()
total method returns 0 instead of null
There's so much wrong here.

First, if null values from the database are invalid, those values never should have been allowed to be null when written to the database.

Next, you keep saying "string" and the value should be "0.0". Well, that's a floating-point number, nut really a string. So what is the value type supposed to be? A string or a float? The value you set should NOT be "0.0", but instead should be 0.0. Why would you set the value to "0.0" when you cannot do math with a string? See the difference?

Again, if this is supposed to be a floating-point number in this field, storing it in the database as a string is a really bad idea and should be fixed there.

Next, you can simply do what you are describing, no matter how bad your data handling is, with a simple function that takes a string as a parameter and returns a string:
NotActualJavaCode!
function string ValidateString(string value) {
    string returnValue = value;

    if (string.IsNullOrEmpty(value)
    {
        returnValue = "0.0";
    }

    return returnValue;
}

Then you simply call it for each field you want to validate:
NotActualJavaCode!
someField = ValidateString(someDatabaseField);
 
Share this answer
 
Comments
brahim farhat (AAD) 30-May-23 12:40pm    
Hello my Dear thanks for your answering my question the value in database is data type of REAL but how to get floating piont instead of string i am getting data like this :
Cursor res_p;
		res_p = db.sumProfits();
		res_p.moveToNext();
		total_profits = res_p.getString(0);
Dave Kreskowiak 30-May-23 13:08pm    
You start by fixing the database, THEN you update your code to get the correct data types.
brahim farhat (AAD) 4-Jun-23 10:29am    
Is there a way to get floating values instead of strings? I don't know about this ?
Dave Kreskowiak 4-Jun-23 11:18am    
Are the values still stored as strings in the database or has that column been fixed and now stores the values as REAL's?
brahim farhat (AAD) 6-Jun-23 10:43am    
They already stored as reals that is not the problem the problem is I have many columns when the app doing calculations i got null because rows are not exist yet it is logic to get null with sum() function i fixed some parts of problem by changing
sum()
to
total() total()
puts "0" with some fields

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900