0
\$\begingroup\$

In mobile phones technologies, a SectorId is a 128-bit value broadcast by a 1xEV-DO BTS system.

That is a 16 bytes data.

I made a structure to store them:

using System;
using System.Globalization;
using System.Linq;
using System.Text;
public struct SectorId
{
 readonly byte[] id;
 SectorId(byte[] id)
 {
 this.id = new byte[16];
 Array.Copy(id, this.id, 16);
 }
 public override bool Equals(object obj)
 {
 if (obj is SectorId)
 {
 return Equals((SectorId)obj);
 }
 return false;
 }
 public bool Equals(SectorId other)
 {
 return id.SequenceEqual(other.id);
 }
 public override int GetHashCode()
 {
 int hash = 0;
 unchecked
 {
 foreach (var b in id)
 {
 hash *= 397;
 hash += b;
 }
 }
 return hash;
 }
 public static SectorId Parse(string s)
 {
 Throw.IfArgumentIsNull("s", s);
 SectorId result;
 if (TryParse(s, out result))
 {
 return result;
 }
 throw new FormatException();
 }
 public override string ToString()
 {
 var sb = new StringBuilder();
 foreach (var b in id)
 {
 sb.AppendFormat("{0:X2}", b);
 }
 return sb.ToString();
 }
 public static bool TryParse(string s, out SectorId result)
 {
 if (s == null)
 {
 goto Fail;
 }
 if (s.Length > 32)
 {
 goto Fail;
 }
 if (s.Length < 32)
 {
 s = s.PadRight(32, '0'); 
 }
 var bytes = new byte[16];
 for (var i = 0; i < 16; i++)
 {
 var substring = s.Substring(i * 2, 2);
 if (!byte.TryParse(substring, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out bytes[i]))
 {
 goto Fail;
 }
 }
 result = new SectorId(bytes);
 return true;
 Fail:
 result = default(SectorId);
 return false;
 }
}

The TryParse part is the worse for me. I would like to avoid gotos.

D.C
4651 gold badge6 silver badges18 bronze badges
asked Feb 19, 2016 at 10:13
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Note that you can use multiple return statements in a method, so instead of using goto, you can use a return statement. I.e:

if (s == null)
{
 return false;
}
D.C
4651 gold badge6 silver badges18 bronze badges
answered Feb 19, 2016 at 10:42
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Don't forget to initialise the out parameter first. \$\endgroup\$ Commented Feb 19, 2016 at 10:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.