10 Replies - 2192 Views - Last Post: 17 March 2010 - 12:32 AM Rate Topic: -----

#1 coder 123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-February 10

insert null in string array help

Posted 11 March 2010 - 05:08 AM

How do I insert a null in a variable string array.
string[] AutoComp = { "michal", "chris", "gems" };



thanks
Is This A Good Question/Topic? 0
  • +

Replies To: insert null in string array help

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: insert null in string array help

Posted 11 March 2010 - 05:13 AM

Null you can't but what you can insert is string.Empty. That has the same general principle as null, but it is techincally a value so it should work. Without more info and knowing what you are trying to accomplish though I can't say for sure.
Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: insert null in string array help

Posted 11 March 2010 - 05:20 AM

I don't understand what is the problem...

This works just fine for me...

string[] array = { "b", "n", "d", null };
Was This Post Helpful? 2
  • +
  • -

#4 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: insert null in string array help

Posted 11 March 2010 - 05:48 AM

DOH! that is true. Since String is a reference type not a value type, null is allowed. I retract my previous statement and thanks FlashM for correcting that!
Was This Post Helpful? 0
  • +
  • -

#5 Sn0wBum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 178
  • Joined: 04-February 10

Re: insert null in string array help

Posted 11 March 2010 - 05:57 AM

I'm not sure why you would want to enter a null value like this. In this example with the addition of the null you could initialize the array by setting its size and then enter it's values.

string[] AutoComp = new string[4];
AutoComp[0] = "name1";
AutoComp[1] = "name2";
AutoComp[2] = "name3";

// Doing it this way will allow to you work through your code and enter a value for AutoComp[3] where ever you please. Keeping scope in mind of course.  :)/>


Was This Post Helpful? 1
  • +
  • -

#6 coder 123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 21-February 10

Re: insert null in string array help

Posted 11 March 2010 - 06:02 AM

thanks
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Prashant Gudadhe*


Reputation:

Re: insert null in string array help

Posted 16 March 2010 - 07:13 PM

View Postcoder 123, on 11 March 2010 - 04:08 AM, said:

How do I insert a null in a variable string array.
string[] AutoComp = { "michal", "chris", "gems" };



thanks


hi
try this...

DbNull.Value
or only "" (double quotes) or string.Empty

i think it will be helpfull to u.
Was This Post Helpful? 0

#8 Guest_Prashant Gudadhe*


Reputation:

Re: insert null in string array help

Posted 16 March 2010 - 07:15 PM

hi
try this...

DbNull.Value
or only "" (double quotes) or string.Empty

i think it will be helpfull to u.
Was This Post Helpful? 0

#9 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: insert null in string array help

Posted 16 March 2010 - 11:58 PM

Just to let you know: null is not equal to string.Empty...
Was This Post Helpful? 0
  • +
  • -

#10 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 525
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: insert null in string array help

Posted 17 March 2010 - 12:04 AM

Quote

Just to let you know: null is not equal to string.Empty..


While this is true, you should be doing the check String.IsNullOrEmpty() instead of myString != null && myString != string.Empty.

As for the original problem of inserting null - bad idea. As stated, use string.Empty. Well, unless you can provide a compelling reason for it of course. I just think it's going to bite you later.

Furthermore, string.Empty is easy to see what the person writing the code meant to say. With null, there's a bit more guess work that usually goes into understanding exact intent.
Was This Post Helpful? 0
  • +
  • -

#11 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: insert null in string array help

Posted 17 March 2010 - 12:32 AM

I would use null value for string variable when I would want to save a record in my database and at the same time wanted some field in a record to remain null value in my database. Otherwise, I don't see any reason to use null value for a string variable.

One more thing that might be important: Usually if you wanted to insert a record with some of the fields being null, you would use DBNull.Value. If using NHibernate, you would use null instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1