Hi I'm currently working on a project that involves a carwash simulation. At the moment I'm stuck on sending a car object from the car class I created to another program. I am using udp for the sending of data for this project. I've googled how to do this with no luck so I figured I would ask here. I already know how to send a string message using udp but not sure how I would send this object.

The car class is just something the source uses to randomly generate a car. The things it is comprised of is simply a make, mode, and color which are stored in arrays to be randomly picked. What I need to do is get the source, that generates cars, to send the car object (using udp) that was generated to the attendant so it can be washed. Any help would would be appreciated here.

you need to implement ISerializable

If it's a smulation, why use 2 programs?
And why not using events?
Could you provide a bit of code on what you got so far?

Here is the car class followed source program to generate them. So far the source just generates a car of random qualities at at random times. Then it ouputs them to the screen for now. I'm not sure I understand what you mean by implementing it as events. We are thinking of using a counter variable to implement the discrete events of the carwashing itself.

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

namespace RandomTest
{
    public class Car
    {
        public string make;
        public string model;
        public string color;
        public int yearBuilt;

        public Car()
        {
            //car making = new car();
            int ranCounter;
            string[] make = new string[] { "Ford", "Crystler", "Toyota" };

            Random roller = new Random();
            int randomDelay = roller.Next(1, 50);
            Thread.Sleep(randomDelay);
            ranCounter = roller.Next(0, make.Length);
            this.make = make[ranCounter];
            string[] model;
            roller = new Random();
            randomDelay = roller.Next(1, 50);
            Thread.Sleep(randomDelay);
            if (this.make == "Ford")
            {
                model = new string[] { "Mustang", "Flex", "Fusion", "Ranger", "Explorer" };
                ranCounter = roller.Next(0, model.Length);
                this.model = model[ranCounter];
            }

            if (this.make == "Crystler")
            {
                model = new string[] { "Aspen", "Crossfire", "PT Cruiser", "Town & Country" };
                ranCounter = roller.Next(0, model.Length);
                this.model = model[ranCounter];
            }

            if (this.make == "Toyota")
            {
                model = new string[] { "Camry", "4Runner", "Avalon", "Land Cruiser", "Highlander" };
                ranCounter = roller.Next(0, model.Length);
                this.model = model[ranCounter];
            }
            randomDelay = roller.Next(1,50);
            Thread.Sleep(randomDelay);
            roller = new Random();
            string[] color = new string[] { "Red", "Yellow", "Green", "Blue", "Black", "White", "Orange", "Purple" };
            ranCounter = roller.Next(0, color.Length);
            this.color = color[ranCounter];

            randomDelay = roller.Next(1, 50);
            Thread.Sleep(randomDelay);
            roller = new Random();
            this.yearBuilt = roller.Next(1983, 2009);

            ;
        }
    }


    class Program
    {
    


        
        static int Main(string[] args)
        {
            Random waitTime = new Random();
            Car sendingCar;
            int rndNumbers;
            int randomDelay;
            for (int counter = 0; counter < 50; counter++)
            {
                rndNumbers = waitTime.Next(1, 61);
                
                
               randomDelay = rndNumbers * 1000;
                Console.WriteLine("Wait Seconds: {0}", rndNumbers);
                sendingCar = new Car();
                Thread.Sleep(randomDelay);

              //[B]need to send the car to the attendant here[/B]
                
                Console.WriteLine("Car: A " +sendingCar.color +" "+ sendingCar.yearBuilt + " " + sendingCar.make + " " + sendingCar.model);
            }

            return 0;
                
                
        }
    }
}

Biggest problem with udp is theres no guarentee it will get there - you would do far better to use tcp, so packets arrive in order, and are guarenteed to succeed or fail you can test both ends that they got there.

agree with lizr, i don't know why the need to send it udp

if you truly need a network based simulation, rather than same computer simulation, i would suggest using a webservice

Hmm I guess I'll have to figure out how to serialize the object and then sent it via tcp. I was only using udp because phase 1 of our project used it. I don't know much about implementing web services. This project is for distributed computing. It's a very hard class for me and this project is the last hurdle to pass it.

Then using tcp rather than udp alone would at least be a huge advantage, you dont *have* to use webservices.

As a side note to the discussion, although it is convenient in this context to have the default Car constructor generate a random car, the constructor seems to be doing an awful lot, including adding delays?

I try to promote the simple constructor theory where the constructor is as simple as possible and very rarely has problems.

It would be a better design to pull the random generation out and move it to a stand-alone function that would create a Car with 'random' attributes.

Is there any real benefit (either to the class or the program) to have several random delays in the single constructor method?

commented: I could not have said it better +2

As a side note to the discussion, although it is convenient in this context to have the default Car constructor generate a random car, the constructor seems to be doing an awful lot, including adding delays?

I try to promote the simple constructor theory where the constructor is as simple as possible and very rarely has problems.

It would be a better design to pull the random generation out and move it to a stand-alone function that would create a Car with 'random' attributes.

Is there any real benefit (either to the class or the program) to have several random delays in the single constructor method?

Yeah we are just doing the bare bones implementation first. I actually didn't create the car class my partner did. I just created the source program that randomly generates the cars. We might change that time permitting and what not. The random delays are to make the random seed different since the method we are using randomly generates according to the system clock.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.