|
-
Dec 26th, 2003, 07:20 AM
#1
Thread Starter
Frenzied Member
what does {} do in .net? [RESOLVED]
I am looking at the indexof() and one of the examples shows this:
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, [end] - 1)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
what will the output be?
here's the full example:
Code:
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
Dim [end] As Integer
Dim count As Integer
[end] = str.Length
start = [end] / 2
Console.WriteLine()
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, [end] - 1)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("The string 'he' occurs at position(s): ")
count = 0
at = 0
While start <= [end] AndAlso at > - 1
' start+count must be a position within -str-.
count = [end] - start
at = str.IndexOf("he", start, count)
If at = - 1 Then
Exit While
End If
Console.Write("{0} ", at)
start = at + 1
End While
Console.WriteLine()
Last edited by Andy; Dec 29th, 2003 at 03:08 AM.
-
Dec 26th, 2003, 08:03 AM
#2
Sleep mode
It's just a variable in const string . Consider these examples (test them to understand what actually they do ) :
VB Code:
Private Sub ExampleOne()
Dim strng As String = "World"
Console.Write("Hello {0}", strng)
Console.Read()
End Sub
{0} , this means : include the variable indexed 0 in the paramters list passed to Write method .
Private Sub ExampleTwo()
Dim sring1 As String = "VB"
Dim string2 As String = ".NET"
Console.Write(" {0}{1} Forums", sring1, string2)
Console.Read()
End Sub
{0}{1} , this means : include the variables number 0 and 1 in the paramters list passed to Write method .
They are equal to this :
VB Code:
Private Sub ExampleOne()
Dim strng As String = "Hello World"
Console.Write(strng)
Console.Read()
End Sub
Private Sub ExampleTwo()
Console.Write("VB.NET Forums")
Console.Read()
End Sub
-
Dec 26th, 2003, 10:31 AM
#3
Thread Starter
Frenzied Member
thanks Pirate. I assumed it had something to do like that. I saw another example but it was a LOT harder to understand.
Code:
Console.WriteLine("Bill total:" + ControlChars.Tab + "{0,8:c}", billTotal)
I dont understand what the 0, 8:c represents.
another thing, are there any major differences from console.write and .writeline? They apear to do the same job.
-
Dec 26th, 2003, 11:06 AM
#4
Sleep mode
Originally posted by thephantom
thanks Pirate. I assumed it had something to do like that. I saw another example but it was a LOT harder to understand.
Code:
Console.WriteLine("Bill total:" + ControlChars.Tab + "{0,8:c}", billTotal)
I dont understand what the 0, 8:c represents.
another thing, are there any major differences from console.write and .writeline? They apear to do the same job.
WriteLine : It writes to the console and stop in the next new line .
Write : Writes to the console and stop where the last character was typed . Consider this example (call one of them at a time )
VB Code:
Private Sub WriteLineTest()
Console.WriteLine("This uses WriteLine Method")
Console.Read()
End Sub
Private Sub WriteTest()
Console.Write("This uses Write Method")
Console.Read()
End Sub
-
Dec 26th, 2003, 11:14 AM
#5
Thread Starter
Frenzied Member
oh ok, that makes sense. Thanks alot!!
what about the 8:c part?
-
Dec 26th, 2003, 11:25 AM
#6
Sleep mode
Originally posted by thephantom
what about the 8:c part?
I don't know . What's the output anyway ?
-
Dec 26th, 2003, 11:39 AM
#7
Thread Starter
Frenzied Member
well, don't really know. I had a bit of trouble figuring out how to place the code in my "test" project.
Code:
Public Class TipCalculator
Private Const tipRate As Double = 0.18
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Function Main(args() As String) As Integer
Dim billTotal As Double
If args.Length < 2 Then
Console.WriteLine("usage: TIPCALC total")
Return 1
Else
Try
billTotal = Double.Parse(args(1))
Catch
Console.WriteLine("usage: TIPCALC total")
Return 1
End Try
Dim tip As Double = billTotal * tipRate
Console.WriteLine()
Console.WriteLine("Bill total:" + ControlChars.Tab + "{0,8:c}", billTotal)
Console.WriteLine("Tip total/rate:" + ControlChars.Tab + "{0,8:c} ({1:p1})", tip, tipRate)
Console.WriteLine("".PadRight(24, "-"c))
Console.WriteLine("Grand total:" + ControlChars.Tab + "{0,8:c}", billTotal + tip)
Return 0
End If
End Function 'Main
End Class 'TipCalculator
that's what is in the msdn. maybe you can make heads and tails of it?
-
Dec 26th, 2003, 12:03 PM
#8
Frenzied Member
where did you see {0,8:c} used???
I've always seen it like this (C# Code)
VB Code:
double billTotal = 15.56;
Console.WriteLine("Bill Total: {0:c}", billTotal);
The {0:c} takes the variable billTotal and formats it as currency (c)
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Dec 26th, 2003, 12:49 PM
#9
Frenzied Member
This is all string formatting. When you have {0,8:c}, it's saying this is the 0 index (parameter specifier). The ,8 is the alignment component, and the c (as already stated) is the format string component.
The alignment component allows one to specify the preferred width of the field.
Search MSDN for "Composite Formatting"
-
Dec 26th, 2003, 06:46 PM
#10
Thread Starter
Frenzied Member
Thanks mike! the msdn is great WHEN you know what to look up!! 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|