|
GetElementsByTagName needs "DIV" or "INPUT" but not "aa"
Cannot understand your question more to help you
|
|
|
|
|
Hi everyone
Please I need your help, I have this exercise the problem is when i run it I get incorrect results
can someone please tell me what i am doing wrong.
I am trying to take the user input and multiply it by a constant I initialized to 7.
and when I enter input i used 5, it returns 371.
the answer is actually 35.
static void Main(String[] args)
{
Console.WriteLine("Enter your dogs age");
const int years = 7;
int dogAge = Console.Read();
int humanYears = dogAge * years;
Console.Write(humanYears);
Console.ReadKey();
|
|
|
|
|
If you input 5 then 35 would be the answer. I suggest you put a breakpoint and debug the code to see what is happening.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Thank ryan...for your input
|
|
|
|
|
What you are seeing here is the fact that the value you are getting out of Console.Read is not the value you think you're getting. The value that you are getting out of Console.Read here is 53. This is because 53 is the ASCII code for 5. In other words, you're getting a single character out of Console.Read which you are casting to an integer, which gives you the underlying ASCII value. What you want to do is something like this:
int dogAge;
if (!int.TryParse(Console.Read(), out dogAge)) return; This code does the check to see if the user has entered a number, and converts it to an integer if possible.
|
|
|
|
|
Thank peter i will try that.
|
|
|
|
|
|
It worked , i had to tweak it a bit but it worked. Thanks again
|
|
|
|
|
Ah but tweaking is where the fun is, that is where you learn stuff. Give the answer an upvote if you want to show appreciation.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Hi,
I writing code for send and received data by use UDP client the send data is ok but problem in received data
1- I send this data "S11E"to program and in program display received data in label i see only "S" display in label why not display "S11E"
2- I put received code inside code button this mean cannot received any data until press the button .
can be Mark program that reads data automatically
used this code for recived data
byte[] reciveddata= udpClient.Receive(ref ipend);
string returnData = Encoding.ASCII.GetString(reciveddata);
label3.Text = ""+returnData;
in reciveddata only one byte but I send string data .
where is problem?
my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace etherent_send_and_recive_data
{
public partial class Form1 : Form
{
UdpClient udpClient;
IPEndPoint ipend;
public Form1()
{
InitializeComponent();
udpClient = new UdpClient();
}
private void send_data_through_etherent( string send_data_ETH)
{
byte[] senddataETH = ASCIIEncoding.ASCII.GetBytes(send_data_ETH);
udpClient.Send(senddataETH,4);
}
private void button1_Click(object sender, EventArgs e)
{
int port;
button1.Text = "disconnected";
port = int.Parse(textBox2.Text);
ipend = new IPEndPoint(IPAddress.Parse(textBox1.Text), port);
try
{
udpClient.Connect(ipend);
button1.Text = "Connected";
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString());
}
}
private void switch1_StateChanged(object sender, NationalInstruments.UI.ActionEventArgs e)
{
if (switch1.Value == true) send_data_through_etherent("S11E");
else send_data_through_etherent("S10E");
try
{
byte[] reciveddata= udpClient.Receive(ref ipend);
string returnData = Encoding.ASCII.GetString(reciveddata);
label3.Text = ""+returnData;
}
catch (Exception e1)
{
MessageBox.Show("ERROR");
}
}
}
}
modified 8-May-15 3:51am.
|
|
|
|
|
Your code relies on things we don't have access to, but this worked for me, if it works for you too then maybe analyse the differences (button1 connects and button2 sends)
public partial class Form1 : Form
{
Thread listenThread;
UdpClient udpClient;
IPEndPoint ipend;
bool listening;
public Form1()
{
InitializeComponent();
udpClient = new UdpClient();
}
private void send_data_through_etherent(string send_data_ETH)
{
byte[] senddataETH = ASCIIEncoding.ASCII.GetBytes(send_data_ETH);
udpClient.Send(senddataETH, 4);
}
private void button1_Click(object sender, EventArgs e)
{
int port;
button1.Text = "disconnected";
port = int.Parse(textBox2.Text);
ipend = new IPEndPoint(IPAddress.Parse(textBox1.Text), port);
try
{
udpClient.Connect(ipend);
button1.Text = "Connected";
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString());
}
listenThread = new System.Threading.Thread(StartListening);
listenThread.Start();
}
public void StartListening()
{
UdpClient c = new UdpClient(ipend);
string data = string.Empty;
do
{
byte[] reciveddata = c.Receive(ref ipend);
data = Encoding.ASCII.GetString(reciveddata);
System.Diagnostics.Debug.WriteLine(data);
if (listening)
{
label3.Invoke((MethodInvoker)delegate { label3.Text += data; });
}
} while (data != "exit");
}
private void button2_Click(object sender, EventArgs e)
{
send_data_through_etherent("S11E");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
listening = false;
send_data_through_etherent("exit");
}
}
|
|
|
|
|
Think you Dear for help me
Sir when use your code display error message
in code
UdpClient c = new UdpClient(ipend);
in
public void StartListening()
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
Additional information: The requested address is not valid in its context
How can resolve this problem
Regards
|
|
|
|
|
Make sure the IP is in textbox 1 (127.0.0.1 - it connects to itself), an unused port is in textbox2, then click button2 to connect and button2 to send.
|
|
|
|
|
thank you for Reply
Yes I Put IP in textbox1 and port in textbox2 and for check write
MessageBox.Show(textBox1.Text + " " + textBox2.Text);
and when press button the message display IP and port is correct after that also the program connected successfully
the problem when the program input to StartListening() and stop direct in code
UdpClient c = new UdpClient(ipend);
also I have anther problem the form cannot exit from button x
where the problem ?
|
|
|
|
|
Not sure what's happening TBH, it works on my machine. The listener needs to work for the form to exit properly, if it's not working then it can't listen for the command to stop.
|
|
|
|
|
Sir Now resolve the problem
I write this code
UdpClient c = new UdpClient(textBox1.Text, port);
but no any data received in Lable3
Regards
|
|
|
|
|
For sending data i use sockets a bit like this for UDP
IPEndPoint IPE = new IPEndPoint(IPAddress.Parse(this.ServerIP), this.ServerPort);
this.Soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
this.Soc.Connect(IPE);
Soc.Send(this.Request, SocketFlags.None);
buffer = new byte[2048];
int Size = Soc.Receive(buffer);
this.Reply = new byte[Size];
Soc.Close();
The server needs to look a bit like this
IP4receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IP4receiveEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), ClientPort);
IP4receiveSocket.Bind(IP4receiveEndPoint);
IPAddress IPA = IPAddress.Parse("0.0.0.0");
byte []Buf=new byte[1024];
whilst (ServiceIsRunning)
{
EndPoint RemoteEndPoint = new IPEndPoint(IPA, 0);
int RequestSize = IP4receiveSocket.ReceiveFrom(Buf, ref RemoteEndPoint);
IP4receiveSocket.SendTo(SomeByteData, RemoteEndPoint );
}
if you are using threads to send then watchout for requests that just hanging even if you set time-outs.
You could try killing the thread using an .Abort() but this won't kill them all so the best way is to use the killing thread to call code like shown below and passing in the socket.
public static void ShutMeDown(Socket Soc)
{
try{Soc.Shutdown(SocketShutdown.Both);} catch {;}
try { Soc.Close(); }catch { ;}
}
Hope this helps
|
|
|
|
|
1. there's lots of precedents in .NET itself for having classes that are non-generic (can be instantiated), and that have static "utility" methods.
2. when you write a generic class like this (deliberately abbreviated) example:
public class Node<T> : IEquatable<Node<T>> where T : IEquatable<T>
{
public List<Node<T>> Nodes { set; get; }
public int Level { set; get; }
public int ID { set; get; }
public T Value { set; get; }
public static Func<Node<T>, Node<T>, bool> GatherFunc;
public static Dictionary<int, List<int>> Gather(Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null)
{
}
} 3. The issue is that when you want to invoke those static function in the generic class:
a. you can't use Extension methods with generic classes
b. you have to use a rather elaborate syntax to invoke those methods, like:
var result1 = Node<TestClass>.Gather(XNode, Node<TestClass>.GatherMode.Values);
var result2 = Node<TestClass>.Gather(XNode, Node<TestClass>.GatherMode.Names); c. yes, you can using a 'Using directive statement, for example:
using node = May_1_2015_MultiList.Node<May_1_2015_MultiList.TestClass>; and, as you can see, that requires you fully qualify the references, even though all the classes, including Node, are in the same NameSpace.
Is there any other way to invoke the static methods using a simpler syntax ?
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"
modified 8-May-15 9:32am.
|
|
|
|
|
Hi Bill,
not sure if I'm missing your point - but why don't you do something like this?:
public enum GatherMode
{
Values,
Names
}
public class TestClass : IEquatable<TestClass>
{ }
public static class NodeExtensions
{
public static Dictionary<int, List<int>> Gather<T>(this Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null)
where T : IEquatable<T>
{
if (func == null)
func = (n1, n2) => n1.GatherFunc<T>(n2);
return null;
}
public static bool GatherFunc<T>(this Node<T> node, Node<T> otherNode)
where T : IEquatable<T>
{
return true;
}
}
public class Node<T> : IEquatable<Node<T> where T : IEquatable<T>
{
private void test()
{
var XNode = new Node<TestClass>();
var result1 = XNode.Gather(GatherMode.Values);
var result2 = XNode.Gather(GatherMode.Names);
}
}
/Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thanks for taking the time to reply, Sascha. What you suggest cannot be done:
1. If you declare the static class that holds the static Extension with a generic constraint <T> that would not compile: "Extension method can only be declared in non-generic, non-nested static class."
2. If you declare the static class that holds the static Extension with no generic constraint, then the compiler will throw an error because it cannot resolve the generic parameter.
I view this as similar to the compiler constraint that when you create a dynamic instance of Func, or Action, you have to give it Types which can be resolved: which is actually a very cool feature
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"
|
|
|
|
|
Would you have to make changes to what I posted which won't compile? Because what I posted compiles for me.
edit: after implementing IEquatable of course
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
modified 8-May-15 6:01am.
|
|
|
|
|
Hi Sacha, You are correct ! I am so delighted to find out I'm wrong
But, what's puzzling is that if I take your code example and omit the generic constraint on the 'Gather method I get the error message stating 'T does not implement IEquatable ... an error message I never saw in my experiments.
I assumed that because the 'Node class itself is constrained in the way shown in my code example ... implements IEquatable, and the 'TestClass is also constrained and implements IEquatable, that it was not necessary for me to also constrain the 'Gather method.
I am off to the gym to try and sauna and swim my way out of the 96F, feels like 106F, tropical heat of the day here (GMT+7), but, when I return, I look forward to reviewing my code and figuring out what happened.
Big thank you !
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"
|
|
|
|
|
Hi Bill!
BillWoodruff wrote: I assumed that because the 'Node class itself is constrained in the way shown in my code example ... implements IEquatable, and the 'TestClass is also constrained and implements IEquatable, that it was not necessary for me to also constrain the 'Gather method. That's true if the Gather-method is a method of Node<T> but not when trying to implement it as an extension method.
BillWoodruff wrote: I am off to the gym to try and sauna and swim my way out of the 96F Now I know where these visions come from
Just kidding, hope you had a good time!
BillWoodruff wrote: Big thank you ! My pleasure!
cheers, Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
NB: As per the code Sascha posted, you put the generic parameter and constraints on the extension method itself, not the containing class.
In other words:
public static class NodeExtensions<T>
{
public static Dictionary<int, List<int>> Gather(this Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null)
{
...
}
}
public static class NodeExtensions
{
public static Dictionary<int, List<int>> Gather(this Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null)
{
...
}
}
public static class NodeExtensions
{
public static Dictionary<int, List<int>> Gather<T>(this Node<T> node, GatherMode gathermode, Func<Node<T>, Node<T>, bool> customfunc = null) where T : IEquatable<T>
{
...
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|