Sort of. I have to create an online contract signing system for some
customers. I also need to make it to where the admin can edit the original
contract if something changes in it. One of the sections is a Definitions
section that gives all of the words and their definitions that are used in
the contract that might not be so obvious as to what they mean. I created
the xsd schema to define the contract layout since it will be saved as an
xml file. What I have to figure out, is how to save the Definitions and
Sections lists and content. They are the only 2 "list/array" properties in
the whole Contract class. Any ideas how to make this work? These 2 sections
have to be able to support unlimited entries.
Any ideas...? I have been stuck on this for almost a week now...
news:09ce40ed-8be9-454f-a7cb-0202f74208e7@x41g2000hsb.googlegroups.com...
I have this code:
public class Contract {
private Definition[] definitionField = new Definition[];
public Definition[] Definitions {
get {return value;}
set {definitionField=value;}
}
}
Then when I go to use it like this:
//create a contract
Contract Contract = new Contract();
//assign definitions to the definitions sections. These 2 lines create:
Error 2 Array creation must have array size or array initializer
Contract.Definitions[0].Word="Venue";
Contract.Definitions[0].Definition1="The place at which the event takes
place";
...
How do you make an array without having to define the array size?
Try using ArrayList instead, by the way are you trying to build a
dictionary?
Hi Andy
Don't you think that the Definitions property of the Contract class
should be indexed using the word for which the definition is required.
I think so because if you're going to have unlimited entries in the
Definitions property then finding definition for a particular word can
be very time consuming as you'll have to iterate over the array.
Anyways i'll write down the code to do this where you don't have to
type cast the object on retrieval.
Moreover, i figure out that you can have more than one definition for
a word but that is also now fixed, some word may have 2 defnitions,
some will have 5, etc. So its better that the Definition.Definition1
property should be implemented as an ArrayList i.e. a variable sized
array.
In the code below i've renamed the Definitions property of the
Contract class to ContractDictionary and the Definition.Definition1
property to WordDefinitions to remove any ambiguity. Please note that
there is still a class called Definition with two properties,
Definition.Word and Definition.WordDefinitions.
Here's the ContractDictionary class
using System;
using System.Collections;
using System.IO;
using System.ComponentModel;
namespace OnlineContractSigningSystem
{
public class ContractDictionary : DictionaryBase
{
// the object of this class contain the Definition ojbects in
a hashed
// list, hashed on the word to be defined.
// we can have an array of objects of this class to improve
the performance
// of the hashed list if it grows too large.
public ContractDictionary()
{
}
/// <summary>
/// Adds the Definition object to the hashed list if its not
already present
/// using the Add() method of the DictionaryBase class.
/// </summary>
/// <param name="newWord">Key for the hashed list.</param>
/// <param name="newElement">Definition ojbect to be used as
value</param>
/// <returns>void</returns>
public void Add(string newWord, Definition newElement)
{
if(!Dictionary.Contains(newWord)) {
Dictionary.Add(newWord, newElement);
}
else
{
// whatever you wish to do
}
}
/// <summary>
/// Removes a Definition object from the hashed list if its
present
/// using the Remove() method of the DictionaryBase class.
/// </summary>
/// <param name="oldWord">Key for the hashed list.</param>
/// <returns>void</returns>
public void Remove(string oldWord)
{
if (Dictionary.Contains(oldWord))
{
Dictionary.Remove(oldWord);
}
}
/// <summary>
/// Finds whether a word exists in the ContractDictionary or
not.
/// </summary>
/// <param name="sWord"></param>
/// <returns>bool</returns>
public bool Contains(string sWord)
{
return (Dictionary.Contains(sWord));
}
/// <summary>
/// Gets a collection of all the words.
/// </summary>
public ICollection Keys
{
get
{
return InnerHashtable.Keys;
}
}
/// <summary>
/// Gets or sets a Definition object in the hash table based
on the word
/// </summary>
/// <param name="sWord"></param>
/// <returns>Definition</returns>
public Definition this[string sWord]
{
get
{
return (Definition)Dictionary[sWord];
}
set
{
if(!Dictionary.Contains(sWord))
Dictionary[sWord] = value;
}
}
/// <summary>
/// Just to provide the foreach() statement support
/// </summary>
/// <returns>Definition</returns>
public new IEnumerator GetEnumerator()
{
foreach (object obj in Dictionary.Values)
{
yield return (Definition)obj;
}
}
}
}
Here's the WordDefinitions class
using System;
using System.Collections;
namespace OnlineContractSigningSystem
{
public class WordDefinitions : CollectionBase
{
public void Add(string newDef)
{
List.Add(newDef);
}
public void Remove(string newDef)
{
List.Remove(newDef);
}
public new void RemoveAt(int iWordDefIndex)
{
List.RemoveAt(iWordDefIndex);
}
public string this[int iWordDefIndex]
{
get
{
return (string)List[iWordDefIndex];
}
set
{
List[iWordDefIndex] = value;
}
}
}
}
Hope this helps.
I haven't compiled this yet but it shouldn't give any errors.
If you dig around the CollectionBase and the DictionaryBase from the
MSDN, you'll find out various methods to add the these classes.
Prateek.