WCF server/client conected clients checking value for server - c#

Now I have another problem with WCF server/client.
I have a method:
bool spr_wiersz(int wiersz, int kolumna) //check_row(int row, int column)
{
for (int i = 0; i < 9; i++)
{
if (i != kolumna)
{
//if(grid(row,i)==grid[row,column]
if (tablica[wiersz, i] == tablica[wiersz, kolumna])
return false;
}
}
return true;
}
And I want send every one value to clients and they will check it and return true, or false to server.
ex.
client 1 compare for i=0
client 2 compare for i=1
client 3 compare for i=2
client 1 compare for i=3
client 2 compare for i=4
......
To send something to everyone clients I use :
subscribers.ForEach(delegate(ImessageCallback callback)
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
}
});
I hevan't any idea how conect this ;/
And another problem is, how send grid to client to compare value.

I solve the problem :)
it almost work properly.
bool spr_wiersz(int wiersz, int kolumna)
{
wys_tab();
Console.WriteLine("wiersz: {0}, kolumna: {1}", wiersz, kolumna);
int i_wiersz=0;
bool[] result = new bool[9];
while (i_wiersz < 9)
{
subscribers.ForEach(delegate(ImessageCallback callback)
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
Console.WriteLine("wiersz: {0}, kolumna: {1}, i: {2}", wiersz, kolumna, i_wiersz);
result[i_wiersz] = callback.spr_wiersz(wiersz, kolumna, i_wiersz);
i_wiersz++;
}
});
for (int j = 0; j < i_wiersz; j++)
{
if (result[j] == false)
{
return false;
}
}
}
return true;
}
It's work good only for 1,3 or 9 clients. For other number of clients this doesn't work yet.

Related

C# two-dimensional array to validate entry in the form

There is a form that each user is required to complete, it has 4 fields: date, bill code, amount and currency. Bill code has a drop down menu with a lot of options of 4 that options are valid (Health, Travel, Meal, Hotel). Bill code field cannot be left blank, and it should take only one of these 4 options. A user make 4 entries with each of the 4 bill codes. If user enters only Health and Travel, an error message should fire that Meal and Hotel records need to be added. This is what I got so far:
public bool ValidateBillCode(bills billArray[][])
{
for(int i = 0; i < billArray.Length; i++)
{
for(int j = 0; j < billArray[0].Length; j++)
{
if(billArray[i][j].IndexOf("Health") >= 0 ||
billArray[i][j].IndexOf("Travel") >= 0 ||
billArray[i][j].IndexOf("Meal") >= 0||
billArray[i][j].IndexOf("Hotel") >= 0)
{
return true;
}
else
{
return false;
}
}
}
}
But it doesn't make sure that all four of these are entered, and I'm not sure how to make an error message that would tell the user which of the four are missing. I will appreciate any help with this.
I believe you don't need any 2-dimensional or jagged array. You need to define a struct like:
public struct Bill
{
public string Date;
public string BillCode;
public string Amount;
public string Currency;
}
Then your ValidateBillCode() becomes like this:
public bool ValidateBillCode(Bill[] billArray)
{
bool healthEntered = false;
bool travelEntered = false;
bool mealEntered = false;
bool hotelEntered = false;
for (int i = 0; i < billArray.Length; i++)
{
if (billArray[i].BillCode == "Health")
healthEntered = true;
else if (billArray[i].BillCode == "Travel")
travelEntered = true;
else if (billArray[i].BillCode == "Meal")
mealEntered = true;
else if (billArray[i].BillCode == "Hotel")
hotelEntered = true;
}
return healthEntered && travelEntered && mealEntered && hotelEntered;
}
But this is just a very simplistic approach. For a more appropriate solution, you'd better to use class instead of struct, use enum for BillCode, DateTime for Date, and double for Amount.

Merging two lists which are already sorted in c#

