473,387 Members | 1,574 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,387 software developers and data experts.

Empty string

M D
You know how you assume you know until you find out you don't know.

Well, I typed into a function definition "..., new String("")).

I know what I want. Everyone reading this knows what I want. However,
the 1.0 compiler has no idea what I want.

Can anyone share with me the syntax that will actually communicate the
concept to the compiler, please?

thx
md

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
21 5719
Actually I'm not sure what you want to accomplish. Creating a new
string with "new String()" automatically creates a zero length string.
So why use quotation marks?

Best Regards
Johann Blake

Nov 17 '05 #2
M D wrote:
You know how you assume you know until you find out you don't know.

Well, I typed into a function definition "..., new String("")).

I know what I want. Everyone reading this knows what I want.
However, the 1.0 compiler has no idea what I want.

Can anyone share with me the syntax that will actually communicate the
concept to the compiler, please?

thx
md

*** Sent via Developersdex http://www.developersdex.com ***


Why not use just "" ?
It's a bit of overkill to create a new string-object that you supply with
a string-literal (which is also a string-object) that has exactly the same
value as you want to end up with...

You could also use the predefined constant String.Empty.

By the way, 1.1 has been out for quite some time ...

Hans Kesting
Nov 17 '05 #3
I second this, use the constant String.Empty, instead of using a magic
value. It's just better practice.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
M D wrote:
You know how you assume you know until you find out you don't know.

Well, I typed into a function definition "..., new String("")).

I know what I want. Everyone reading this knows what I want.
However, the 1.0 compiler has no idea what I want.

Can anyone share with me the syntax that will actually communicate the
concept to the compiler, please?

thx
md

*** Sent via Developersdex http://www.developersdex.com ***


Why not use just "" ?
It's a bit of overkill to create a new string-object that you supply with
a string-literal (which is also a string-object) that has exactly the same
value as you want to end up with...

You could also use the predefined constant String.Empty.

By the way, 1.1 has been out for quite some time ...

Hans Kesting

Nov 17 '05 #4
M D
new String(): "No overload for method 'String' takes '0' arguments"

I need to create a new string on the heap for the top of a recursive
method. Wouldn't just foo(..., "") reference a constant on the string
table, ie an immutable object?

I haven't been given the VS.net version beyond the first, hence 1.0.

thx
md

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5
Hi M.D

if you really have to use a new instance of string, use:
string.Copy("")

but there really is no use of that other, than demonstrating the difference
of value and reference comparison.

Christof

"M D" <ma******@aol.com> schrieb im Newsbeitrag
news:%2***************@TK2MSFTNGP10.phx.gbl...
You know how you assume you know until you find out you don't know.

Well, I typed into a function definition "..., new String("")).

I know what I want. Everyone reading this knows what I want. However,
the 1.0 compiler has no idea what I want.

Can anyone share with me the syntax that will actually communicate the
concept to the compiler, please?

thx
md

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #6
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
I second this, use the constant String.Empty, instead of using a magic
value. It's just better practice.


In what way is it better practice than ""? They're both effectively
magic values in a way - it's not like using "foo" in two different
places, where you could accidentally change "foo" in one place but not
in another, but change the value in one place if you had it defined as
a constant. In this case, you'll never be able to change the value of
String.Empty anyway...

I've heard several people say that it *is* better to use String.Empty,
but never any believable reasons why. Personally I find "" more
readable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Jon,

It really lends itself to maintaining discipline more than anything
else. In this case, I would say that there is a good arguement for "".
However, it is because of special conditions such as this that other
deviations from the practice become accepted. If there was actually a value
there, I am sure you would argue that a named constant should be used, for a
number of reasons. If the OP's value was subject to change, I would
actually recommend him using a named constant of his own (and not
String.Empty) and use that instead.

The interesting part about this is that I would not use a constant for
something such as 0 (when starting a loop) or 1 (for incrementing something
by one). Everyone has different tolerances, the empty string falls outside
of mine in this case.

