Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Simple ASP.Net if statement in C#

    I feel really dumb for asking about this but I have a problem using if statements in ASP.Net because I'm going from client-side script to server-side so I'm used to using things like it is in JavaScript because I have a really strong ability in client-side development (CSS/JavaScript/HTML) and my book I learned about ASP.Net from has all the examples in Visual Basic which I completely hate so I decided to use C# because it's more like JavaScript. This is a simple one I tried thats not working that would work in JavaScript but not in ASP. The code should be self-explanatory of what I'm trying to do. Can somebody just show me how to rewrite this correctly so I can go on my way to developing actual applications please? I believe the way I'm rewriting the variable in the if statement is incorrect in C# as well, so also tell me if thats correct if you could, thank you.

    Code:
    <script language="C#" runat="server">
            string copyrightYear = DateTime.Now.Year.ToString();
            if (copyrightYear != "2012") {
                copyrightYear = "2012-" + DateTime.Now.Year.ToString();
            }
    </script>
                <div class="copyright" align="center">Copyright &copy; <% Response.Write(copyrightYear); %> SiteName.com<br />All Rights Reserved</div>

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,718
    Thanks
    41
    Thanked 191 Times in 190 Posts
    So it's not in a web form, but in a windows form- but you should be able to follow the logic...

    Code:
    namespace sample
    {
        public partial class Form1 : Form
        {
            private DateTime[] _testDates = new DateTime[4] { new DateTime(2001, 05, 16), new DateTime(2008, 09, 20), new DateTime(1995, 06, 03), new DateTime(1880, 02, 02) };  
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                richTextBox1.Text += string.Format("Copyright 2012-{0:yyyy}\n", DateTime.Now);
                foreach (DateTime _d in _testDates)
                {
                    richTextBox1.Text += string.Format("Copyright {0:yyyy}-2012. Current year is {1:yyyy}\n", _d, DateTime.Now);
                }
                
            }
        }
    }
    this outputs...
    Copyright 2012-2012
    Copyright 2001-2012. Current year is 2012
    Copyright 2008-2012. Current year is 2012
    Copyright 1995-2012. Current year is 2012
    Copyright 1880-2012. Current year is 2012
    you will want to check out these references...
    datetime
    datetime.now
    string.Format
    datetime and strings

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,718
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Sorry, I didn't really answer the "if" part....
    Altering this part of the code...
    Code:
    private DateTime[] _testDates = new DateTime[5] { new DateTime(2001, 05, 16), new DateTime(2008, 09, 20), new DateTime(1995, 06, 03), new DateTime(1880, 02, 02), DateTime.Now };
    /// and also...
    
    foreach (DateTime _d in _testDates)
                {
                    if (_d.Year != 2012)
                    {
                        richTextBox1.Text += string.Format("Copyright {0:yyyy}-2012. Current year is {1:yyyy}\n", _d, DateTime.Now);
                    }
                    else
                        richTextBox1.Text += "The years are equal.";
                }
    would then alter the output to
    Copyright 2012-2012
    Copyright 2001-2012. Current year is 2012
    Copyright 2008-2012. Current year is 2012
    Copyright 1995-2012. Current year is 2012
    Copyright 1880-2012. Current year is 2012
    The years are equal.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,133
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Actually, I think your problem is simply that the code in the <script runat="Server"> tag is running *AFTER* the <% Response.Write ... %> code!

    Try just changing it to
    Code:
    <%
            string copyrightYear = DateTime.Now.Year.ToString();
            if (copyrightYear != "2012") {
                copyrightYear = "2012-" + DateTime.Now.Year.ToString();
            }
    %>
            <div class="copyright" align="center">Copyright &copy; <%=copyrightYear%> SiteName.com<br />All Rights Reserved</div>
    IF that doesn't work, you can do it all inline:
    Code:
            <div class="copyright" align="center">Copyright &copy; 
                    <%= "2012" + (DateTime.Now.Year > 2012 ? "-" + DateTime.Now.Year : "" )%>
                    SiteName.com<br />All Rights Reserved</div>
    *******

    EDIT: I just tried BOTH those methods, and both worked.

    And, indeed, the problem is that the <script runat="server"> code executes *AFTER* the <%...%> code. You can easily prove that by setting breakpoints in the debugger.

    (I tested by using both 2012 and then 2011 as the test year values.)
    Last edited by Old Pedant; 06-22-2012 at 09:03 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,718
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Quote Originally Posted by Old Pedant View Post
    Actually, I think your problem is simply that the code in the <script runat="Server"> tag is running *AFTER* the <% Response.Write ... %> code!
    I didn't think of that :P I guess I was thinking too much on code-behind

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #6
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    using System;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    int number;

    Console.WriteLine("Please enter a number between 0 and 10:");
    number = int.Parse(Console.ReadLine());

    if(number > 10)
    Console.WriteLine("Hey! The number should be 10 or less!");
    else
    if(number < 0)
    Console.WriteLine("Hey! The number should be 0 or more!");
    else
    Console.WriteLine("Good job!");

    Console.ReadLine();
    }
    }
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •