Click here to Skip to main content
15,898,939 members
Home / Discussions / C#
   

C#

 
QuestionSuperMarket Inventory Database Software Pin
Ayaz Muhammad Hanif23-Sep-11 21:40
Ayaz Muhammad Hanif23-Sep-11 21:40 
AnswerRe: SuperMarket Inventory Database Software Pin
André Kraak23-Sep-11 21:52
André Kraak23-Sep-11 21:52 
AnswerRe: SuperMarket Inventory Database Software Pin
Richard MacCutchan23-Sep-11 22:52
mveRichard MacCutchan23-Sep-11 22:52 
QuestionHow to add checkbox to the title of the groupbox in C#.Net Pin
s_akram23-Sep-11 21:22
s_akram23-Sep-11 21:22 
AnswerRe: How to add checkbox to the title of the groupbox in C#.Net Pin
BillWoodruff23-Sep-11 23:13
professionalBillWoodruff23-Sep-11 23:13 
QuestionSample Data Pin
Xarzu23-Sep-11 15:44
Xarzu23-Sep-11 15:44 
AnswerRe: Sample Data Pin
BillWoodruff23-Sep-11 23:34
professionalBillWoodruff23-Sep-11 23:34 
Questionhelp to deal with graph data structure Pin
Mahdi_mnj23-Sep-11 12:10
Mahdi_mnj23-Sep-11 12:10 
Dear friends I have simple experience programming with C#,but new to deal with graph data structure. when I got some examples from MSDN which is the following

I face this error message Error

'WindowsApplication4.Graph<t> does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()
'WindowsApplication4.Graph<l>GetEnumerator()' is either static, not public, or has the wrong return type.

and I had changed the return data type to all possible type and the error still the same please help me to overcome this problem.


C#
#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

#endregion

namespace WindowsApplication4
{
    /// <summary>
    /// Represents a graph.  A graph is an arbitrary collection of GraphNode instances.
    /// </summary>
    /// <typeparam name="T">The type of data stored in the graph's nodes.</typeparam>
    public class Graph<T> : IEnumerable<T>
    {
        #region Private Member Variables
        private NodeList<T> nodeSet;        // the set of nodes in the graph
        #endregion

        #region Constructors
        public Graph() : this(null) { }
        public Graph(NodeList<T> nodeSet)
        {
            if (nodeSet == null)
                this.nodeSet = new NodeList<T>();
            else
                this.nodeSet = nodeSet;
        }
        #endregion

        #region Methods
        #region Add
        #region AddNode
        /// <summary>
        /// Adds a new GraphNode instance to the Graph
        /// </summary>
        /// <param name="node">The GraphNode instance to add.</param>
        ///


        #region IEnumerable<T> Members
        /// <summary>
        /// Returns an enumerator that allows for iterating through the contents of the graph.
        public IEnumerator<T> GetEnumerator()
        {
            foreach (GraphNode<T> gnode in nodeSet)
               yield return gnode.Value;
        }
        #endregion



        public void AddNode(GraphNode<T> node)
        {
            // adds a node to the graph
            nodeSet.Add(node);
        }

        /// <summary>
        /// Adds a new value to the graph.
        /// </summary>
        /// <param name="value">The value to add to the graph</param>
        public void AddNode(T value)
        {
            nodeSet.Add(new GraphNode<T>(value));
        }
        #endregion

        #region Add*Edge Methods
        /// <summary>
        /// Adds a directed edge from a GraphNode with one value (from) to a GraphNode with another value (to).
        /// </summary>
        /// <param name="from">The value of the GraphNode from which the directed edge eminates.</param>
        /// <param name="to">The value of the GraphNode to which the edge leads.</param>
        public void AddDirectedEdge(T from, T to)
        {
            AddDirectedEdge(from, to, 0);
        }

        /// <summary>
        /// Adds a directed edge from one GraphNode (from) to another (to).
        /// </summary>
        /// <param name="from">The GraphNode from which the directed edge eminates.</param>
        /// <param name="to">The GraphNode to which the edge leads.</param>
        public void AddDirectedEdge(GraphNode<T> from, GraphNode<T> to)
        {
            AddDirectedEdge(from, to, 0);
        }

