|
It is currently Sat Apr 27, 2024 10:41 am
|
|
Page 1 of 1
|
[ 3 posts ] |
|
Author |
Message |
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16650 Location: On a slope
|
c# code snippets
Ok, so I have one for javascript, now I'm making one for c# (in an asp.net context).
how to tell if a number is even or odd
[php]
if ((i & 1) != 1) //if odd
{
rowclass = " class='shaded'";
}
or
if ((i & 1) == 1) //if even
{
rowclass = " class='shaded'";
}
[/php]
multidimensional array initialization
I always forget this, it's just different enough from php to confuse me
[php]
string [,] varname= new string[2,3];
or
string[,] varname= new string[,]{
{ "whatever","things","doh" },
{ "","spacer","" },
{ "things","radioheader","" },
}
[/php]
and a quick how to iterate through it
[php]for (int i = 0; i < (varname.Length / 3); i++)
{
returnstring += varname[i, 0] + " " + varname[i, 1] + " " + varname[i, 2] + "<br>";
}[/php]
I'm sure I'll have more later
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
|
Thu Apr 19, 2007 7:26 am |
|
|
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16650 Location: On a slope
|
some more stuff....
getting the current executing page
[php]
Path.GetFileName(Request.FilePath)
[/php]
do a page redirect
[php]
Response.Redirect("login.aspx", true);
//true means to stop script execution.
//otherwise script execution of the current page continues
[/php]
setting cookies
[php]
//set cookies
HttpCookie usernameCookie = new HttpCookie("username");
HttpCookie passwordCookie = new HttpCookie("password");
usernameCookie.Value = this.Username;
passwordCookie.Value = this.EncryptedPassword;
usernameCookie.Expires = System.DateTime.Now.AddHours(cookietime);
passwordCookie.Expires = System.DateTime.Now.AddHours(cookietime);
HttpContext.Current.Response.Cookies.Add(usernameCookie);
HttpContext.Current.Response.Cookies.Add(passwordCookie);
[/php]
killing cookies
[php]
HttpCookie username_cookie = HttpContext.Current.Request.Cookies["username"];
HttpCookie password_cookie = HttpContext.Current.Request.Cookies["password"];
try
{
username_cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(username_cookie);
}
catch{ }
try
{
password_cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(password_cookie);
}
catch { }
[/php]
sessions
[php]
//set sessions
Session["username"] = this.Username;
Session["password"] = this.EncryptedPassword;
Response.Write(Session["username"]);
Response.Write(Session["password"]);
//kill sessions
Session.Abandon();
[/php]
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
|
Tue Jul 24, 2007 12:47 pm |
|
|
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16650 Location: On a slope
|
building a multidimensional array from a database. Multidimensional arrays are one of c#'s major weaknesses and probably the biggest thing I don't like about it.
*edit* ok, the non breaking space thing is really annoying
[php]
string select = @"SELECT count(*)
FROM mytable
WHERE whatever > 0";
doQuery(select); //this is a method I built that sets an OleDbDataReader object with the query results.
dr.Read();
int numItems = Convert.ToInt16(dr.GetValue(0));
dr.Dispose();
conn.Close();
//create the array ... I made it a string... no mixed arrays without using some list type or something
int num_fields = 5;
string[,] multidimensionalArray = new string[numItems, num_fields];
select = @"SELECT one, two, three, four, five
FROM mytable
WHERE whatever > 0";
doQuery(select);
int i = 0;
while (dr.Read())
{
//populate the array
for (int k = 0; k < num_fields; k++)
{
multidimensionalArray[i, k] = Convert.ToString(dr.GetValue(k));
}
}
dr.Dispose();
conn.Close();
//you now have the array. You can iterate through it similarly as setting it
Response.Write("<table>");
for (i = 0; i < numItems; i++)
{
Response.Write("<tr>");
for (int k = 0; k < num_fields; k++)
{
Response.Write("<td>" + multidimensionalArray[i, k] + "</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
//one gotcha. ... multidimensionalArray.Length() does not give you the number of rows,
//it gives you the number of elements, so you HAVE to know how many elements / row.
[/php]
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
|
Wed Jul 25, 2007 6:51 am |
|
|
|
Page 1 of 1
|
[ 3 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 44 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|