For me, I just find it easier to adhere to an absolute rule instead of
having special cases (and admittedly, this is one of those cases where the
value isn't readily apparent). The more special cases you have, the more
you have to remember what they are, and the more subject your code is to
bugs.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
I second this, use the constant String.Empty, instead of using a
magic
value. It's just better practice.


In what way is it better practice than ""? They're both effectively
magic values in a way - it's not like using "foo" in two different
places, where you could accidentally change "foo" in one place but not
in another, but change the value in one place if you had it defined as
a constant. In this case, you'll never be able to change the value of
String.Empty anyway...

I've heard several people say that it *is* better to use String.Empty,
but never any believable reasons why. Personally I find "" more
readable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #8
Well, it's a monir point, but "String.Empty" is easier to search for, if
you needed to find everyplace it's used (searching for "" would trip you up
with false positives on strings like : @"Who said ""Which way did he
go?""?";

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
I second this, use the constant String.Empty, instead of using a magic value. It's just better practice.


In what way is it better practice than ""? They're both effectively
magic values in a way - it's not like using "foo" in two different
places, where you could accidentally change "foo" in one place but not
in another, but change the value in one place if you had it defined as
a constant. In this case, you'll never be able to change the value of
String.Empty anyway...

I've heard several people say that it *is* better to use String.Empty,
but never any believable reasons why. Personally I find "" more
readable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
It really lends itself to maintaining discipline more than anything
else. In this case, I would say that there is a good arguement for "".
However, it is because of special conditions such as this that other
deviations from the practice become accepted. If there was actually a value
there, I am sure you would argue that a named constant should be used, for a
number of reasons. If the OP's value was subject to change, I would
actually recommend him using a named constant of his own (and not
String.Empty) and use that instead.
Absolutely.
The interesting part about this is that I would not use a constant for
something such as 0 (when starting a loop) or 1 (for incrementing something
by one). Everyone has different tolerances, the empty string falls outside
of mine in this case.
That's fine - but I think it's then a matter of personal preference
rather than best practice.
For me, I just find it easier to adhere to an absolute rule instead of
having special cases (and admittedly, this is one of those cases where the
value isn't readily apparent). The more special cases you have, the more
you have to remember what they are, and the more subject your code is to
bugs.


I think it depends on the readability for me - "" is more readable than
String.Empty, and it's an easy enough single value to remember as an
exception to the rule.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
James Curran <ja*********@mvps.org> wrote:
Well, it's a monir point, but "String.Empty" is easier to search for, if
you needed to find everyplace it's used (searching for "" would trip you up
with false positives on strings like : @"Who said ""Which way did he
go?""?";


How many of those do you regularly have? ;)

(Personally I rarely use verbatim string literals just for the sake of
quotes, preferring \" to "".)

I suspect the number of minutes wasted on false positives when looking
for "" and finding other examples is vanishingly small. I'll take
readability over ease of searching any time.

(I know that some find String.Empty more readable, at which point it
certainly makes sense for them to use that instead. I just find the
idea that String.Empty is absolutely *better* than "" to be dodgy.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
M D
I think you guys have missed the point.

What I had to do was create a string field at object level and
initialize it at the begining of each call to the recursion because ""
cannot be passed by ref as it is not an lvalue (as I suggested, it is on
the stack not the heap.) This is what I was trying to avoid as it is
poor form in object orientation where I should be able to create a new
String(), leave it nameless and under the perview of the garbage
collector once the recursion is complete.

I wouldn't have had this problem if I were creating, say, a new
StreamWriter. Or if there were a way to create a new empty string
object.

String.Copy("") is also not an lvalue.

thx
md

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #12
aha.... What you want is:

string mystring = "";
MyRecursiveFunction(ref mystring);

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"M D" <ma******@aol.com> wrote in message
news:#4**************@tk2msftngp13.phx.gbl...
I think you guys have missed the point.

What I had to do was create a string field at object level and
initialize it at the begining of each call to the recursion because ""
cannot be passed by ref as it is not an lvalue (as I suggested, it is on
the stack not the heap.) This is what I was trying to avoid as it is
poor form in object orientation where I should be able to create a new
String(), leave it nameless and under the perview of the garbage
collector once the recursion is complete.

I wouldn't have had this problem if I were creating, say, a new
StreamWriter. Or if there were a way to create a new empty string
object.

String.Copy("") is also not an lvalue.

thx
md

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #13
M D,
| Or if there were a way to create a new empty string
| object.
Have you tried:

String empty = new String('\0', 0)

Which creates a new empty string object.

Hope this helps
Jay
"M D" <ma******@aol.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
|I think you guys have missed the point.
|
| What I had to do was create a string field at object level and
| initialize it at the begining of each call to the recursion because ""
| cannot be passed by ref as it is not an lvalue (as I suggested, it is on
| the stack not the heap.) This is what I was trying to avoid as it is
| poor form in object orientation where I should be able to create a new
| String(), leave it nameless and under the perview of the garbage
| collector once the recursion is complete.
|
| I wouldn't have had this problem if I were creating, say, a new
| StreamWriter. Or if there were a way to create a new empty string
| object.
|
| String.Copy("") is also not an lvalue.
|
| thx
| md
|
| *** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #14
Actually, I'm unclear what you what to do. The following works as you'd
expect:

public class MyClass
{
public static void Main()
{
MyClass m = new MyClass();
Console.WriteLine(m.Foo(10, ""));
}

public string Foo(int n, string str)
{
if (n == 0)
return str;
else
return String.Format("{0}-{1}", n, Foo(n-1, str));
}
}
==========================================
This also works as you'd expect:

public class MyClass
{
public static void Main()
{
MyClass m = new MyClass();
string str = "";
m.Foo2(10, ref str);
Console.WriteLine(str);
}

public void Foo2(int n, ref string str)
{
if (n == 0)
return ;
else
{
str += '-' + n.ToString();
Foo2(n-1, ref str);
return ;
}
}
}

================================================
This, on the other hand, seems to show the error you are getting:
public class MyClass
{
public static void Main()
{
MyClass m = new MyClass();
m.Foo2(10, ref "");
Console.WriteLine(str); /// what do you output here?
RL();
}
public void Foo2(int n, ref string str)
{
if (n == 0)
return ;
else
{
str += '-' + n.ToString();
Foo2(n-1, ref str);
return ;
}
}
}
========================================
That won't compile, but even if it did, it wouldn't work, because Foo2 is
generating output, but aren't saving it.

"ref" says "I'm giving you a place to put the answer", but you expressly
DON'T want to give it such a place. The problem isn't with the compile, but
with your code, which is not consistent with itself.
""
cannot be passed by ref as it is not an lvalue (as I suggested, it is on
the stack not the heap.)
No. "" is not an lvalue because it is NEITHER on the stack frame NOR on
the heap.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"M D" <ma******@aol.com> wrote in message
news:#4**************@tk2msftngp13.phx.gbl... I think you guys have missed the point.

What I had to do was create a string field at object level and
initialize it at the begining of each call to the recursion because ""
cannot be passed by ref as it is not an lvalue (as I suggested, it is on
the stack not the heap.) This is what I was trying to avoid as it is
poor form in object orientation where I should be able to create a new
String(), leave it nameless and under the perview of the garbage
collector once the recursion is complete.

I wouldn't have had this problem if I were creating, say, a new
StreamWriter. Or if there were a way to create a new empty string
object.

String.Copy("") is also not an lvalue.

thx
md

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #15
Jay B. Harlow [MVP - Outlook] <Ja************@msn.com> wrote:
M D,
| Or if there were a way to create a new empty string
| object.
Have you tried:

String empty = new String('\0', 0)

Which creates a new empty string object.


Indeed it does - but I had to check. You see, the seemingly similar:

String empty = new String (new char[0]);

*doesn't* produce a new string on .NET 1.1. Instead, it returns the
same reference that "" or String.Empty do. This curiosity appears to
have been removed from .NET 2.0...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #16
M D <ma******@aol.com> wrote:
I think you guys have missed the point.
That would be because you never actually spelled out the point -
nowhere in your previous posts did you mention that you wanted to pass
anything by reference.
What I had to do was create a string field at object level and
initialize it at the begining of each call to the recursion because ""
cannot be passed by ref as it is not an lvalue (as I suggested, it is on
the stack not the heap.)
The value of the local variable is on the stack, whatever the type is.
The string data itself is never on the stack.

See http://www.pobox.com/~skeet/csharp/memory.html
This is what I was trying to avoid as it is
poor form in object orientation where I should be able to create a new
String(), leave it nameless and under the perview of the garbage
collector once the recursion is complete.

I wouldn't have had this problem if I were creating, say, a new
StreamWriter.


Yes you would, if you'd wanted to use pass by reference. To pass
*anything* by reference, you have to have a variable, whether it's a
string or a StreamWriter.

It's possible that you're using pass-by-reference semantics
unnecessarily - see
http://www.pobox.com/~skeet/csharp/parameters.html

Also, building up a string should usually be done using a StringBuilder
rather than creating lots of temporary strings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #17
Jon,
I specifically checked 1.1 for new String('\0', 0), I didn't bother checking
new String (new char[0]), interesting it doesn't...

I had not tried 2.0 yet, as I've been busy with VSTO for Outlook on it.

Thanks for the additional info
Jay

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
| Jay B. Harlow [MVP - Outlook] <Ja************@msn.com> wrote:
| > M D,
| > | Or if there were a way to create a new empty string
| > | object.
| > Have you tried:
| >
| > String empty = new String('\0', 0)
| >
| > Which creates a new empty string object.
|
| Indeed it does - but I had to check. You see, the seemingly similar:
|
| String empty = new String (new char[0]);
|
| *doesn't* produce a new string on .NET 1.1. Instead, it returns the
| same reference that "" or String.Empty do. This curiosity appears to
| have been removed from .NET 2.0...
|
| --
| Jon Skeet - <sk***@pobox.com>
| http://www.pobox.com/~skeet
| If replying to the group, please do not mail me too
Nov 17 '05 #18
M D
not just:

string mystring = "";
foo(ref mystring);

I wanted to call:

foo(ref new string('\0',0), 0); //such intuitive syntax
...
private foo(ref string scratch, int k) {
do some things;
foo(scratch, k+1)
do something with what was accumulated in scratch;
}

and NO

private string mystring = "";

Do they call that a anonomous object?

I've never used StringBuilder which is probably a better way except for
the month of study it would likely take to figure out MS wants it to be
used.

I thought my question was rather clear, "how do you make a new empty
string?" All the answers until someone (thank-you) came up with new
String('\0',0) insisted that "" was an object rather than some static
item on the string table!

I guess bad code is that which is not dedicated to a WinForm.

thx
md

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #19
M D <ma******@aol.com> wrote:
not just:

string mystring = "";
foo(ref mystring);

I wanted to call:

foo(ref new string('\0',0), 0); //such intuitive syntax
You couldn't do that whatever the type was - the problem isn't creating
a new empty string, it's that you don't have a variable in the above,
and you can't pass a plain value by reference - you have to pass the
variable.

From the C# spec:

<quote>
When a formal parameter is a reference parameter, the corresponding
argument in a method invocation must consist of the keyword ref
followed by a variable-reference (§12.3.3) of the same type as the
formal parameter.
</quote>
private foo(ref string scratch, int k) {
do some things;
foo(scratch, k+1)
do something with what was accumulated in scratch;
}
The right way to do that is definitely to use a StringBuilder instead.
I've never used StringBuilder which is probably a better way except for
the month of study it would likely take to figure out MS wants it to be
used.
If it takes you a month to learn how to use StringBuilder, then you
should really consider a different hobby or line of work. There's a
very brief description of it at
http://www.wintellect.com/resources/...ive.aspx?id=10
I thought my question was rather clear, "how do you make a new empty
string?"
But without any context, and certainly no mention of ref parameters.
It's very, very rare to really *need* a new empty string, and you don't
actually need one here. If you'd written some of the context in your
original post, we could have saved a lot of time.
All the answers until someone (thank-you) came up with new
String('\0',0) insisted that "" was an object rather than some static
item on the string table!
"" is a reference to an object, just as new String('\0', 0) is. They're
both references to strings, and all strings are immutable. You haven't
demonstrated any need for actually creating a *new* string object
rather than using String.Empty or "".
I guess bad code is that which is not dedicated to a WinForm.


I think you've misunderstood both the problem you're facing and the
answers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #20
"M D" <ma******@aol.com> schrieb im Newsbeitrag
news:e4**************@TK2MSFTNGP15.phx.gbl...
<snip>
I wanted to call:

foo(ref new string('\0',0), 0); //such intuitive syntax
..

</snip>

If that would be possible in C#, it would be the same as:

foo(ref "", 0);

(besides, it would be the same instance as any other "" literal; but tha is
only remarkable by reference comparison)

ref doesn't mean your giving an object reference to the method.
it means your giving a variable reference to the method.

Christof
Nov 17 '05 #21
M D
You are correct. I figured it was like:

for (int i=0; i<somearray.len; i++)
somearray[i] = new StreamWriter(i.ToString()+"outfile.txt");

where I never have to name the object.

ref won't let you do that. That explains why none of the suggestions
worked.

my apologies.

thx
md

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
11
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
4
by: web1110 | last post by:
I have an array of of 5 string elements. I put values in 3 of them. Yet when I loop over them, I do not catch the empty string. The code output below does not include "Empty" stringx=new...
14
by: cj | last post by:
What is string.empty used for? I can't say: if string.empty then I have to use: if string = "" then which is ok, I just want to know what .empty is for.
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
2
by: Jay | last post by:
I have a SQL Server table with nvarchar type column which has not null constraint. I am inserting empty string ("") from Java to the table column. When I export this table into .csv file using bcp...
21
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.