|
Hi everybody, i would like some help i am studying c#, however the text book i am studying from has examples that doesnt make any sense. Can someone please give me a example of how to use property in consuming classes. Thank you very much.
|
|
|
|
|
I'm not too sure what your actual problem is. Does the following help?
class ClassWithProperties
{
public string SomeProperty { get; private set; }
public ClassWithProperties(string someProperty)
{
SomeProperty = someProperty;
}
}
class ConsumingClass
{
public string SomeOtherProperty { get; private set; }
public ConsumingClass(ClassWithProperties cwp)
{
SomeOtherProperty = cwp.SomeProperty + "foo";
}
}
If not, please clarify.
Here's a good and free PDF-book on programming in C#: http://www.introprogramming.info/[^]
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
hi to all and sorry for my bad english (im Italian).
i have a code (c#) for chat server and client using NetworksApi library. server and client works good but only in local (192.168.1.--).
what i would is the same operation but with public Ip (internet for far PC).
I tried putting the public IP and port number but nothing works.
here is my code for SERVER :
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 NetworksApi.TCP.SERVER;
using System.IO;
using System.Net;
namespace AdvanceServer
{
public delegate void UpdateChatLog(string txt);
public delegate void UpdateListBox(ListBox box,string value, bool Remove);
public partial class Form1 : Form
{
Server server;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
server = new Server("192.168.1.6","90");
server.OnClientConnected += new OnConnectedDelegate(server_OnClientConnected);
server.OnClientDisconnected += new OnDisconnectedDelegate(server_OnClientDisconnected);
server.OnDataReceived += new OnReceivedDelegate(server_OnDataReceived);
server.OnServerError += new OnErrorDelegate(server_OnServerError);
server.Start();
}
Here is the code for CLIENT :
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 NetworksApi.TCP.CLIENT;
using System.IO;
using System.Net;
namespace AdvancedClient
{
public delegate void UpdateText(string txt);
public partial class Form1 : Form
{
Client client;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ChangeTextBox(string txt)
{
if (textBox1.InvokeRequired)
{
Invoke(new UpdateText(ChangeTextBox), new object[] { txt });
}
else
{
textBox1.Text += txt + "\r\n";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
{
client = new Client();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.ServerPort = textBox5.Text;
client.OnClientConnected += new OnClientConnectedDelegate(client_OnClientConnected);
client.OnClientConnecting += new OnClientConnectingDelegate(client_OnClientConnecting);
client.OnClientDisconnected += new OnClientDisconnectedDelegate(client_OnClientDisconnected);
client.OnClientError += new OnClientErrorDelegate(clien_OnClientError);
client.OnClientFileSending += new OnClientFileSendingDelegate(client_OnClientFileSending);
client.OnDataReceived += new OnClientReceivedDelegate(client_OnDataReceived);
client.Connect();
}
else
{
MessageBox.Show("E' obbligatorio completare tutti i campi");
}
}
anybody can help me ?
Thanks, Nick.
|
|
|
|
|
It may be blocked by your firewall, or perhaps you do not have a fixed IP address. Going through an ISP on auto-generated IP addresses does not work.
|
|
|
|
|
thanks Richard
so .... which could be the solution?
|
|
|
|
|
I connected my second PC with the phone (hotspot) and I identified public IP replacing it immediately to the local
|
|
|
|
|
That will not work, the 'public' IP only gets as far as the service provider. They then re-route the data to your broadband connection. If you wish to use a socket connection across the internet then you need to get a fixed IP address from your service provider.
|
|
|
|
|
|
I solved the issue.The 4.5 framework does not support destructor.
If someone use the destructor in the class file the solution build properly but the error occur when service is running.So it removed the destuctor and re-build.
Narayan Mitra.
Software Developer.
Narayan Mitra.
Software Developer.
|
|
|
|
|
How to Retrieve Image binary format data store in image
show the image Gridview
|
|
|
|
|
Use a BinaryWriter object.
Small Example:
using (var filestream = File.OpenWrite(file))
{
BinaryWriter bw = new BinaryWriter(filestream);
bw.Write(imagefieldhere, 0, (int)imagefieldhere.Length);
bw.Close();
}
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
|
I am writing an application with Visual Studio 2013 C# that will have a password to enter the program. If the password entered is incorrect, I want to let them know and then close the application. I was trying:
public myapp() {
if (yourguess != password) {
messagebox('sorry');
application.exit();
}
}
But that showed the message and then went on into the program. I want it to show the message and then everything just goes away. Did it in Java, but trying to get it in C#.
|
|
|
|
|
Easiest way?
Look in your program.cs file: it starts your app with the line:
Application.Run(new myMainForm());
One way to do it is to replace that with a form which asks for the password.
Handle teh OK button, and if the user enters it incorrectly, call Form.Close and the app will end.
If he doesn't:
Hide();
myMainForm mf = new MyMainForm();
mf.ShowDialog();
Close(); Will show the main form.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Are you sure that this method doesn't have nasty side effects? Looks a bit ... hacky
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
It's less hacky than opening the password entry form in the form constructor / load event - and if the wrong password is entered the main app doesn't really start at all.
If (as it appears to me from his question) he has the rest of the app in place, it's a less intrusive solution as well.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I don't find an ApplicationContext hard to implement or intrusive.. but if you say this method can't backfire I'll take your word for it
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thx, OriginalGriff, that gave me the idea to be able to get it to work. I added the code for the password to the Main() in program.cs and it worked exactly how I wanted it to. You don't get it right and it lets you know and closes. If you do, it lets you in. Thx!
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I think that is a bad design! The user has to restart the app if he gets the password wrong. I always allow 3 tries before closing the application, caters for the dyslexic idiots we have as users!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello.
You may Use this.close() method for this.
It will close your window form.
Thanks.
|
|
|
|
|
Hello all,
as an old fashioned c-programmer I'm new to c# and this results in not understanding how to distribute data read from RS232c port (ASCii) to local file and via http (get) to my HTTP-Server on the other edge of our science world ...
I fond some examples how to read data from RS232 which works fine - I found an example how to write to disk ( aka file), but how do I combine this in one Process (foreground or background is irrelevant)? How do I split the data to file and ftp that come from serial port?
Many thanks for your help
Werner
|
|
|
|
|
Depends on exactly what you are trying to do: both operations individually are pretty simple! It's when you try to combine them that it gets more complex - and there are thousands of possible things you could be trying to do.
If you are looking at storing everything you receive to a disk as it comes in, then that;s reasonably trivial: handle the SerialPort.DataReceived[^] event and write it to a permanently open class level stream as byte data. As long as you call Stream.Flush[^] periodically (so that if your app crashes you don't loose the data) you should be fine.
Quite where FTP comes into the task, I'm not sure...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thank you OriginalGriff,
I found this sniplet which takes the data and displays it in an GUI-Text-Window ...
My Idea was to follow the description in the comment "Send data to whom ever interested", but I can not catch the provided data in an different procedure/function anymore
<pre lang="c#">void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
// Send data to whom ever interested
if (NewSerialDataRecieved != null)
NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}
</pre>
Where and how do I connect to "data" again?
I tried new functions and I tried from within the existing function ,... but I cannot find the data
Thank you
Werner
|
|
|
|
|
What do you mean "I can not catch the provided data in an different procedure/function anymore"?
I assume this is part of a class / control and NewSerialDataRecieved is an event of yours? What is handling the event? How have you linked the class to the handler method?
Does the handler have a try...catch block that swallows exceptions? If so, is it accessing any UI controls within the block?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|