Category Archives: Programming

C# Attributes, Using statement, and Properties

More adventures in C#!
There are a few new concepts I learned recently while working. As the title of this entry suggests, they are attributes, the using statement, and properties. While all three were new to me, they’re very logical (as one would expect with anything dealing with programming).
Attributes allow you to assign metadata to your code. An attribute goes directly before the data it describes. It’s encased in square brackets and is of the form Key(Value). For instance, if I want to describe who wrote a particular class and when, I could put // Peter Anargirou, 7/9/2008. However, that isn’t very structured. Instead I could put [Author(“Peter Anargirou”),Date(“7/9/2008”)] directly preceding the class declarations. Attributes can be programmatically referenced by the program in which they exist, making them very useful for code reflection.
The using statement is also very interesting. It’s of the format using ( object declarations here ) { …code…}. Anything declared in the using declaration is deallocated at the end of the using block. For example, you can open a file in the using declaration and not have to worry about closing it. When execution leaves the using block, the file is closed automatically. It’s similar to a try/catch/finally.
Lastly, properties are something new I recently discovered. They’re similar to accessor methods in other languages. Normally, if I have an int that I want to be able to access from outside the class it’s in, MyClass, I make it public. If I do that, then I might accidentally overwrite it when I that’s not what I intend to do. Also, there’s no way to check what I’m doing with it. For instance, I might not want it to store a value of 0, but with a public int, I have no way of enforcing that. Instead, I could make a private int and have accessor methods. I might have private int count, public int getCount(), and public void updateCount(int newCount). This is definitely the preferred way of doing things, but now when I want to retrieve the count from outside, I always have to say MyClass myClass = new MyClass(); int x = myClass.getCount();. To update the count, I have to use myClass.updateCount(42);. With properties, you can create accessor methods that invoke automatically while you treat the property like a public variable. For example, I now write

private int myCount;
public int count
{
get { return myCount; }
set { if ( value != 0) myCount = value; }
}

Now from outside I can simply write x = myClass.count to retrieve the value and myClass.count = 42 to update the value. Of course, it’s also checking that I’m not updating with 0. If I try to update with 0, it won’t actually update myCount. It’s clean and simple yet still provides the more complex checking that can be done with accessor methods. Very handy!

HTML Form Combo Box

I found an interesting article on how to create a Combo Box in DHTML. Form controls are definitely in need of the combo box, but I’m not so sure this does the job all that well.
http://www.tgreer.com/comboArticle.html.
A combo box is a drop down select list that can also allow the user to input his or her own strings. The author of the article builds one in DHTML by first placing a select list on the page and then placing a textbox on top of it using CSS. He then causes the dropdown list to make a Javascript function call, causing the textbox to update with the value of the list’s selection. It’s interesting in concept, but you can tell something strange is happening. Looking at the page, you can tell there’s a rendering error (in this case not an error) and that there are controls stacked on top of each other.

Getting Vertical Scroll Position in a Div to Persist Across Postbacks