So i have two sorted lists with deliverable's and I want to Merge them. I never did this operation before and i can not really make it work. You can see method below that i wrote. I can not figure out while statement because it fires exception all the time. I dont know what to do...
public void MergeLists(List<Deliverable> a, List<Deliverable> b)
{
int index1 = 0;
int index2 = 0;
while (a.Count() >= index1 || b.Count() >= index2)
{
if (a[index1].ID> b[index2].ID)
{
FinalDeliverables.Add(a[index1]);
index1++;
}
else if (a[index1].ID < b[index2].ID)
{
FinalDeliverables.Add(a[index2]);
index2++;
}
else if (a[index1].ID == b[index2].ID)
{
FinalDeliverables.Add(a[index1]);
FinalDeliverables.Add(a[index2]);
index1++;
index2++;
}
}
}
I believe that the exceptions you are getting are coming from null pointers. This would occur if, for example, you already reached the end of one of the lists, yet your while loop is still trying to compare values. One workaround to this is to simply add a check before the if statements to see if one (or both) of the ends of the lists have been reached. If so, then simply add the remainder of the items from the other list.
public void MergeLists(List<Deliverable> a, List<Deliverable> b)
{
int index1 = 0;
int index2 = 0;
while (true)
{
// if the end of the 'a' list has been reached, then add
// everything from the 'b' list and break from the loop
if (index1 >= a.Count()) {
for (int i=index2; i < b.Count(); ++i) {
FinalDeliverables.Add(b[i]);
}
break;
}
// if the end of the 'b' list has been reached, then add
// everything from the 'a' list and break from the loop
if (index2 >= b.Count()) {
for (int i=index1; i < a.Count(); ++i) {
FinalDeliverables.Add(a[i]);
}
break;
}
if (a[index1].ID > b[index2].ID)
{
FinalDeliverables.Add(a[index1]);
index1++;
}
else if (a[index1].ID < b[index2].ID)
{
FinalDeliverables.Add(a[index2]);
index2++;
}
else if (a[index1].ID == b[index2].ID)
{
FinalDeliverables.Add(a[index1]);
FinalDeliverables.Add(a[index2]);
index1++;
index2++;
}
}
}
Note that a side effect of this refactor is that your original while loop no longer has to check boundaries. Instead, the loop will be terminated when one of the lists has been exhausted.
Also note that this solution assumes that your input lists are sorted in descending order.
You can use the LINQ Extension methods for this purpose, The Method signature for MergeLists will be like the following:
public void MergeLists(List<Deliverable> a, List<Deliverable> b)
{
var finalList = a.Concat(b);
List<Deliverable> FinalSortedList = finalList.OrderBy(x => x.ID).ToList();
}
Or else you can modify your own method as like the following: Before that Let me assume the following, The Count of elements in the Input list a will always be Greater than that of b. SO what you need to do is Check the count of elements before calling the method as well. So the call will be like the following:
if(a.Count>b.Count)
MergeLists(a,b);
else
MergeLists(b,a);
You mentioned that the two inputs ware sorted, Let me assume those lists are sorted in ascending order. Now consider the following code:
public void MergeLists(List<Deliverable> a, List<Deliverable> b)
{
int largeArrayCount = a.Count;
int currentBIndex = 0;
List<Deliverable> FinalResult = new List<Deliverable>();
for (int i = 0; i < largeArrayCount; i++)
{
if (i < b.Count)
{
if (a[i].ID >= b[i].ID)
{
// Add All elements of B Which is smaller than current element of A
while (a[i].ID <= b[currentBIndex].ID)
{
FinalResult.Add(b[currentBIndex++]);
}
}
else
{
FinalResult.Add(a[i]);
}
}
else
{
// No more elements in b so no need for checking
FinalResult.Add(a[i]);
}
}
}

Condition if for Nunit assert

I need to compare two Lists by NUnit's Assert.AreEqual. If this statement is false (lists not the same) - then I need find elements in lists which not the same.
How I can use If statement for know- Assert return true or false?
It seems that nunit CollectionAssert.AreEquivalent is exactly what you were looking for.
This method compare between the collections.
If a mismatch was found then the method will throw exception with the difference.
Here's a possible solution.
[Test]
public void TestMethod1()
{
List<int> a = new List<int>();
List<int> b = new List<int>();
//Fake data
a.Add(1);
b.Add(2);
b.Add(2);
Assert.IsTrue(AreEquals(a,b), GetDifferentElements(a,b));
}
private string GetDifferentElements(List<int> a, List<int> b)
{
if (AreEquals(a, b))
{
return string.Empty;
}
if (a.Count != b.Count)
{
return "The two lists have a different length";
}
StringBuilder s = new StringBuilder();
for (int i = 0; i < a.Count; i++)
{
if (a[i] != b[i])
{
s.Append(i.ToString() + " ");
}
}
return string.Format("Elements at indexes {0} are different", s.ToString());
}
private bool AreEquals(List<int> a, List<int> b)
{
if (a.Count != b.Count)
{
return false;
}
for (int i = 0; i < a.Count; i++)
{
if (a[i] != b[i])
{
return false;
}
}
return true;
}
UPDATE
Of course I was unaware of the CollectionAssert.AreEquivalent method provided in the accepted answer. That's a better solution of course!

