On Fri, 20 May 2005 06:50:33 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Oberon <Ob****@solstice.com> wrote: My HashTable (Global.Games) is a static collection of objects of type
Game. A Game object has 8 fields (exposed as properties). The key to
the HashTable is also one of these fields (GameID, of type int).
When I try to create a SortedList from the HashTable with the
following:
SortedList sortedGames = new SortedList(Global.Games);
I get an error message:
Argument '1': cannot convert from 'ChatMark1.GameHashTable' to 'int'
But the SortedList constructor is overloaded and one parameter it
accepts is an object of type Systems.Collection.IDictionary. Why does
it now expect the Global.Games to be of type 'int'?
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
I can't do that. I have to post two complete pages. This is the
'simplified' code for the problem, below.
The program fails on this line:
SortedList sortedGames = new SortedList(Global.Games);
with the error messages:
Argument '1': cannot convert from 'SortHash.GameHashTable' to
'int'
The best overloaded method match for
'System.Collections.SortedList.SortedList(int)' has some invalid
arguments
Could not find any attribute 'disabled' of element 'CheckBox'.
(The 3rd (other) error message is not important since the program
works even if the offending disabled attribute is left in the code
when the line creating the SortedList is commentated out.
I can see what I'm doing wrong now. I'm giving the wrong parameters to
the new SortedList. The problem is that I don't actually understand
what parameters it will take here. I was using an example from some
book code, but perhaps the constructors for the sorted list have
changed since then?, or maybe the code only works with a simple
HashTable, not my more complex one?
Would I be better off leaving the HashTable out completely and just
putting my recordset in the Application Cache? I think so. That is
what I will try to do to solve the problem as I'm probably out of my
depth here.
+ + + + + + + + + + + + + + + + + + + + +
Here is my web page.
+ + + + + + + + + + + + + + + + + + + + +
<%@ Page language="c#" Codebehind="Default.aspx.cs"
AutoEventWireup="false" Inherits="SortHash._Default1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Default</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgGames" runat="server"
AutoGenerateColumns="False" EnableViewState="False"
AllowSorting="True" OnSortCommand="SortRows">
<Columns>
<asp:BoundColumn DataField="Title" HeaderText="Title"
SortExpression="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="PlayTime" HeaderText="Day"
DataFormatString="{0:ddd}"></asp:BoundColumn>
<asp:BoundColumn DataField="PlayTime" HeaderText="Time"
DataFormatString="{0:HH':'mm':' 'GMT'}"></asp:BoundColumn>
<asp:BoundColumn DataField="PlayTime" HeaderText="Start"
SortExpression="Start" DataFormatString="{0:m}"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Recruiting">
<ItemTemplate>
<asp:CheckBox id="chkRecruiting" runat="server"
Checked='<%# DataBinder.Eval(Container, "DataItem.Recruiting") %>'
disabled />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></form>
</body>
</HTML>
+ + + + + + + + + + + + + + + + + + + + +
Here is the code behind for the above page:
+ + + + + + + + + + + + + + + + + + + + +
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace SortHash
{
public class _Default1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgGames;
private void Page_Load(object sender, System.EventArgs e)
{
dgGames.DataSource = Global.Games;
dgGames.DataBind();
}
public void SortRows(object sender, DataGridSortCommandEventArgs
e)
{
SortedList sortedGames = new SortedList(Global.Games);
dgGames.DataSource = Global.Games;
dgGames.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
public class GameHashTable : IEnumerable
{
Hashtable _games;
public GameHashTable()
{
_games = new Hashtable();
}
public IEnumerator GetEnumerator()
{
return _games.Values.GetEnumerator();
}
public Game this[int GameID]
{
get {return (Game) _games[GameID];}
set {Add(value);}
}
public void Add(Game Item)
{
if (Item == null)
throw new ArgumentException("Game can not be null");
_games.Add(Item.GameID, Item);
}
public void Remove(Game Item)
{
_games.Remove(Item.GameID);
}
}
public class Game
{
private int _gameID;
private string _title;
private bool _recruiting;
private DateTime _playTime;
public Game(
int initialGameID,
string initialTitle,
bool initialRecruiting,
DateTime initialPlayTime)
{
GameID = initialGameID;
Title = initialTitle;
Recruiting = initialRecruiting;
PlayTime = initialPlayTime;
}
public int GameID
{
get { return _gameID; }
set {_gameID = value;}
}
public string Title
{
get {return _title;}
set {_title = value;}
}
public bool Recruiting
{
get {return _recruiting;}
set {_recruiting = value;}
}
public DateTime PlayTime
{
get {return _playTime;}
set {_playTime = value;}
}
}
}
+ + + + + + + + + + + + + + + + + + + + +
Here is my Global.asax
+ + + + + + + + + + + + + + + + + + + + +
using System;
using System.Collections;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
namespace SortHash
{
public class Global : System.Web.HttpApplication
{
public static string gameConnection = "packet size=4096;user
id=ape;pwd=monkey;data source=ASROCK;persist security
info=False;initial catalog=ChatMark1";
public static GameHashTable games;
private System.ComponentModel.IContainer components = null;
public Global()
{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
{
Global.GameLoad();
}
public static string GameConnection
{ get { return gameConnection;} }
public static void GameLoad()
{
SqlConnection connGames = new SqlConnection(gameConnection);
connGames.Open();
SqlCommand cmd = new SqlCommand("SELECT gameID, title,
recruiting, playTime FROM Games", connGames);
SqlDataReader reader;
reader = cmd.ExecuteReader();
Game game;
games = new GameHashTable();
while (reader.Read())
{
game = new Game(
reader.GetInt32(0),
reader.GetString(1),
reader.GetBoolean(2),
reader.GetDateTime(3));
games.Add(game);
}
reader.Close();
connGames.Close();
}
public static GameHashTable Games
{
get { return games; }
}
#region Web Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}
+ + + + + + + + + + + + + + + + + + + + +
Last. My database table:
+ + + + + + + + + + + + + + + + + + + + +
CREATE TABLE Games (
gameID int IDENTITY (1, 1) NOT NULL ,
title varchar (50) NOT NULL ,
recruiting bit NOT NULL CONSTRAINT DF_Games_recruiting DEFAULT
(1),
playTime datetime NOT NULL CONSTRAINT DF_Games_playTime DEFAULT
(getdate()),
CONSTRAINT PK_Games PRIMARY KEY CLUSTERED (gameID) ON PRIMARY
)