I’ve been working on this for a little bit now, and it was surprisingly easy. I just didn’t know all of the syntax.
First, any element in a webpage that scrolls has a scrollTop attribute that’s equal to the number of pixels it has been scrolled. When it’s scrolled all the way to the top, scrollTop is zero.
To cause this to persist, you can store it in a hidden input.
<input type=”hidden” name=”whatever” id=”whatever” value=”default” runat=”server”>
Name and id should be equal, and making sure runat=”server” is present is important. Now this should persist automatically. The only other thing that’s needed is to store and restore the value. You can use an onscroll=”nameOfJavascriptFunctionThatStoresThePosition()” attribute in the div in question to call a function whenever the div is scrolled. Then in your Javascript section, set window.onload equal to whatever function is going to restore it. Parentheses aren’t needed at the end because you’re mapping onload to a function.
The last thing to remember is that in your storing and restoring functions, you can’t simply refer to the id of the hidden input because your codebehind might, and most likely will, change it. You’d have to use <%=idOfInput.ClientID%>. You can use document.getElementById(‘ blah ‘).value, where blah is that previous tag. To restore the value that value should be set into the div’s scrollTop (again, you can use document.getElementById(), but you won’t need to do ClientID stuff because the div’s not runat=”server”. To store the value, you just set the scrollTop into that hidden input using that same method. Pretty simple!
Of course, if you’re not concerned with a div but rather the whole document, you can do a similar thing using body’s scrollTop or just enable SmartNavigation if the page is being designed for InternetExplorer.

Kalah

Back mid-quarter, we had to create the AI for a Kalah playing program. Jacobson then had them play against each other and ranked the results on the course website. Here’s how he did it, copied from his document.

Each player’s score is the sum, across all games played, of beans in the player’s pot at each game’s end. The higher your score, the better you did.
Each (non-disqualified) player played two games, one as top player and one as bottom player, against every other (non-disqualified) player. Players were disqualified for taking longer than 5 seconds to make a move, making an illegal move, throwing unchecked exceptions at run-time or compile-time errors. We did not disqualify a player whose program wrote to the screen, though technically we could have done so.

And here’s the results ordered by high score rather than player, and with the half or so disqualified scores removed, with mine bolded (and gem right under me). I didn’t care about the formatting, but notice our positions when ranked by score 🙂
Player Score Record as Bottom Record as Top
———————————————————————–
5700 2197 20- 1- 0 18- 3- 0
xxxx 2191 20- 1- 0 19- 2- 0
xxxx 2187 20- 1- 0 19- 2- 0
xxxx 2184 20- 1- 0 18- 3- 0
xxxx 1935 15- 6- 0 18- 3- 0
xxxx 1920 15- 6- 0 15- 6- 0
xxxx 1752 13- 8- 0 11- 9- 1
xxxx 1682 12- 9- 0 14- 7- 0
xxxx 1575 10- 10- 1 13- 8- 0
xxxx 1369 10- 11- 0 7- 12- 2
xxxx 1342 9- 11- 1 7- 14- 0
xxxx 1304 7- 14- 0 7- 14- 0
xxxx 1300 12- 8- 1 4- 17- 0
xxxx 1271 11- 10- 0 1- 19- 1
xxxx 1210 11- 10- 0 3- 17- 1
xxxx 1194 9- 12- 0 2- 19- 0
xxxx 1180 10- 11- 0 5- 16- 0
xxxx 1167 3- 17- 1 7- 13- 1
xxxx 1152 7- 14- 0 5- 16- 0
xxxx 1148 8- 12- 1 4- 17- 0
xxxx 1061 11- 9- 1 1- 20- 0
xxxx 943 4- 17- 0 1- 20- 0
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED
xxxx 0 DISQUALIFIED

Science Library Looks Like a Vagina

I heard that the Science Library looks like a vagina.
Anyways, I doubt if WebPageless will really come back anytime soon. I guess this is semi-permanent. Cox blocks all incoming ports entirely. It’s like my computer doesn’t exist on the Internet. In better news, Cox has at least made the speeds go up to normal DSL/cable speeds. I also tried using the AntWeb. To set that up, I had to learn how to VPN into the school. But after setting it up, I found that pages on it are only accessible to people on the UCI network.
I gave up the Start Menu and Explorer a long time ago (about a year now). But I had used Black Box menus or BBIcons. Well, I gave it all up in trade for Google Desktop Search. I works really well. I use it to launch all my applications and everything. Good program. Good organization, that Google. I loves them.
My Palm Tungsten C died completely today. The screen is totally fucked up. Like Chopsticks. That really sucks. I kinda depend on it. I’m gonna sync to my dad’s Palm to at least get my financing info off of it.
I got about a standard deviation above the mean on the CSE 21 midterm. Did well on both projects. My Kalah AI was the best in the class. The average on my CSE 31L midterm was 54%. I got 97%. I’ve aced all my 31L labs, and have done well in Classics. Turned in a concert report on Tuesday. It went well, I think.
Last weekend was cool. On Friday, gem and I went to LNP. I wore a robe for Halloween and got in early. I never had got in early before. Then we rented Chamber of Secrets and bought Harvest Moon: A Wonderful Life. Saturday we played video games basically. Then gem, Jon, Gabby, Andy and I went to see Nnenna Freelon at the Jazz Bakery in LA. I thought it was a pretty good show. Afterwards we came back to my apartment, drank, and watched the Grudge. Gabby was too scared to watch. She kept hiding. Patrick kept scaring her. It was great. Chris was too talkative during the movie. Andy was a bitch, and we’re never watching a movie that matters with him again. Sunday gem and I worked on our labs. Christine came over, but we didn’t really get to do much. That night, gem, Jon, Gabby, and I went to Knott’s Scary Farm. It was a lot of fun, except Jon and Gabby fought a bit. But Gabby got over her fear of the big rollercoasters – kind of. Anyways, she went on it. And we got on quickly because Jon used a wheelchair.
Harvest Moon: A Wonderful Life is a really fun game. I thought the chapters would suck, but they don’t really get in the way of playing.
I finished reading Starship Troopers again. It’s so much better now than when I read it in highschool or junior high. I love Heinlein. And I love that book. Totally awesome.
On Tuesday (I think) I hung out with gem, Allison, and Lauren. It was fun. Afterwards Lauren went home, and the three of us went to Denny’s to eat. Great fun! I love friends.
I hadn’t talked to Kalin in a long time, but I’m chatting with him now. Good ol’ AIM.
My mom’s birthday was the 1st, and Pete’s is the 3rd. Happy birthdays! gem and I went out to dinner with them tonight to an Italian restaurant. I had the rigotoni, and it was great.
gem’s making Gryffindor scarves for us for the Goblet of Fire release.
Tomorrow I have a history of jazz midterm at one. I feel so unprepared. It has nothing to do with computers or math. Ah! But I’ll probably do alright.

CSE 22 Project 5

Fucked up a little.
Correctness: 9
*) Your program is well done. It passed all the tests. The only thing is
that there are always more spaces than necessary in your output.
Style:10
+) Good quality!
+) Naming and comments are proper.
+) Program is neat and clean
+) Program is well organized.
+) You way to handle the ArrayList of RightHandSide is good: to generate a
new class which is the super class of Variable and Terminal. But you can use
generable also.
+) Good you tell the user that the output is finished.
+) Good you also considered exceptions

CSE 22 Project 4

Correctness: 10
+) Very well done. program passed all the tests!
Style: 10
+) Nice interface, correct and clear.
+) All operations are done in a proper class, i.e. within the scope of the
corresponding class.
+) Proper comments and proper naming.
+) Program is well organized!