newbie problem with if-else

  • Thread starter Thread starter Aahz
  • Start date Start date
A

Aahz

Hello;
I have dynamically crated string of numbers divided with comma
(2,3,5,6,4,)
Last comma is trimmed becose I need this string to create SQL query
When string is not empty it works fine, last comma is gone.

Now I need to make to determine if string is empty to skip trimming
comma.

Somehow I cannot do that becose when string is empty I always get:
"Object reference not set to an instance of an object."

Plese tell me what I am doing wrong ???

My code:

if (string =="")
{
newstring = "";
}

else
{
char comma = ',';
newstring = string.TrimEnd(comma);
}

Thanks;
 
string can be also null, that's why you get that exception

if (s != null && s != "")
{
...
}
 
int index = someString.LastIndexOf(',');
if (index == -1) newString = "";
else newString = someString.Substring(0, index);

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Aahz said:
Hello;
I have dynamically crated string of numbers divided with comma
(2,3,5,6,4,)
Last comma is trimmed becose I need this string to create SQL query
When string is not empty it works fine, last comma is gone.

Now I need to make to determine if string is empty to skip trimming
comma.

Somehow I cannot do that becose when string is empty I always get:
"Object reference not set to an instance of an object."

<snip>

As well as the other responses, if you're using .NET 2.0, you can use
String.IsNullOrEmpty.
 
I am afraid it not it. Probably I did't explain corectly
I made changes you suggested and still getting "Object reference not
set to an instance of an object"

It say error is in line: newstring = string.TrimEnd(comma);

or if I do as Kevin Spencer suggested then same error in line:

int index = someString.LastIndexOf(',');

I am confuzed, I really did't expect this to be dificult..
 
Aahz said:
I am afraid it not it. Probably I did't explain corectly
I made changes you suggested and still getting "Object reference not
set to an instance of an object"

It say error is in line: newstring = string.TrimEnd(comma);

That line shouldn't even compile, as TrimEnd isn't a static method.

Could you post a short but complete program that demonstrates the
problem? See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon
 
Hi,

Looks like you have named a variable to a type.
i.e string string = "hello";

Is that the problem ?
If yes, rename the variable OR prefix it with @.

HTH
Kalpesh
 
Here is short but complete: I remind you that I removed unnecessary
parts
like one where str1 is being created
(str1 is always series of numbers formated like this: 2,4,5,6, )
I managed to get rid of last comma but when str1 is empty things fall
apart.

using System;
public class WebForm1 : System.Web.UI.Page
{
private string str1;
private string str2;
private void Button2_Click(object sender, System.EventArgs e)
{
if (str1 =="" && str2 ==null)
{
str2 = "";
}

else
{
char strcomma = ',';
str2 = str1.TrimEnd(strcomma);
}
}
}

Thank you;
 
Aahz said:
Here is short but complete

No, that's *not* complete. There's no button! In other words, we can't
just copy and paste this code, run it and see the problem. In general,
console apps are *much* easier to use to solve problems (even if it
originally occurred in an ASP.NET or WinForms app).

Please read http://www.pobox.com/~skeet/csharp/incomplete.html for
future reference.

However, your problem is probably this line:
if (str1 =="" && str2 ==null)

You probably mean:

if (str1=="" || str1==null)

Note the change from "&&" to "||" and from comparing str2 to str1.

Jon
 
If str1 is always a string (not null), using LastIndexOf will not throw an
exception. If there are no commas in the string, it will return -1.
Otherwise, it will return the index of the last comma in the string.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Hello;
I have dynamically crated string of numbers divided with comma
(2,3,5,6,4,)
Last comma is trimmed becose I need this string to create SQL query
When string is not empty it works fine, last comma is gone.

Now I need to make to determine if string is empty to skip trimming
comma.

Somehow I cannot do that becose when string is empty I always get:
"Object reference not set to an instance of an object."

Plese tell me what I am doing wrong ???

My code:

if (string =="")
{
newstring = "";
}

else
{
char comma = ',';
newstring = string.TrimEnd(comma);
}

Thanks;

How about constructing the string correctly in the first place:

In pseudocode:
add_number_to_string(number, string) {
if (string length > 0) append "," to string;
append number to string;
return;
}

This way there is no need to remove a comma from the end of the
string. It works by treating the first number differently, it is the
only one not preceeded by a comma.

rossum


The ultimate truth is that there is no ultimate truth
 
Back
Top