        /// <summary>
        /// Adds a directed edge from one GraphNode (from) to another (to) with an associated cost.
        /// </summary>
        /// <param name="from">The GraphNode from which the directed edge eminates.</param>
        /// <param name="to">The GraphNode to which the edge leads.</param>
        /// <param name="cost">The cost of the edge from "from" to "to".</param>
        public void AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
        {
            from.Neighbors.Add(to);
            from.Costs.Add(cost);
        }

        /// <summary>
        /// Adds a directed edge from a GraphNode with one value (from) to a GraphNode with another value (to)
        /// with an associated cost.
        /// </summary>
        /// <param name="from">The value of the GraphNode from which the directed edge eminates.</param>
        /// <param name="to">The value of the GraphNode to which the edge leads.</param>
        /// <param name="cost">The cost of the edge from "from" to "to".</param>
        public void AddDirectedEdge(T from, T to, int cost)
        {
            ((GraphNode<T>)nodeSet.FindByValue(from)).Neighbors.Add(nodeSet.FindByValue(to));
            ((GraphNode<T>)nodeSet.FindByValue(from)).Costs.Add(cost);
        }

        /// <summary>
        /// Adds an undirected edge from a GraphNode with one value (from) to a GraphNode with another value (to).
        /// </summary>
        /// <param name="from">The value of one of the GraphNodes that is joined by the edge.</param>
        /// <param name="to">The value of one of the GraphNodes that is joined by the edge.</param>
        public void AddUndirectedEdge(T from, T to)
        {
            AddUndirectedEdge(from, to, 0);
        }

        /// <summary>
        /// Adds an undirected edge from one GraphNode to another.
        /// </summary>
        /// <param name="from">One of the GraphNodes that is joined by the edge.</param>
        /// <param name="to">One of the GraphNodes that is joined by the edge.</param>
        public void AddUndirectedEdge(GraphNode<T> from, GraphNode<T> to)
        {
            AddUndirectedEdge(from, to, 0);
        }

        /// <summary>
        /// Adds an undirected edge from one GraphNode to another with an associated cost.
        /// </summary>
        /// <param name="from">One of the GraphNodes that is joined by the edge.</param>
        /// <param name="to">One of the GraphNodes that is joined by the edge.</param>
        /// <param name="cost">The cost of the undirected edge.</param>
        public void AddUndirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
        {
            from.Neighbors.Add(to);
            from.Costs.Add(cost);

            to.Neighbors.Add(from);
            to.Costs.Add(cost);
        }

        /// <summary>
        /// Adds an undirected edge from a GraphNode with one value (from) to a GraphNode with another value (to)
        /// with an associated cost.
        /// </summary>
        /// <param name="from">The value of one of the GraphNodes that is joined by the edge.</param>
        /// <param name="to">The value of one of the GraphNodes that is joined by the edge.</param>
        /// <param name="cost">The cost of the undirected edge.</param>
        public void AddUndirectedEdge(T from, T to, int cost)
        {
            ((GraphNode<T>)nodeSet.FindByValue(from)).Neighbors.Add(nodeSet.FindByValue(to));
            ((GraphNode<T>)nodeSet.FindByValue(from)).Costs.Add(cost);

            ((GraphNode<T>)nodeSet.FindByValue(to)).Neighbors.Add(nodeSet.FindByValue(from));
            ((GraphNode<T>)nodeSet.FindByValue(to)).Costs.Add(cost);
        }
        #endregion
        #endregion

        #region Clear
        /// <summary>
        /// Clears out the contents of the Graph.
        /// </summary>
        public void Clear()
        {
            nodeSet.Clear();
        }
        #endregion

        #region Contains
        /// <summary>
        /// Returns a Boolean, indicating if a particular value exists within the graph.
        /// </summary>
        /// <param name="value">The value to search for.</param>
        /// <returns>True if the value exist in the graph; false otherwise.</returns>
        public bool Contains(T value)
        {
            return nodeSet.FindByValue(value) != null;
        }
        #endregion