Break foreach loop (WCF server/client)

I have a simple code, which send from server to clients value to count. This loop count for 9 value, from 1 to 9. Everything work good for 1,3 or 9 clients. But for other number of clients when i_wiersz has value 9 and foreach loop want sent something to another client server break down. Ho make, to work with any one numbers of clients?
I try put inside foreach loop:
if(i_wiersz == 9)
break;
but a get an error: Error
Control cannot leave the body of an anonymous method or lambda
expression
My code:
bool spr_wiersz(int wiersz, int kolumna) //chck_roow(int roow, int column)
{
wys_tab();
int i_wiersz = 0;
bool[] result = new bool[9];
while (i_wiersz < 9)
{
subscribers.ForEach(delegate(ImessageCallback callback)
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
result[i_wiersz] = callback.spr_wiersz(wiersz, kolumna, i_wiersz);
i_wiersz++;
}
});
for (int j = 0; j < i_wiersz; j++)
{
if (result[j] == false)
{
return false;
}
}
}
return true;
}
Can’t you simply convert it to a traditional foreach?
foreach (IMessageCallback callback in subscribers)
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
result[i_wiersz] = callback.spr_wiersz(wiersz, kolumna, i_wiersz);
i_wiersz++;
if (i_wiersz == 9)
break;
}
}

Listbox For statement not looping through listboxs

4th time asking this question after trying to apply so fixes. My problem is I have 2 listboxs one holds IDs and the other holds Cell ids. The ID listbox has 10 items (for testing) and Cell boxes have 5 ID. I'm trying to process listId and listCell at the same time creating update links...
example MakeReq will create txtWebUpdate.Text listID listCell &ire=1, in which is
store.domain.com/101/sec01&ire=1
store.domain.com/102/sec02&ire=1
store.domain.com/103/sec03&ire=1
store.domain.com/104/sec04&ire=1
store.domain.com/105/sec05&ire=1
store.domain.com/106/sec01&ire=1 <- notice how listCell starts over it
continues to loop applying sections until the ListID is complete.
here's the code i've been working with. what's happening with my code is that it's only selecting the first item. It's not going to the next item after its done.
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() +
"&ire=1", listCell.Items[b].ToString());
//System.Threading.Thread.Sleep(5);
}
}
}
Please only comment if you can help. I'm almost positive that the problem is the for statement and how it's selecting list items, but I may be wrong.
Edit More Info: after testing an example/suggestion for #duffp below the problem is definitely with my For statements. What's happening is its counting that I have 5 entries in ListCell and then output one (the same) ListID 5times but with a different ListCell. Can someone help me write it following what I wrong above?
It may have something to do within the MakeReq method call. I just tried the following code:
private void Form1_Load(object sender, EventArgs e)
{
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(listId.Items[a].ToString(), listCell.Items[b].ToString());
}
}
}
}
public void MakeReq(string listId, string cellId)
{
Console.WriteLine("store.domain.com/" + listId + "sec01&ire=" + cellId);
}
And it gave the output:
store.domain.com/ID_10sec01&ire=Cell_1
store.domain.com/ID_10sec01&ire=Cell_2
store.domain.com/ID_10sec01&ire=Cell_3
store.domain.com/ID_10sec01&ire=Cell_4
store.domain.com/ID_10sec01&ire=Cell_5
store.domain.com/ID_10sec01&ire=Cell_6
store.domain.com/ID_10sec01&ire=Cell_7
store.domain.com/ID_10sec01&ire=Cell_8
store.domain.com/ID_10sec01&ire=Cell_9
store.domain.com/ID_10sec01&ire=Cell_10
Isn't that what you wanted?
I think there might be some side-effects in MakeReq that you are not showing us. How about adding some debug statements to your code, like this:
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
int listCellCount = listCell.Items.Count;
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() +
"&ire=1", listCell.Items[b].ToString());
//System.Threading.Thread.Sleep(5);
Debug.Assert(listCellCount == listCell.Items.Count);
}
}
}
The assertion (listCellCount == listCell.Items.Count) should always be true, unless something is changing listCell.Items.

Categories

Resources