Index Zero issue with sql - c#

I keep running into a problem with my sql:
{
Name.Text = String.Format("{0} {1}", reader.GetString(0), reader.GetString(1));
Aboutme.Text = String.Format("{2}", reader.GetString(0));
}
Index (zero based) must be greater than or equal to zero?
Not sure if its my String.Format?

Yes, the second line should be again 0 for string.Format:
Aboutme.Text = String.Format("{0}", reader.GetString(0));

My guess would be the second line of code
Aboutme.Text = String.Format("{2}", reader.GetString(0));
The String.Format, is looking for 3 parameters in that case, {0}, {1} and finally {2}. You have only one.
You should write it has :
Aboutme.Text = String.Format("{0}", reader.GetString(0));

The problem isn't the index for SQL, it's the index for string.Format. This should be evident in the stack trace, which will point to string.Format rather than reader.GetString. Looking carefully at stack traces can save you all manner of effort :)
Of course you don't really need to format at all here (given that you'll just end up with the input string), and I suspect you want column 2. (My guess is that that's where the error came from, after all.)
Aboutme.Text = reader.GetString(2);
If you really want to use string.Format:
Aboutme.Text = string.Format("{0}", reader.GetString(2));

There is a better solution than that proposed by the accepted answer. Note that the statement ...
Aboutme.Text = String.Format("{0}", reader.GetString(0));
... passes the format string and some string value to String.Format, which analyzes the format string, calls ToString() on the string value, and then inserts that value into the result at the position indicated by the sequence {0}. Then, of course, it returns the constructed string as its result. Because the format string is "{0}", the constructed string will have the same value as the argument.
Both the written code and the executed algorithm will be simpler if you write this:
Aboutme.Text = reader.GetString(0);

Related

String Insertion won't take any effect

Inserting a string into a string doesn't appear to have any effect. I'm using the following code:
string stNum = string.Format("{0:00}", iValue);
DesiredCode.Insert(0, stNum);
txtCode.Text = DesiredCode;
Breakpoints show stNum has the desired value, and DesiredCode is also as we would expect before insertion.
But after insertion, nothing will happen and the DesiredCode is the same as before!
Can someone please point me in the right direction as to what I'm doing wrong?
Strings are immutable. All the methods like Replace and Insert return a new string which is the result of the operation, rather than changing the data in the existing string. So you probably want:
txtCode.Text = DesiredCode.Insert(0, stNum);
Or for the whole block, using direct ToString formatting instead of using string.Format:
txtCode.Text = DesiredCode.Insert(0, iValue.ToString("00"));
Or even clearer, in my opinion, would be to use string concatenation:
txtCode.Text = iValue.ToString("00") + DesiredCode;
Note that none of these will change the value of DesiredCode. If you want to do that, you'd need to assign back to it, e.g.
DesiredCode = iValue.ToString("00") + DesiredCode;
txtCode.Text = DesiredCode;
Strings are immutable!
DesiredCode = DesiredCode.Insert(0, stNum);
Strings are immutable in C#. What this means is that you need to assign the return value of String.Insert to a string variable after the operation in order to access it.
string stNum = string.Format("{0:00}", iValue);
DesiredCode = DesiredCode.Insert(0, stNum);
txtCode.Text = DesiredCode;

Is it possible to include a C# variable in a string variable without using a concatenator?

Does .NET 3.5 C# allow us to include a variable within a string variable without having to use the + concatenator (or string.Format(), for that matter).
For example (In the pseudo, I'm using a $ symbol to specify the variable):
DateTime d = DateTime.Now;
string s = "The date is $d";
Console.WriteLine(s);
Output:
The date is 4/12/2011 11:56:39 AM
Edit
Due to the handful of responses that suggested string.Format(), I can only assume that my original post wasn't clear when I mentioned "...(or string.Format(), for that matter)". To be clear, I'm well aware of the string.Format() method. However, in my specific project that I'm working on, string.Format() doesn't help me (it's actually worse than the + concatenator).
Also, I'm inferring that most/all of you are wondering what the motive behind my question is (I suppose I'd feel the same way if I read my question as is).
If you are one of the curious, here's the short of it:
I'm creating a web app running on a Windows CE device. Due to how the web server works, I create the entire web page content (css, js, html, etc) within a string variable. For example, my .cs managed code might have something like this:
string GetPageData()
{
string title = "Hello";
DateTime date = DateTime.Now;
string html = #"
<!DOCTYPE html PUBLIC ...>
<html>
<head>
<title>$title</title>
</head>
<body>
<div>Hello StackO</div>
<div>The date is $date</div>
</body>
</html>
";
}
As you can see, having the ability to specify a variable without the need to concatenate, makes things a bit easier - especially when the content increases in size.
No, unfortunately C# is not PHP.
On the bright side though, C# is not PHP.
Almost, with a small extension method.
static class StringExtensions
{
public static string PHPIt<T>(this string s, T values, string prefix = "$")
{
var sb = new StringBuilder(s);
foreach(var p in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
sb = sb.Replace(prefix + p.Name, p.GetValue(values, null).ToString());
}
return sb.ToString();
}
}
And now we can write:
string foo = "Bar";
int cool = 2;
var result = "This is a string $foo with $cool variables"
.PHPIt(new {
foo,
cool
});
//result == "This is a string Bar with 2 variables"
No, it doesn't. There are ways around this, but they defeat the purpose. Easiest thing for your example is
Console.WriteLine("The date is {0}", DateTime.Now);
string output = "the date is $d and time is $t";
output = output.Replace("$t", t).Replace("$d", d); //and so on
Based on the great answer of #JesperPalm I found another interesting solution which let's you use a similar syntax like in the normal string.Format method:
public static class StringExtensions
{
public static string Replace<T>(this string text, T values)
{
var sb = new StringBuilder(text);
var properties = typeof(T)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.ToArray();
var args = properties
.Select(p => p.GetValue(values, null))
.ToArray();
for (var i = 0; i < properties.Length; i++)
{
var oldValue = string.Format("{{{0}", properties[i].Name);
var newValue = string.Format("{{{0}", i);
sb.Replace(oldValue, newValue);
}
var format = sb.ToString();
return string.Format(format, args);
}
}
This gives you the possibility to add the usual formatting:
var hello = "Good morning";
var world = "Mr. Doe";
var s = "{hello} {world}! It is {Now:HH:mm}."
.Replace(new { hello, world, DateTime.Now });
Console.WriteLine(s); // -> Good morning Mr. Doe! It is 13:54.
The short and simple answer is: No!
string.Format("The date is {0}", DateTime.Now.ToString())
No, But you can create an extension method on the string instance to make the typing shorter.
string s = "The date is {0}".Format(d);
string.Format (and similar formatting functions such as StringBuilder.AppendFormat) are the best way to do this in terms of flexibility, coding practice, and (usually) performance:
string s = string.Format("The date is {0}", d);
You can also specify the display format of your DateTime, as well as inserting more than one object into the string. Check out MSDN's page on the string.Format method.
Certain types also have overloads to their ToString methods which allow you to specify a format string. You could also create an extension method for string that allows you to specify a format and/or parse syntax like this.
How about using the T4 templating engine?
http://visualstudiomagazine.com/articles/2009/05/01/visual-studios-t4-code-generation.aspx
If you are just trying to avoid concatenation of immutable strings, what you're looking for is StringBuilder.
Usage:
string parameterName = "Example";
int parameterValue = 1;
Stringbuilder builder = new StringBuilder();
builder.Append("The output parameter ");
builder.Append(parameterName);
builder.Append("'s value is ");
builder.Append(parameterValue.ToString());
string totalExample = builder.ToString();
Since C# 6.0 you can write string "The title is \{title}" which does exactly what you need.
you can use something like this as mentioned in C# documentation.
string interpolation
string name = "Horace";
int age = 34;
Console.WriteLine($"He asked, \"Is your name {name}?\", but didn't wait for a reply :-{{");
Console.WriteLine($"{name} is {age} year{(age == 1 ? "" : "s")} old.");
Or combined:
Console.WriteLine("The date is {0}", DateTime.Now);
Extra info (in response to BrandonZeider):
Yep, it is kind-a important for people to realize that string conversion is automatically done. Manually adding ToString is broken, e.g.:
string value = null;
Console.WriteLine("The value is '{0}'", value); // OK
Console.WriteLine("The value is '{0}'", value.ToString()); // FAILURE
Also, this becomes a lot less trivial once you realize that the stringification is not equivalent to using .ToString(). You can have format specifiers, and even custom format format providers... It is interesting enough to teach people to leverage String.Format instead of doing it manually.

c# string formatting

I m curious why would i use string formatting while i can use concatenation such as
Console.WriteLine("Hello {0} !", name);
Console.WriteLine("Hello "+ name + " !");
Why to prefer the first one over second?
You picked too simple of an example.
String formatting:
allows you to use the same variable multiple times: ("{0} + {0} = {1}", x, 2*x)
automatically calls ToString on its arguments: ("{0}: {1}", someKeyObj, someValueObj)
allows you to specify formatting: ("The value will be {0:3N} (or {1:P}) on {2:MMMM yyyy gg}", x, y, theDate)
allows you to set padding easily: (">{0,3}<", "hi"); // ">hi <"
You can trade the string for a dynamic string later.
For example:
// In a land far, far away
string nameFormat = "{0} {1}";
// In our function
string firstName = "John";
string lastName = "Doe";
Console.WriteLine(nameFormat, firstName, lastName);
Here, you can change nameFormat to e.g. "{1}, {0}" and you don't need to change any of your other code. With concatination, you would need to edit your code or possibly duplicate your code to handle both cases.
This is useful in localization/internationalization.
There isn't a singular correct answer to this question. There are a few issues you want to address:
Performance
The performance differences in your examples (and in real apps) are minimal. If you start writing MANY concatenations, you will gradually see better memory performance with the formatted string. Refer to Ben's answer
Readability
You will be better off with a formatted string when you have formatting, or have many different variables to stringify:
string formatString = "Hello {0}, today is {1:yyyy-MM-dd}";
Console.WriteLine(formatString, userName, Date.Today);
Extensibility
Your situation will determine what's best. You tell me which is better when you need to add an item between Username and Time in the log:
Console.WriteLine(
#"Error!
Username: " + userName + "
Time: " + time.ToString("HH:mm:ss") + "
Function: " + functionName + "
Action: " + actionName + "
status: " + status + "
---");
or
Console.WriteLine(#"Error!
Username: {0}
Time: {1}
Function: {2}
Action: {3}
status: {4}
---",
username, time.ToString("HH:mm:ss"), functionName, actionName, status);
Conclusion
I would choose the formatted string most of the time... But I wouldn't hesitate at all to use concatenation when it was easier.
I think the main thing here is readability. So I always choose for what have the best readability for each case.
Note:
With string interpolation of C# 6 your code could be simplified to this:
Console.WriteLine($"Hello {name}!");
Which I think better than your two suggested options.
String formatting allows you to keep the format string separate, and use it where it's needed properly without having to worry about concatenation.
string greeting = "Hello {0}!";
Console.WriteLine(greeting, name);
As for why you would use it in the exact example you gave... force of habit, really.
I think a good example is about i18n and l10n
If you have to change a string between different languages, this: "bla "+variable+"bla bla.."
Will give problems to a program used to create sobstitution for your strings if you use a different language
while in this way: "bla {0} blabla" is easily convertible (you will get {0} as part of the string)
Formatting is usually preferred for most of the reasons explained by other members here. There are couple more reasons I want to throw in from my short programming experience:
Formatting will help in generating Culture aware strings.
Formatting is more performant than concatenation. Remember, every concatenation operation will involve creation of temporary intermediate strings. If you have a bunch of strings you need to concatenate, you are better off using String.Join or StringBuilder.
You are using a trivial example where there is not much of a difference. However, if you have a long string with many parameters it is much nicer to be able to use a format string instead of many, many + signs and line breaks.
It also allows you to format numerical data as you wish, i.e., currency, dates, etc.
The first format is recommended. It allows you to specify specific formats for other types, like displaying a hex value, or displaying some specific string format.
e.g.
string displayInHex = String.Format("{0,10:X}", value); // to display in hex
It is also more consistent. You can use the same convention to display your Debug statement.
e.g.
Debug.WriteLine (String.Format("{0,10:X}", value));
Last but not least, it helps in the localisation of your program.
In addition to reasons like Ignacio's, many (including me) find String.Format-based code much easier to read and alter.
string x = String.Format(
"This file was last modified on {0} at {1} by {2}, and was last backed up {3}."
date, time, user, backupDate);
vs.
string x = "This file was last modified on " + date + " at "
+ time + " by " + user + " and was last backed up " + backupDate + ".";
I have found the former approach (using string.format) very useful when overriding ToString() methods in Entity classes.
For example, in my Product class;
public override string ToString()
{
return string.format("{0} : {1} ({2} / {3} / {4}",
this.id,
this.description,
this.brand,
this.model);
}
Then, when users decide they want the product to appear differently it's easy to change the order/contents or layout of the string that is returned.
Of course you still concatentate this string together but I feel string.Format makes the whole thing a bit more readable and maintainable.
I guess that's the short answer I'm giving then isn't it - readability and maintainability for lengthy or complex strings.
Since C# 6 release a few years ago, it is also possible to perform string interpolation. Per example this:
var n = 12.5847;
System.Console.WriteLine(string.Format("Hello World! {0:C2}", n));
Becomes that:
var n = 12.5847;
System.Console.WriteLine($"Hello World! {n:C2}");
And both give you this result:
Hello World! £12.58

Formatting my string

How can I format a string like this:
string X = "'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}'",????
I remember I used to be able to put a comma at the end and specify the actual data to assign to {0},{1}, etc.
Any help?
Use string.Format method such as in:
string X = string.Format("'{0}','{1}','{2}'", foo, bar, baz);
An alternative is to use Join, if you have the values in a string array:
string x = "'" + String.Join("','", valueArray) + "'";
(Just wanted to be different from the 89724362 users who will show you how to use String.Format... ;)
String.Format("'{0}', '{1}'", arg0, arg1);
The String.Format method accepts a format string followed by one to many variables that are to be formatted. The format string consists of placeholders, which are essentially locations to place the value of the variables you pass into the function.
Console.WriteLine(String.Format("{0}, {1}, {2}", var1, var2, var3));
Your question is a bit vague, but do you mean:
// declare and set variables val1 and val2 up here somewhere
string X = string.Format("'{0}','{1}'", val1, val2);
Or are you asking for something else?
use string.format, and put individual format specifiers inside the braces, after the number and a colon, as in
string s = string.Format(
" {0:d MMM yyyy} --- {1:000} --- {2:#,##0.0} -- {3:f}",
DateTime.Now, 1, 12345.678, 3e-6);
and, as you can see from the example, you don;t need the single quotes to delineate literals, anything not inside braces will be output literally
are you looking for:
String.Format("String with data {0}, {1} I wish to format", "Foo", "Bar");
would result in
"String with data Foo, Bar I wish to format"
Use string.Format as in
var output = string.Format("'{0}', '{1}'", x, y);

String has how many parameters

Before using String.Format to format a string in C#, I would like to know how many parameters does that string accept?
For eg. if the string was "{0} is not the same as {1}", I would like to know that this string accepts two parameters
For eg. if the string was "{0} is not the same as {1} and {2}", the string accepts 3 parameters
How can I find this efficiently?
String.Format receives a string argument with format value, and an params object[] array, which can deal with an arbitrary large value items.
For every object value, it's .ToString() method will be called to resolve that format pattern
EDIT: Seems I misread your question. If you want to know how many arguments are required to your format, you can discover that by using a regular expression:
string pattern = "{0} {1:00} {{2}}, Failure: {0}{{{1}}}, Failure: {0} ({0})";
int count = Regex.Matches(Regex.Replace(pattern,
#"(\{{2}|\}{2})", ""), // removes escaped curly brackets
#"\{\d+(?:\:?[^}]*)\}").Count; // returns 6
As Benjamin noted in comments, maybe you do need to know number of different references. If you don't using Linq, here you go:
int count = Regex.Matches(Regex.Replace(pattern,
#"(\{{2}|\}{2})", ""), // removes escaped curly brackets
#"\{(\d+)(?:\:?[^}]*)\}").OfType<Match>()
.SelectMany(match => match.Groups.OfType<Group>().Skip(1))
.Select(index => Int32.Parse(index.Value))
.Max() + 1; // returns 2
This also address #280Z28 last problem spotted.
Edit by 280Z28: This will not validate the input, but for any valid input will give the correct answer:
int count2 =
Regex.Matches(
pattern.Replace("{{", string.Empty),
#"\{(\d+)")
.OfType<Match>()
.Select(match => int.Parse(match.Groups[1].Value))
.Union(Enumerable.Repeat(-1, 1))
.Max() + 1;
You'll have to parse through the string and find the highest integer value between the {}'s...then add one.
...or count the number of sets of {}'s.
Either way, it's ugly. I'd be interested to know why you need to be able to figure out this number programatically.
EDIT
As 280Z28 mentioned, you'll have to account for the various idiosyncrasies of what can be included between the {}'s (multiple {}'s, formatting strings, etc.).
I rely on ReSharper to analyze that for me, and it is a pity that Visual Studio does not ship with such a neat feature.

Categories