Click here to Skip to main content
15,920,030 members
Home / Discussions / C#
   

C#

 
GeneralRe: closing a .exe from within a seperate .exe Pin
Nick Parker2-Jan-03 9:06
protectorNick Parker2-Jan-03 9:06 
QuestionRestricted access to properties? Pin
Nnamdi Onyeyiri2-Jan-03 7:01
Nnamdi Onyeyiri2-Jan-03 7:01 
AnswerRe: Restricted access to properties? Pin
James T. Johnson2-Jan-03 9:35
James T. Johnson2-Jan-03 9:35 
GeneralRe: Restricted access to properties? Pin
Nnamdi Onyeyiri2-Jan-03 11:55
Nnamdi Onyeyiri2-Jan-03 11:55 
Generalmessage box stuff Pin
jtmtv182-Jan-03 6:59
jtmtv182-Jan-03 6:59 
GeneralRe: message box stuff Pin
Ray Cassick2-Jan-03 7:55
Ray Cassick2-Jan-03 7:55 
GeneralRe: message box stuff Pin
Brian Olej2-Jan-03 14:23
Brian Olej2-Jan-03 14:23 
GeneralArray Serialization - kinda urgent Pin
Mr BallyDaHob2-Jan-03 5:18
Mr BallyDaHob2-Jan-03 5:18 
Hi All,
I'm stuck with a problem serializing and de-serializing an array. I'm hoping somebody will be able to help me with. I'm trying to serialize an array of my custom class Query to a file. But when I check the file, I can only see one query in there. And when I try to de-serialize it I get an error saying Object doesn't implement IConvertable.
Does anyone have any idea what I am doing wrong? I've put the code below, maybe yee'd like to compile it and check what’s going wrong?
Thanks,
Donal

using System;<br />
using System.IO;<br />
using System.Runtime.Serialization;<br />
using System.Runtime.Serialization.Formatters.Soap;<br />
using System.Collections;<br />
<br />
<br />
<br />
namespace ProjQueryManager<br />
{<br />
	/// <summary><br />
	/// Summary description for Class1.<br />
	/// </summary><br />
	class QueryManager<br />
	{<br />
		public QueryList qList;<br />
		string sourceFile = @"C:\source1.dta";<br />
<br />
		public QueryManager()<br />
		{<br />
			qList = new QueryList();<br />
		}<br />
		<br />
		public bool CheckQuerySourceFile()<br />
		{<br />
			return true;<br />
		}<br />
<br />
		public void addQuery(Query QueryInfo)<br />
		{<br />
			qList.Add(QueryInfo);<br />
		}<br />
		<br />
		public bool LoadFile()<br />
		{<br />
			FileStream inStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);<br />
			StreamReader sReader = new StreamReader(inStream);<br />
			SoapFormatter soapReader = new SoapFormatter();<br />
			if(qList != null)<br />
				qList = null;<br />
			qList = (QueryList) soapReader.Deserialize(inStream);<br />
			inStream.Close();<br />
<br />
			foreach(Query q in qList)<br />
				q.PrintInfo();<br />
			return true;<br />
		}<br />
<br />
<br />
		public bool SaveQuerys()<br />
		{<br />
			FileStream outStream = new FileStream(sourceFile, FileMode.OpenOrCreate, FileAccess.Write);<br />
			try<br />
			{<br />
				SoapFormatter soapWriter = new SoapFormatter();<br />
				soapWriter.Serialize(outStream, qList);<br />
				outStream.Close();<br />
			}<br />
			catch(SerializationException sem)<br />
			{<br />
				outStream.Close();<br />
				Console.WriteLine(sem.ToString());<br />
			}<br />
<br />
			return true;<br />
		}<br />
<br />
		public bool SaveQuerys2()<br />
		{<br />
			try<br />
			{<br />
				ArrayList temp = new ArrayList();<br />
				temp.Add("howdy");<br />
				temp.Add("partner");<br />
<br />
				FileStream outStream = new FileStream(sourceFile, FileMode.OpenOrCreate, FileAccess.Write);<br />
				SoapFormatter soapWriter = new SoapFormatter();<br />
				soapWriter.Serialize(outStream, temp);<br />
			}<br />
			catch(SerializationException sem)<br />
			{<br />
				Console.WriteLine(sem.ToString());<br />
			}<br />
<br />
			return true;<br />
		}<br />
<br />
<br />
<br />
		/// <summary><br />
		/// The main entry point for the application.<br />
		/// </summary><br />
		[STAThread]<br />
		static void Main(string[] args)<br />
		{<br />
			QueryManager myQM = new QueryManager();<br />
			Query qi = new Query();<br />
<br />
			qi.name = "Query 1";<br />
			qi.description = "The first Query";<br />
			qi.SQL = "Selecr dfg dfg fdsfdsg gfds";<br />
			myQM.addQuery(qi);<br />
<br />
			qi.name = "Query 2";<br />
			qi.description = "The second Query";<br />
			qi.SQL = "Selecr dfg dfg fdsfdsg gfds";<br />
			myQM.addQuery(qi);<br />
<br />
			qi.name = "Query 3";<br />
			qi.description = "The third Query";<br />
			qi.SQL = "Selecr dfg dfg fdsfdsg gfds";<br />
			myQM.addQuery(qi);<br />
<br />
			<br />
<br />
<br />
			myQM.SaveQuerys();<br />
<br />
			//myQM.LoadFile();<br />
		}<br />
	}<br />
}<br />
<br />
<br />
<br />
<br />
<br />
/// <summary><br />
	/// A struct for storing query information<br />
	/// </summary><br />
