Empty string problem

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following:
Article.Tags = Article.TagsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new YTag() { Name =
t.Trim() }).ToList();

This works fine but when TagsCsv is empty I get an error.
Is there a way to solve this without needing to add an IF?

Thanks,
Miguel
 
I have the following:
Article.Tags = Article.TagsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new YTag() { Name =
t.Trim() }).ToList();
This works fine but when TagsCsv is empty I get an error.
Is there a way to solve this without needing to add an IF?

Until you are more specific about _what_ error you get, and especially  
what you expect/would like to happen instead, no.  There's not really a 
solution.

As usual, you should consider being more specific when you ask your  
question.  Concise-but-complete code examples are _always_ a good idea.

Pete

Basically initially both Tags and TagsCsv are null.
When I apply this code line if TagsCsv is null I get the error:
Object reference not set to an instance of an object.

I would like to not get an error and both, Tag and TagCsv, keep being
null.

I can use an if like:
if (!String.IsNullOrEmpty(TagsCsv))
Article.Tags = Article.TagsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new YTag() { Name =
t.Trim() }).ToList();

I am just checking if there is a better way that would be incorporated
on the Split code line.

Thanks,
Miguel
 
Any chance of actually telling the newsgroup what the error is...?

Mark I wrote what the error is on my last thread:
"Object reference not set to an instance of an object."

This is the error I get when I debug it. I think it is because I am
trying to split a string (TagsCsv) that is empty.
So the only solution I see is using the IF but I am not sure ...

Thanks,
Miguel
 
I have the following:
Article.Tags = Article.TagsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new YTag() { Name =
t.Trim() }).ToList();

This works fine but when TagsCsv is empty I get an error.

As far as I can see Tags and TagsCsv are(propably) static so maybe try
simply define TagsCsv?

public class Article
{
public static IEnumerable<string> Tags;
public static string TagsCsv = string.Empty;
}

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TestLinq();
}
public void TestLinq()
{
Article.Tags = Article.TagsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim());
Article.Tags = from s in Article.TagsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries) select s;
}
}

Dawid Rutyna
 
Basically initially both Tags and TagsCsv are null.

FYI: "null" is not the same as "empty", especially when talking about  
strings and collection classes.
When I apply this code line if TagsCsv is null I get the error:
  Object reference not set to an instance of an object.
I would like to not get an error and both, Tag and TagCsv, keep being
null.
I can use an if like:
if (!String.IsNullOrEmpty(TagsCsv))
  Article.Tags = Article.TagsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new YTag() { Name =
t.Trim() }).ToList();
I am just checking if there is a better way that would be incorporated
on the Split code line.

You can't avoid the check for null.  But you can avoid writing it so  
explicitly.  Something like this might suit you (extra stuff elided for 
brevity):

     Article.Tags = (Article.TagsCsv ?? "")
         .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
         .Select(t => new YTag() { Name = t.Trim() })
         .ToList();

Note the "null coalescing" operator that does the check for you and  
replaces the expression with something usable.

If that's not helpful to you, then I will reiterate: a  
concise-but-complete code sample is always a good idea.

Pete

Thank you Pete ...

Please, don't think that I avoid to post all my code ...
But when you say to post a concise-but-complete code sample I really
don't know what you mean ...

In this case, Article is a class with a few properties including:
List<YTag> Tags and String TagsCsv
YTag is also a class with two properties: Guid Id and String Name

Is this what you mean? I should post the code for these objects when I
post a question?

Thanks,
Miguel
 
Why don't you want to use an "If", in this case the benefit is also that it
optimizes your processing.

Cor

Any chance of actually telling the newsgroup what the error is...?

Mark I wrote what the error is on my last thread:
"Object reference not set to an instance of an object."

This is the error I get when I debug it. I think it is because I am
trying to split a string (TagsCsv) that is empty.
So the only solution I see is using the IF but I am not sure ...

Thanks,
Miguel
 
Why don't you want to use an "If", in this case the benefit is also that it
optimizes your processing.

Cor




Mark I wrote what the error is on my last thread:
"Object reference not set to an instance of an object."

This is the error I get when I debug it. I think it is because I am
trying to split a string (TagsCsv) that is empty.
So the only solution I see is using the IF but I am not sure ...

Thanks,
Miguel

Thank You for the links
 
Back
Top