Results 1 to 10 of 10

Thread: what does {} do in .net? [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    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.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It's just a variable in const string . Consider these examples (test them to understand what actually they do ) :

    VB Code:
    1. Private Sub ExampleOne()
    2.         Dim strng As String = "World"
    3.         Console.Write("Hello {0}", strng)
    4.         Console.Read()
    5.     End Sub
    6.  
    7. {0} , this means : include the variable indexed 0 in the paramters list passed to Write method .
    8.  
    9.     Private Sub ExampleTwo()
    10.         Dim sring1 As String = "VB"
    11.         Dim string2 As String = ".NET"
    12.         Console.Write(" {0}{1} Forums", sring1, string2)
    13.         Console.Read()
    14.     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:
    1. Private Sub ExampleOne()
    2.         Dim strng As String = "Hello World"
    3.         Console.Write(strng)
    4.         Console.Read()
    5.     End Sub
    6.  
    7.  
    8.     Private Sub ExampleTwo()
    9.         Console.Write("VB.NET Forums")
    10.         Console.Read()
    11.     End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    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.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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:
    1. Private Sub WriteLineTest()
    2.         Console.WriteLine("This uses WriteLine Method")
    3.         Console.Read()
    4.  
    5.     End Sub
    6.  
    7.     Private Sub WriteTest()
    8.         Console.Write("This uses Write Method")
    9.         Console.Read()
    10.     End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    oh ok, that makes sense. Thanks alot!!

    what about the 8:c part?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by thephantom
    what about the 8:c part?
    I don't know . What's the output anyway ?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    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?

  8. #8
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    where did you see {0,8:c} used???

    I've always seen it like this (C# Code)
    VB Code:
    1. double billTotal = 15.56;
    2.  
    3. 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

  9. #9
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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"

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    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
  •  



Click Here to Expand Forum to Full Width