Arrays
-
I am having trouble with this compiling. The compiler says that invalid array initializer.
int i1 = 12; byte[][] bs1 = new byte[][(uint)dnsQuestions.Count]; for (int j1 = 0; j1 < dnsQuestions.Count; j1++) { bs1[j1] = ((DnsQuestion)dnsQuestions[j1]).ToByteArray(); i1 += (int)bs1[j1].Length; } byte[][] bs2 = new byte[][(uint)answerResourceRecords.Count]; for (int k1 = 0; k1 < answerResourceRecords.Count; k1++) { bs2[k1] = ((ResourceRecord)answerResourceRecords[k1]).ToByteArray(); i1 += (int)bs2[k1].Length; } byte[][] bs3 = new byte[][(uint)authorityResourceRecords.Count]; for (int i2 = 0; i2 < authorityResourceRecords.Count; i2++) { bs3[i2] = ((ResourceRecord)authorityResourceRecords[i2]).ToByteArray(); i1 += (int)bs3[i2].Length; } byte[][] bs4 = new byte[][(uint)additionalResourceRecords.Count]; for (int j2 = 0; j2 < additionalResourceRecords.Count; j2++) { bs4[j2] = ((ResourceRecord)additionalResourceRecords[j2]).ToByteArray(); i1 += (int)bs4[j2].Length; } byte[] bs5 = new byte[(uint)i1]; BinaryWriter binaryWriter = new BinaryWriter(new MemoryStream(bs5)); try { BigEndian.Write(binaryWriter, identifier); BigEndian.Write(binaryWriter, (ushort)flags.Data); BigEndian.Write(binaryWriter, (ushort)dnsQuestions.Count); BigEndian.Write(binaryWriter, (ushort)answerResourceRecords.Count); BigEndian.Write(binaryWriter, (ushort)authorityResourceRecords.Count); BigEndian.Write(binaryWriter, (ushort)additionalResourceRecords.Count); for (int k2 = 0; k2 < (int)bs1.Length; k2++) { binaryWriter.Write(bs1[k2]); } for (int i3 = 0; i3 < (int)bs2.Length; i3++) { binaryWriter.Write(bs2[i3]); } for (int j3 = 0; j3 < (int)bs3.Length; j3++) { binaryWriter.Write(bs3[j3]); } for (int k3 = 0; k3 < (int)bs4.Length; k3++) { binaryWriter.Write(bs4[k3]);
-
I am having trouble with this compiling. The compiler says that invalid array initializer.
int i1 = 12; byte[][] bs1 = new byte[][(uint)dnsQuestions.Count]; for (int j1 = 0; j1 < dnsQuestions.Count; j1++) { bs1[j1] = ((DnsQuestion)dnsQuestions[j1]).ToByteArray(); i1 += (int)bs1[j1].Length; } byte[][] bs2 = new byte[][(uint)answerResourceRecords.Count]; for (int k1 = 0; k1 < answerResourceRecords.Count; k1++) { bs2[k1] = ((ResourceRecord)answerResourceRecords[k1]).ToByteArray(); i1 += (int)bs2[k1].Length; } byte[][] bs3 = new byte[][(uint)authorityResourceRecords.Count]; for (int i2 = 0; i2 < authorityResourceRecords.Count; i2++) { bs3[i2] = ((ResourceRecord)authorityResourceRecords[i2]).ToByteArray(); i1 += (int)bs3[i2].Length; } byte[][] bs4 = new byte[][(uint)additionalResourceRecords.Count]; for (int j2 = 0; j2 < additionalResourceRecords.Count; j2++) { bs4[j2] = ((ResourceRecord)additionalResourceRecords[j2]).ToByteArray(); i1 += (int)bs4[j2].Length; } byte[] bs5 = new byte[(uint)i1]; BinaryWriter binaryWriter = new BinaryWriter(new MemoryStream(bs5)); try { BigEndian.Write(binaryWriter, identifier); BigEndian.Write(binaryWriter, (ushort)flags.Data); BigEndian.Write(binaryWriter, (ushort)dnsQuestions.Count); BigEndian.Write(binaryWriter, (ushort)answerResourceRecords.Count); BigEndian.Write(binaryWriter, (ushort)authorityResourceRecords.Count); BigEndian.Write(binaryWriter, (ushort)additionalResourceRecords.Count); for (int k2 = 0; k2 < (int)bs1.Length; k2++) { binaryWriter.Write(bs1[k2]); } for (int i3 = 0; i3 < (int)bs2.Length; i3++) { binaryWriter.Write(bs2[i3]); } for (int j3 = 0; j3 < (int)bs3.Length; j3++) { binaryWriter.Write(bs3[j3]); } for (int k3 = 0; k3 < (int)bs4.Length; k3++) { binaryWriter.Write(bs4[k3]);
Replace all occurences of
[][(uint)dnsQuestions.Count]
with[(uint)dnsQuestions.Count][].
-
I am having trouble with this compiling. The compiler says that invalid array initializer.
int i1 = 12; byte[][] bs1 = new byte[][(uint)dnsQuestions.Count]; for (int j1 = 0; j1 < dnsQuestions.Count; j1++) { bs1[j1] = ((DnsQuestion)dnsQuestions[j1]).ToByteArray(); i1 += (int)bs1[j1].Length; } byte[][] bs2 = new byte[][(uint)answerResourceRecords.Count]; for (int k1 = 0; k1 < answerResourceRecords.Count; k1++) { bs2[k1] = ((ResourceRecord)answerResourceRecords[k1]).ToByteArray(); i1 += (int)bs2[k1].Length; } byte[][] bs3 = new byte[][(uint)authorityResourceRecords.Count]; for (int i2 = 0; i2 < authorityResourceRecords.Count; i2++) { bs3[i2] = ((ResourceRecord)authorityResourceRecords[i2]).ToByteArray(); i1 += (int)bs3[i2].Length; } byte[][] bs4 = new byte[][(uint)additionalResourceRecords.Count]; for (int j2 = 0; j2 < additionalResourceRecords.Count; j2++) { bs4[j2] = ((ResourceRecord)additionalResourceRecords[j2]).ToByteArray(); i1 += (int)bs4[j2].Length; } byte[] bs5 = new byte[(uint)i1]; BinaryWriter binaryWriter = new BinaryWriter(new MemoryStream(bs5)); try { BigEndian.Write(binaryWriter, identifier); BigEndian.Write(binaryWriter, (ushort)flags.Data); BigEndian.Write(binaryWriter, (ushort)dnsQuestions.Count); BigEndian.Write(binaryWriter, (ushort)answerResourceRecords.Count); BigEndian.Write(binaryWriter, (ushort)authorityResourceRecords.Count); BigEndian.Write(binaryWriter, (ushort)additionalResourceRecords.Count); for (int k2 = 0; k2 < (int)bs1.Length; k2++) { binaryWriter.Write(bs1[k2]); } for (int i3 = 0; i3 < (int)bs2.Length; i3++) { binaryWriter.Write(bs2[i3]); } for (int j3 = 0; j3 < (int)bs3.Length; j3++) { binaryWriter.Write(bs3[j3]); } for (int k3 = 0; k3 < (int)bs4.Length; k3++) { binaryWriter.Write(bs4[k3]);
Bo, I can see two mistakes: (1) referring to '7.5.10.2 Array creation expressions' in the C# online reference you cannot instantiate the sub-arrays, because the initial value of the first array is null. But in any case this would be an array of new byte [count] []. You try to create a subarray, where no array is created and initiated: new byte [][count] <- this can never work, because the compiler doesn't know how many sub-arrays (i.e. memory) must be initiated. (2) don't cast to uint, use System.Convert.ToUInt32. Hope this makes your year :cool: Wolfgang
-
Bo, I can see two mistakes: (1) referring to '7.5.10.2 Array creation expressions' in the C# online reference you cannot instantiate the sub-arrays, because the initial value of the first array is null. But in any case this would be an array of new byte [count] []. You try to create a subarray, where no array is created and initiated: new byte [][count] <- this can never work, because the compiler doesn't know how many sub-arrays (i.e. memory) must be initiated. (2) don't cast to uint, use System.Convert.ToUInt32. Hope this makes your year :cool: Wolfgang
So how do I instantiate this?
byte[][] bs1 = new byte[(uint)dnsQuestions.Count][]; for (int j1 = 0; j1 < dnsQuestions.Count; j1++) { bs1[j1] = ((DnsQuestion)dnsQuestions[j1]).ToByteArray(); i1 += (int)bs1[j1].Length; }
Then how do I instatiate the other part of the array?byte[][] bs1 = new byte[dnsQuestions.Count][dnsQuestions.Count]; for (int j1 = 0; j1 < dnsQuestions.Count; j1++) { bs1[j1] = ((DnsQuestion)dnsQuestions[j1]).ToByteArray(); i1 += (int)bs1[j1].Length; }
Like this and IntelliSense says this is wrong. Thank You Bo Hunter -
So how do I instantiate this?
byte[][] bs1 = new byte[(uint)dnsQuestions.Count][]; for (int j1 = 0; j1 < dnsQuestions.Count; j1++) { bs1[j1] = ((DnsQuestion)dnsQuestions[j1]).ToByteArray(); i1 += (int)bs1[j1].Length; }
Then how do I instatiate the other part of the array?byte[][] bs1 = new byte[dnsQuestions.Count][dnsQuestions.Count]; for (int j1 = 0; j1 < dnsQuestions.Count; j1++) { bs1[j1] = ((DnsQuestion)dnsQuestions[j1]).ToByteArray(); i1 += (int)bs1[j1].Length; }
Like this and IntelliSense says this is wrong. Thank You Bo Hunter