Convert Java to C# Help.

Hi Ladoes and Gents,

I am very new to Java and had a little code I would love some help with.  I basically need to get the associative C# version of this Java code.  Please if you could comment the heck out of the code to describe what it does on the c# versioni as well it would be greatly appreciated.

FYI I have tried to install the java to C# conversion assistant on a few different machines with different versions of visual studio with no luck.  I get an error message so a automated conversion is out.

Let me please relay my gracious thank you in advance.  I hope everyone is well.

Best - Dynotoe
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class TickData extends Data {
    //number of bytes in binary representation
    public static final int BYTES_COUNT = 8 + 8 * 4; //time + ask + bid + askVol + bidVol
    
    public double ask;
    public double bid;
    public double askVol;
    public double bidVol;
    
    public TickData() {
    }

    public TickData(long time, double ask, double bid, double askVol, double bidVol) {
        super(time);
        this.ask = ask;
        this.bid = bid;
        this.askVol = askVol;
        this.bidVol = bidVol;
    }

    public void fromBytes(byte[] bytes, int off) {
        time = getLong(bytes, off);
        ask = getDouble(bytes, off + 1 * 8);
        bid = getDouble(bytes, off + 2 * 8);
        askVol = getDouble(bytes, off + 3 * 8);
        bidVol = getDouble(bytes, off + 4 * 8);
    }
    
    public final void toBytes(byte[] buff, int off) {
        off = putLong(buff, off, time);
        off = putDouble(buff, off, ask);
        off = putDouble(buff, off, bid);
        off = putDouble(buff, off, askVol);
        putDouble(buff, off, bidVol);
    }

    public int getBytesCount() {
        return BYTES_COUNT;
    }

    public String toString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        StringBuilder stamp = new StringBuilder();
        stamp.append(time).append("[").append(format.format(time)).append("] / ");
        stamp.append(ask).append(" / ").append(bid);
        return stamp.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        long temp;
        temp = Double.doubleToLongBits(ask);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(askVol);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(bid);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(bidVol);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (getClass() != obj.getClass())
            return false;
        TickData other = (TickData) obj;
        if (Double.doubleToLongBits(ask) != Double.doubleToLongBits(other.ask))
            return false;
        if (Double.doubleToLongBits(askVol) != Double.doubleToLongBits(other.askVol))
            return false;
        if (Double.doubleToLongBits(bid) != Double.doubleToLongBits(other.bid))
            return false;
        if (Double.doubleToLongBits(bidVol) != Double.doubleToLongBits(other.bidVol))
            return false;
        return true;
    }
}

Open in new window

DynotoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

spuleCommented:
This is how it could be. But I can't guarantee that it will work as I don't fully understand what you are trying to do.
Please note that overriding/hiding in C# is a bit more complex than in Java, so you need to think of it in a different way (see the keywords override, new, virtual, and also this article). If I don't know your intentions, then I can't write the code properly.

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

namespace EE_Dynotoe {
    class TickData : Data {
        //number of bytes in binary representation
        public static const int BYTES_COUNT = 8 + 8 * 4; //time + ask + bid + askVol + bidVol

        //public properties
        public double Ask;
        public double Bid;
        public double AskVol;
        public double BidVol;

        public TickData(long time, double ask, double bid, double askVol, double bidVol) : base(time) { //: base(time) does the overloading
            //we set the values of the public properties to the arguments passed
            Ask = ask;
            Bid = bid;
            AskVol = askVol;
            BidVol = bidVol;
        }

        public void FromBytes(byte[] bytes, int off) {
            Time = GetLong(bytes, off);
            Ask = GetDouble(bytes, off + 1 * 8);
            Bid = GetDouble(bytes, off + 2 * 8);
            AskVol = GetDouble(bytes, off + 3 * 8);
            BidVol = GetDouble(bytes, off + 4 * 8);
        }
    
        public void ToBytes(byte[] buff, int off) {
            off = PutLong(buff, off, Time);
            off = PutDouble(buff, off, Ask);
            off = PutDouble(buff, off, Bid);
            off = PutDouble(buff, off, AskVol);
            PutDouble(buff, off, BidVol);
        }

        /* Consider defining BytesCount as a read-only property instead of a getter method:
         * 
         * public int BytesCount{
         *     get{
         *         return BYTES_COUNT;
         *     }
         * }
         **/

        public int GetBytesCount() {
            return BYTES_COUNT;
        }

        public String ToString() {
            //we create a new DateTime object for our Time so that String.Format can format it
            //(can UTC be considered the same as GMT?)
            DateTime dt = new DateTime(Time, DateTimeKind.Utc);
            StringBuilder stamp = new StringBuilder();
            stamp.Append(Time).Append("[").Append(String.Format("yyyy-MM-dd HH:mm:ss.SSSZ", dt)).Append("] / ");
            stamp.Append(Ask).Append(" / ").Append(Bid);
            return stamp.ToString();
        }

        override public int GetHashCode() {
            int prime = 31;
            int result = base.GetHashCode();
            long temp;
            temp = BitConverter.DoubleToInt64Bits(Ask);
            result = prime * result + (int)((uint)(temp) ^ ((uint)(temp) >> 32));
            temp = BitConverter.DoubleToInt64Bits(AskVol);
            result = prime * result + (int)((uint)(temp) ^ ((uint)(temp) >> 32));
            temp = BitConverter.DoubleToInt64Bits(Bid);
            result = prime * result + (int)((uint)(temp) ^ ((uint)(temp) >> 32));
            temp = BitConverter.DoubleToInt64Bits(BidVol);
            result = prime * result + (int)((uint)(temp) ^ ((uint)(temp) >> 32));
            return result;
        }