        #region Remove
        /// <summary>
        /// Attempts to remove a node from a graph.
        /// </summary>
        /// <param name="value">The value that is to be removed from the graph.</param>
        /// <returns>True if the corresponding node was found, and removed; false if the value was not
        /// present in the graph.</returns>
        /// <remarks>This method removes the GraphNode instance, and all edges leading to or from the
        /// GraphNode.</remarks>
        public bool Remove(T value)
        {
            // first remove the node from the nodeset
            GraphNode<T> nodeToRemove = (GraphNode<T>)nodeSet.FindByValue(value);
            if (nodeToRemove == null)
                // node wasn't found
                return false;

            // otherwise, the node was found
            nodeSet.Remove(nodeToRemove);

            // enumerate through each node in the nodeSet, removing edges to this node
            foreach (GraphNode<T> gnode in nodeSet)
            {
                int index = gnode.Neighbors.IndexOf(nodeToRemove);
                if (index != -1)
                {
                    // remove the reference to the node and associated cost
                    gnode.Neighbors.RemoveAt(index);
                    gnode.Costs.RemoveAt(index);
                }
            }

            return true;
        }
        #endregion
        //mnj
        /*#region IEnumerable<T> Members
        /// <summary>
        /// Returns an enumerator that allows for iterating through the contents of the graph.
        public IEnumerator<T> GetEnumerator()
        {
            foreach (GraphNode<T> gnode in nodeSet)
                yield return gnode.Value;
        }
        #endregion */
        //mnj
        #endregion

        #region Public Properties
        /// <summary>
        /// Returns the set of nodes in the graph.
        /// </summary>
        public NodeList<T> Nodes
        {
            get
            {
                return nodeSet;
            }
        }

        /// <summary>
        /// Returns the number of vertices in the graph.
        /// </summary>
        public int Count
        {
            get { return nodeSet.Count; }
        }
        #endregion


    }
}

AnswerRe: help to deal with graph data structure Pin
SledgeHammer0123-Sep-11 13:53
SledgeHammer0123-Sep-11 13:53 
AnswerRe: help to deal with graph data structure Pin
BillWoodruff24-Sep-11 0:04
professionalBillWoodruff24-Sep-11 0:04 
AnswerRe: help to deal with graph data structure Pin
BobJanova26-Sep-11 1:33
BobJanova26-Sep-11 1:33 
GeneralRe: help to deal with graph data structure Pin
Mahdi_mnj26-Sep-11 4:32
Mahdi_mnj26-Sep-11 4:32 
GeneralRe: help to deal with graph data structure Pin
BobJanova26-Sep-11 5:54
BobJanova26-Sep-11 5:54 
GeneralRe: help to deal with graph data structure Pin
Mahdi_mnj26-Sep-11 4:50
Mahdi_mnj26-Sep-11 4:50 
QuestionVersioning the hard way Pin
lukeer23-Sep-11 4:43
lukeer23-Sep-11 4:43 
AnswerRe: Versioning the hard way Pin
André Kraak23-Sep-11 23:03
André Kraak23-Sep-11 23:03 
GeneralRe: Versioning the hard way Pin
lukeer25-Sep-11 20:40
lukeer25-Sep-11 20:40 
AnswerRe: Versioning the hard way Pin
BobJanova26-Sep-11 2:02
BobJanova26-Sep-11 2:02 
QuestionC sharp GUI help needed! Pin
memed0922-Sep-11 20:42
memed0922-Sep-11 20:42 
AnswerRe: C sharp GUI help needed! Pin
memed0922-Sep-11 20:46
memed0922-Sep-11 20:46 
AnswerRe: C sharp GUI help needed! Pin
ScottM122-Sep-11 20:57
ScottM122-Sep-11 20:57 
GeneralRe: C sharp GUI help needed! Pin
memed0922-Sep-11 21:03
memed0922-Sep-11 21:03 
AnswerRe: C sharp GUI help needed! Pin
DaveAuld22-Sep-11 21:53
professionalDaveAuld22-Sep-11 21:53 
AnswerRe: C sharp GUI help needed! Pin
BillWoodruff23-Sep-11 0:43
professionalBillWoodruff23-Sep-11 0:43 
AnswerRe: C sharp GUI help needed! Pin
Bernhard Hiller26-Sep-11 1:07
Bernhard Hiller26-Sep-11 1:07 

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.