[Serializable]<br />
public class Query : ISerializable<br />
{<br />
	/// <summary><br />
	/// The name of the query.<br />
	/// </summary><br />
	public string name;<br />
	/// <summary><br />
	/// A description of the query.<br />
	/// </summary><br />
	public string description;<br />
	/// <summary><br />
	/// The query string.<br />
	/// </summary><br />
	public string SQL;<br />
<br />
	public Query()<br />
	{<br />
	}<br />
<br />
	/// <summary><br />
	/// Constructor<br />
	/// </summary><br />
	/// <param name="qname">The name of the query.</param><br />
	/// <param name="qdesc">A description of the query.</param><br />
	/// <param name="qSQL">The query string.</param><br />
	public Query(string qname, string qdesc, string qSQL)<br />
	{<br />
		name = qname;<br />
		description = qdesc;<br />
		SQL = qSQL;<br />
	}<br />
<br />
	/// <summary><br />
	/// De-serializes a query object<br />
	/// </summary><br />
	/// <param name="serInfo">Holds all the data needed to serialize or deserialize an object</param><br />
	/// <param name="streamContext">The destination for this serialization.</param></param><br />
	public Query(SerializationInfo serInfo, StreamingContext streamContext)<br />
	{<br />
		name = serInfo.GetString("name");<br />
		description = serInfo.GetString("description");<br />
		SQL = serInfo.GetString("SQL");<br />
	}<br />
		<br />
	/// <summary><br />
	/// Prints the query's attributes to the console.<br />
	/// </summary><br />
	public void PrintInfo()<br />
	{<br />
		Console.WriteLine("name: " + name);<br />
		Console.WriteLine("description: " + description);<br />
		Console.WriteLine("SQL: " + SQL);<br />
	}<br />
<br />
	/// <summary><br />
	/// Populates a SerializationInfo with the data needed to serialize the target object.<br />
	/// </summary><br />
	/// <param name="serInfo">The SerializationInfo to populate with data. </param><br />
	/// <param name="streamContext">The destination for this serialization. </param><br />
	public void GetObjectData(SerializationInfo serInfo, StreamingContext streamContext)<br />
	{<br />
		serInfo.AddValue("name", name);<br />
		serInfo.AddValue("description", description);<br />
		serInfo.AddValue("SQL", SQL);<br />
	}<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
[Serializable()]<br />
public class QueryList : IEnumerable, ICollection, ISerializable<br />
{<br />
	protected ArrayList TheQueryList;<br />
		<br />
	/// <summary><br />
	/// Default constructor.<br />
	/// </summary><br />
	public QueryList()<br />
	{<br />
		TheQueryList = new ArrayList();<br />
	}<br />
		<br />
	/// <summary><br />
	/// Called when de-serializing a QueryList object.<br />
	/// </summary><br />
	/// <param name="serInfo">Holds all the data needed to serialize or deserialize an object</param><br />
	/// <param name="streamContext">The destination for this serialization.</param><br />
	public QueryList(SerializationInfo serInfo, StreamingContext streamContext)<br />
	{<br />
		try<br />
		{<br />
<br />
			ArrayList aL = new ArrayList();<br />
			TheQueryList = (ArrayList)serInfo.GetValue("TheQueryList", aL.GetType());<br />
		}<br />
		catch(InvalidCastException e)<br />
		{<br />
			Console.WriteLine("Line 265: " + e.Message + "\n \n \n");<br />
		}<br />
	}<br />
<br />
	/// <summary><br />
	/// Adds a Query object to the query list<br />
	/// </summary><br />
	public int Add(Query QueryInfo)<br />
	{<br />
		return TheQueryList.Add(QueryInfo);<br />
	}<br />
		<br />
	/// <summary><br />
	/// Populates a SerializationInfo with the data needed to serialize the target object.<br />
	/// </summary><br />
	/// <param name="serInfo">The SerializationInfo to populate with data. </param><br />
	/// <param name="streamContext">The destination for this serialization. </param><br />
	public void GetObjectData(SerializationInfo serInfo, StreamingContext streamContext)<br />
	{<br />
		//serInfo.AddValue("TheQueryList", TheQueryList);<br />
		serInfo.AddValue("TheQueryList", TheQueryList.GetType());<br />
	}<br />
		<br />
	/// <summary><br />
	/// Returns an Query object<br />
	/// </summary><br />
	/// <param name="index">The index of the required Query object</param><br />
	/// <returns>null if the index is out of range, otherwise the required Query</returns><br />
	public Query getQueryAt(int index)<br />
	{<br />
		if(TheQueryList.Count < index)<br />
			return (Query) TheQueryList[index];<br />
		else<br />
		{<br />
			Query temp = new Query("Index out of range", "null", "null");<br />
			return temp;<br />
		}<br />
	}<br />
	<br />
<br />
	/// <summary><br />
	/// Creates an object with the provided parameters and adds it to the Query List.<br />
	/// </summary><br />
	/// <param name="name">The name of the query.</param><br />
	/// <param name="description">A description of the query.</param><br />
	/// <param name="statement">The query string.</param><br />
	/// <returns>The index that the query was added to</returns><br />
	public int Add(string name, string description, string statement)<br />
	{<br />
		Query sQI = new Query(name, description, statement);<br />
		return TheQueryList.Add(sQI);<br />
	}<br />
<br />
<br />
<br />
	/// <summary><br />
	/// This Property is the number of elements in the Array list<br />
	/// </summary><br />
	//ICollection.Count<br />
	public int Count<br />
	{<br />
		get<br />
		{<br />
			return TheQueryList.Count;<br />
		}<br />
	}<br />
		<br />
	//ICollection.IsSynchronized<br />
	public bool IsSynchronized<br />
	{<br />
		get<br />
		{<br />
			return TheQueryList.IsSynchronized;<br />
		}<br />
	}<br />
		<br />
	//ICollection.SyncRoot<br />
	public object SyncRoot<br />
	{<br />
		get<br />
		{<br />
			return TheQueryList.SyncRoot;<br />
		}<br />
	}<br />
<br />
	//ICollection.CopyTo<br />
	public void CopyTo(Array dest, int index)<br />
	{<br />
		TheQueryList.CopyTo(dest, index);<br />
	}<br />
<br />
	/// <summary><br />
	/// This Function clears all elements in the list<br />
	/// </summary><br />
	public void Clear()<br />
	{<br />
		TheQueryList.Clear();<br />
	}<br />
<br />
<br />
	//IEnumerable.GetEnumerator<br />
	public IEnumerator GetEnumerator()<br />
	{<br />
		return new QueryListEnumerator(TheQueryList);<br />
	}<br />
<br />
	//IEnumerator Class<br />
	public class QueryListEnumerator : IEnumerator<br />
	{<br />
		ArrayList TheQueryList;<br />
		IEnumerator TheEnumerator;<br />
<br />
		public QueryListEnumerator(ArrayList QLT)<br />
		{<br />
			this.TheQueryList = QLT;<br />
			TheEnumerator = QLT.GetEnumerator();<br />
		}<br />
<br />
		public object Current<br />
		{<br />
			get<br />
			{<br />
				return TheEnumerator.Current;<br />
			}<br />
		}<br />
<br />
		public bool MoveNext()<br />
		{<br />
			return TheEnumerator.MoveNext();<br />
		}<br />
<br />
		public void Reset()<br />
		{<br />
		}<br />
	}<br />
<br />
}

GeneralQuestion about inherited/custom control.. Pin
Randy Williams2-Jan-03 5:08
Randy Williams2-Jan-03 5:08 
GeneralRe: Question about inherited/custom control.. Pin
leppie2-Jan-03 9:35
leppie2-Jan-03 9:35 
GeneralRe: Question about inherited/custom control.. Pin
Randy Williams2-Jan-03 9:41
Randy Williams2-Jan-03 9:41 
GeneralArchitecture Design Advice Pin
Bill West2-Jan-03 5:07
Bill West2-Jan-03 5:07 
GeneralRe: Architecture Design Advice Pin
Anonymous2-Jan-03 10:08
Anonymous2-Jan-03 10:08 
GeneralRe: Architecture Design Advice Pin
leppie2-Jan-03 11:37
leppie2-Jan-03 11:37 
GeneralDoes C# have anything like JAVA Applets Pin
Hudson2-Jan-03 4:35
Hudson2-Jan-03 4:35 
GeneralRe: Does C# have anything like JAVA Applets Pin
Michael P Butler2-Jan-03 4:43
Michael P Butler2-Jan-03 4:43 
GeneralRe: Does C# have anything like JAVA Applets Pin
James T. Johnson2-Jan-03 17:06
James T. Johnson2-Jan-03 17:06 
QuestionDoes something look goofy here? Pin
Nick Parker2-Jan-03 4:25
protectorNick Parker2-Jan-03 4:25 
AnswerRe: Does something look goofy here? Pin
Paul Riley2-Jan-03 4:55
Paul Riley2-Jan-03 4:55 
GeneralRe: Does something look goofy here? Pin
Nick Parker2-Jan-03 5:00
protectorNick Parker2-Jan-03 5:00 
AnswerRe: Does something look goofy here? Pin
Stephane Rodriguez.2-Jan-03 5:01
Stephane Rodriguez.2-Jan-03 5:01 
GeneralRe: Does something look goofy here? Pin
Nick Parker2-Jan-03 6:13
protectorNick Parker2-Jan-03 6:13 
GeneralRe: Does something look goofy here? Pin
Stephane Rodriguez.2-Jan-03 6:25
Stephane Rodriguez.2-Jan-03 6:25 
GeneralRe: Does something look goofy here? Pin
Nick Parker2-Jan-03 7:14
protectorNick Parker2-Jan-03 7:14 
AnswerRe: Does something look goofy here? Pin
James T. Johnson2-Jan-03 9:46
James T. Johnson2-Jan-03 9:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.