        override public bool Equals(Object obj) {
            if (this == obj)
                return true;
            if (!base.Equals(obj))
                return false;
            if (GetType() != obj.GetType())
                return false;
            TickData other = (TickData) obj;
            if(BitConverter.DoubleToInt64Bits(Ask) != BitConverter.DoubleToInt64Bits(other.Ask))
                return false;
            if(BitConverter.DoubleToInt64Bits(AskVol) != BitConverter.DoubleToInt64Bits(other.AskVol))
                return false;
            if(BitConverter.DoubleToInt64Bits(Bid) != BitConverter.DoubleToInt64Bits(other.Bid))
                return false;
            if(BitConverter.DoubleToInt64Bits(BidVol) != BitConverter.DoubleToInt64Bits(other.BidVol))
                return false;
            return true;
        }

    }
}

Open in new window

0
spuleCommented:
Sorry for that, here's a correction. Note that the override/new keywords are missing (it will throw warnings) and that it will throw errors at you because ToBytes and GetBytesCount are not overriden.

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

namespace EE_Dynotoe {
    class TickData : Data {
        //number of bytes in binary representation
        public const int BYTES_COUNT = 8 + 8 * 4; //time + ask + bid + askVol + bidVol

        //public properties
        public double Ask;
        public double Bid;
        public double AskVol;
        public double BidVol;

        public TickData(long time, double ask, double bid, double askVol, double bidVol) : base(time) { //: base(time) does the overloading
            //we set the values of the public properties to the arguments passed
            Ask = ask;
            Bid = bid;
            AskVol = askVol;
            BidVol = bidVol;
        }

        public void FromBytes(byte[] bytes, int off) {
            Time = GetLong(bytes, off);
            Ask = GetDouble(bytes, off + 1 * 8);
            Bid = GetDouble(bytes, off + 2 * 8);
            AskVol = GetDouble(bytes, off + 3 * 8);
            BidVol = GetDouble(bytes, off + 4 * 8);
        }
    
        public void ToBytes(byte[] buff, int off) {
            off = PutLong(buff, off, Time);
            off = PutDouble(buff, off, Ask);
            off = PutDouble(buff, off, Bid);
            off = PutDouble(buff, off, AskVol);
            PutDouble(buff, off, BidVol);
        }

        /* Consider defining BytesCount as a read-only property instead of a getter method:
         * 
         * public int BytesCount{
         *     get{
         *         return BYTES_COUNT;
         *     }
         * }
         **/

        public int GetBytesCount() {
            return BYTES_COUNT;
        }

        public String ToString() {
            //we create a new DateTime object for our Time so that String.Format can format it
            //(can UTC be considered the same as GMT?)
            DateTime dt = new DateTime(Time, DateTimeKind.Utc);
            StringBuilder stamp = new StringBuilder();
            stamp.Append(Time).Append("[").Append(String.Format("yyyy-MM-dd HH:mm:ss.SSSZ", dt)).Append("] / ");
            stamp.Append(Ask).Append(" / ").Append(Bid);
            return stamp.ToString();
        }

        public int GetHashCode() {
            int prime = 31;
            int result = base.GetHashCode();
            long temp;
            temp = BitConverter.DoubleToInt64Bits(Ask);
            result = prime * result + (int)((uint)(temp) ^ ((uint)(temp) >> 32));
            temp = BitConverter.DoubleToInt64Bits(AskVol);
            result = prime * result + (int)((uint)(temp) ^ ((uint)(temp) >> 32));
            temp = BitConverter.DoubleToInt64Bits(Bid);
            result = prime * result + (int)((uint)(temp) ^ ((uint)(temp) >> 32));
            temp = BitConverter.DoubleToInt64Bits(BidVol);
            result = prime * result + (int)((uint)(temp) ^ ((uint)(temp) >> 32));
            return result;
        }

        public bool Equals(Object obj) {
            if (this == obj)
                return true;
            if (!base.Equals(obj))
                return false;
            if (GetType() != obj.GetType())
                return false;
            TickData other = (TickData) obj;
            if(BitConverter.DoubleToInt64Bits(Ask) != BitConverter.DoubleToInt64Bits(other.Ask))
                return false;
            if(BitConverter.DoubleToInt64Bits(AskVol) != BitConverter.DoubleToInt64Bits(other.AskVol))
                return false;
            if(BitConverter.DoubleToInt64Bits(Bid) != BitConverter.DoubleToInt64Bits(other.Bid))
                return false;
            if(BitConverter.DoubleToInt64Bits(BidVol) != BitConverter.DoubleToInt64Bits(other.BidVol))
                return false;
            return true;
        }

    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DynotoeAuthor Commented:
Hi  !

No I understand it's probably difficult to see the whole purpose without all the classes.  There are just 2 more.  I'll list them both now if you want to do those as well.  It is to read in binary historical price files and then work with them.  I've have a file which explains the whole thing if I can email it to you.

Regardless if you continue to help, you're are a life saver.  Thanks spule

I'll be listing the last two now.

Dyno
0
DynotoeAuthor Commented:
Great
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.