It is currently Thu Mar 28, 2024 12:32 pm



Reply to topic  [ 3 posts ] 
c# code snippets 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 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
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
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
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
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
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 7 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

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.