|
Okay, now I'm just starting to build on top of what I've already written.
I get blue squiggly lines underneath:
break;
is it just waiting for me to write some more code so it has something to break to?
I haven't written any more code, yet…
<pre lang="c#">
private static void YesOrNo()
{
string userAns;
Console.WriteLine("If that is correct, press the letter y.\nIf it's not correct, press the letter n.");
userAns = Console.ReadLine();
if (userAns == "y")
{
break;
}
else if(userAns == "n");
{
Console.Clear();
tellUser();
break;
}
//Console.ReadKey();
}
</pre>
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
Nah, that's okay… I'm just practicing creating my own methods. Trying my hardest not to go any further than that for now.
Baby steps
Thanks
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
|
I downloaded it already, thanks… Just haven't taken a look at it yet.
I spent $85 on a book already, a Benjamin on a lifetime membership with learnvisualstudio.net and trying to watch and learn from some YouTube videos.
If you've been following any of my questions/posts you'll notice I'm slowly starting to dig in to more advanced features.
Slowly but surely I'm starting to piece things together.
Sometimes I need to stop and ask for directions.
|
|
|
|
|
WidmarkRob wrote: I spent $85 on a book
Use the best guess
|
|
|
|
|
|
Clearly worth of a 1-vote. Not.
Countered.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
Thanks, looking at the current score, I can only assume that the 1 came from some pond life.
Use the best guess
|
|
|
|
|
My 5 to counter the swimmer.
|
|
|
|
|
They don't like it up 'em, Captain Mainwaring!
Use the best guess
|
|
|
|
|
Hi. In theory I think is about right but for some reason it doesn't work. Any ideas? Thank you very much for your help!
public delegate void PullFunction(GameObject o);
if (Object.S1NPCDataController.Name == "name1") return false;
if (Object.S1NPCDataController.Name == "name2") return false;
if (Object.S1NPCDataController.Name == "name3") return false;
[18-1-19] Compile file : 1212.cs
[18-1-19] [COMPILER] Compilation error !
[18-1-19] Line number : 310, Error Number : CS1519, 'Symbol 'if' not valid
[18-1-19] Line number : 310, Error Number : CS1519, 'Symbol '==' not valid
[18-1-19] Line number : 311, Error Number : CS1519,'Symbol '==' not valid
[18-1-19] Line number : 312, Error Number : CS1519, 'Symbol '==' not valid
|
|
|
|
|
Make sure all if conditions are inside a method.
|
|
|
|
|
Hello,
I am new in the land of programming (C# .NET) and I am trying to develop an application that will do HttpWebRequest and HttpWebResponse to an external site.
Basically, I have a username and password for an external site. Once, I login to the external site I'm redirected to a page that has a field for me to enter an ID, and when I submit that ID I get a table with information relating to that ID(for example Name, Address, Telephone, etc.) Well, I would love to develop an application that will allow users of my application to login to the the external site from my App, enter the ID from my App, and get the result table in my App.
Any help on this will be greatly appreciated. Thanks.
|
|
|
|
|
Go to the articles section[^], here on CodeProject. You will find lots of samples to help you.
Use the best guess
|
|
|
|
|
Hi, I want to realise a Cleint/Server chat app that respects this:
1- Server connects to client;
2- server send "Hello" to client;
3- cleint responses with "Hello, ok";
4- server generates RSA Key and send public key to cleint;
5- client select a random algorithm from DES, AES(128, 192, 256) and sends it to the server;
6- server sends "GENERATE FIRST KEY" to client;
7- cleint generate a key with a size that dependse on the algorithm choosen in step 5, and encrypt it using the public key;
8- server decrypts the key;
Now the server and client can communicate using the shared key, and to make it more secure, after 5 min server send "NEXT KEY", to generate another key
Untill now I just can chat between client and server without encrypt messages, and this is my code
Server:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Server___Tutorial
{
public partial class Form1 : Form
{
int i;
TcpListener server = new TcpListener(IPAddress.Any, 1980);
NetworkStream stream;
TcpClient client;
byte[] datalength = new byte[4];
public Form1()
{
InitializeComponent();
}
public void ServerReceive()
{
stream = client.GetStream();
new Thread(() =>
{
while ((i = stream.Read(datalength, 0, 4)) != 0)
{
byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
stream.Read(data, 0, data.Length);
this.Invoke((MethodInvoker)delegate
{
txtLog.Text += System.Environment.NewLine + "Client : " + Encoding.Default.GetString(data);
});
}
}).Start();
}
public void ServerSend(string msg)
{
stream = client.GetStream();
byte[] data;
data = Encoding.Default.GetBytes(msg);
int length = data.Length;
byte[] datalength = new byte[4];
datalength = BitConverter.GetBytes(length);
stream.Write(datalength, 0, 4);
stream.Write(data, 0, data.Length);
}
private void btnListen_Click(object sender, EventArgs e)
{
server.Start();
MessageBox.Show("Waiting For Connection");
new Thread(() =>
{
client = server.AcceptTcpClient();
MessageBox.Show("Connected To Client");
if (client.Connected)
{
ServerReceive();
}
}).Start();
}
private void btnSend_Click(object sender, EventArgs e)
{
if (client.Connected)
{
ServerSend(txtSend.Text);
}
}
}
}
Client:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Client
{
public partial class Form1 : Form
{
int i;
TcpClient client;
NetworkStream stream;
byte[] datalength = new byte[4];
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
client = new TcpClient("127.0.0.1", 1980);
ClientReceive();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void ClientReceive()
{
stream = client.GetStream();
new Thread(() =>
{
while ((i = stream.Read(datalength, 0, 4)) != 0)
{
byte[] data = new byte[BitConverter.ToInt32(datalength,0)];
stream.Read(data, 0, data.Length);
this.Invoke((MethodInvoker)delegate
{
txtLog.Text += System.Environment.NewLine + "Server : " + Encoding.Default.GetString(data);
});
}
}).Start();
}
public void ClientSend(string msg)
{
stream = client.GetStream();
byte[] data;
data = Encoding.Default.GetBytes(msg);
int length = data.Length;
byte[] datalength = new byte[4];
datalength = BitConverter.GetBytes(length);
stream.Write(datalength, 0, 4);
stream.Write(data, 0, data.Length);
}
private void btnSend_Click(object sender, EventArgs e)
{
if (client.Connected)
{
ClientSend(txtSend.Text);
}
}
}
}
I also have AES, DES and RSA classes, but I don't know how to tell if a message is a key or just a normal message...
Thank you
|
|
|
|
|
The required approach might be defining a protocol and a set of commands in your protocol. Then sending and decoding the message message... Something like
KEY LENGTH KEYDATA (128 bytes or 256 depending on the length)
and decoding the same way in your client.
Note: I wouldn't send symmetric keys in plain sockets. This will defy your purpose of security as someone can easily snoop the key out and set up a man-in-the-middle trap. I would rather consider a SSL socket communication. That includes key exchange and rest intricacies out-of-the box. And I would only focus on my communication protocol.
|
|
|
|
|
Thank you for your answer, yes It s easier if I work with SSL, but this is what my prof wants, so I ve no choice
I didn't undrstand what you mean by (The required approach might be defining a protocol and a set of commands in your protocol)
Can you please give some code to undrstand better,
Thank you
|
|
|
|
|
I'm not that good in programming sockets in c# but in past I've done quite a lot in Java so some out of java base and parts of your code
The protocol:
Contains 4 keywords:
MESSAGE - Generic message <message length="">
KEY - Cipher key <key alg="" length="">
NEXTKEY - Request to generate next key <nextkey>
END - End communication <end>
Each message is terminated by a LF pair (taken from http) and data by CRLF
My idea is read every line, whenever a CRLF is detected, prepare for command parsing.
If MESSAGE command is present, parse the length and display the text that follows.
If NEXTKEY is detected send a KEY message.
If key is detected, read the key and re-initialize the cipher.
public void ServerSendMessage(string msg)
{
stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
int length = data.Length;
writer.Write("MESSAGE "+length+"\n");
stream.Write(msg);
stream.Write("\r\n");
}
public void ServerSendKey(byte[] key, string alogrithm)
{
stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
int length = key.Length;
writer.Write("KEY "+ALGORITHM+" "+length+"\n");
writer.Write(Convert.ToBase64String(key));
writer.Write("\r\n");
}
At Client
public void ClientReceive()
{
stream = client.GetStream();
StreamReader reader = new StreamReader(stream)
string message = reader.ReadLine();
while(reader.peek() > -1 || !message.Equals("END"))
{
if(message.StartsWith("MESSAGE") {
ParseMsg(message,reader);
} else if(message.StartsWith("KEY") {
ParseKey(message,reader);
}
}
message = reader.ReadLine();
}
private void ParseMsg(string cmd,StreamReader r)
{
int length = Integer.ParseInt(cmd.SubString(8));
char[] msg = new char[length];
r.ReadBlock(msg,0,length);
string message = new String(msg);
}
private void ParseKey(string cmd,StreamReader r)
{
string alg = cmd.SubString(3,3);
int length = Integer.ParseInt(cmd.SubString(9));
byte[] key = Convert.FromBase64String(r.ReadLine());
}
NOTE: Not ready for Production Code... Run tests!
|
|
|
|
|
hi guys,
is there a best practice to save an c# window service's data?
i understand that application data usually use:
-ConfigurationSettings file (appname.exe.config)
-Registry
-Custom XML file
is it the same as for services?
|
|
|
|
|
There is no such thing as a "best practice" specific to a service for saving "data". It depends on the data and the business requirements for handling it.
You also forgot files of other formats and databases in that list.
|
|
|
|
|
neodeaths wrote: is there a best practice The reason I stopped liking "best practices" is because people stop thinking once they're handed one.
How you will save the data is not dictated by the source of the data; it's how it will be used that dictates it's form. Since it's a webservice, I'd expect one to call the service again if the data is needed - getting new values.
If you simply want to cache the results; I'd recommend serializing it as a binary file. If it is part of a product that's locally installed, you check how that product saves/retrieves data. If you're going to be fetching large resultsets to be manipulating them locally, I'd suggest a local database. That is, if there's only one user. If there's more users, reading from different computers, you'd probably need a centralized database-server. Or, if security dictates - a webservice.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi
I've had a suggestion on a very basic bit'o'code and I want people's thoughts on it. The example is a voucher that has been redeemed. We produce a batch code for the voucher e.g. "ABC" and a code, e.e. "1234". The user sees the full remption code "ABC1234".
My code:
public void Foo
{
public int? Code { get; set; }
public string BatchCode { get; set; }
public string RedemptionCode
{
get
{
if(Code.HasValue)
return BatchCode + RedemptionCode;
return "";
}
}
}
Code review suggestion:
public void Foo
{
public int? Code { get; private set; }
public string BatchCode { get; private set; }
public void SetClaimedCode(string batch, int? code)
{
if(code.HasValue)
throw new AgumentException("...");
Code = code;
BatchCode = batch
}
public void GetClaimedCode()
{
return BatchCode + RedemptionCode;
}
}
My thinking is Claimed Code is a noun --> poperty. The reviewer thinks that there shouldn't be any processing in a property, as it is doing something it is a method.
Aside from the concat (which you may or may not agree with) and the visibility changes, and, to a certain extent the exception throw what do people think about the property vs method?
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
Keith Barrow wrote: public string RedemptionCode
{
get
{
if(Code.HasValue)
return BatchCode + RedemptionCode;
return "";
}
}
I hope that's a typo, otherwise you've got infinite recursion in your property getter.
I agree with making the setters for Code and BatchCode private, since you need to set them both at once, and you can only set them once. Similarly, you can't use a setter to replace the SetClaimedCode method, since it needs two values.
I'd be inclined to leave the GetClaimedCode as a read-only property, and rename the SetClaimedCode method to something like Claim or Redeem . If the code parameter is required, I'd also make it an int rather than a Nullable<int> . Also, attempting to redeem the voucher twice isn't really a problem with an argument, so an ArgumentException isn't appropriate.
public string RedemptionCode
{
get
{
return Code.HasValue ? BatchCode + Code : string.Empty;
}
}
public void Claim(string batch, int code)
{
if (Code.HasValue) throw new InvalidOperationException("Voucher has already been claimed.");
Code = code;
BatchCode = batch;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yep, typo on the recursive code should have been just Code , I actually simplified a bunch of irrelevant stuff out. I agree on the exception part, we do a lot of this sort of thing here (it's practically a return methodology for anything out-of-the-ordinary). I'm sure I read an article actually advocating this somewhere, the idea was interesting but it seemed a short route to spaghetti without discipline.
Where I'm working we have lots of code reviews and there is enough difference for the way I usually work and how they want things done that it is jarring & I'm beginning to second guess myself. One of the big things is the split between methods and properties, this is a bad example I suppose due to the write-once nature of the properties confuses the issue. One of the things I need to sort out whether a)we drink the cool-aid here a little too much or b) I've not kept up with newer thinking on how c# should be structured. Obviously a bit of both is an answer, or probably the most likely answer.
[Edit]
Forgot my manners - thanks for your suggestions.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
Keith Barrow wrote: Where I'm working we have lots of code reviews and there is enough difference for the way I usually work and how they want things done
Which only indicates that the process is flawed. Code reviews should serve the following purpose
1. Bugs
a. Logical, requirements, security etc. Result: code must be fixed.
b.Associated with assumptions in other parts of the system. Result: Determine which code should be fixed then fix it.
2. Suggestion: Where one person states their preference for how it should be done. Result: Up to the developer whether they think it is worthwhile while fixing bugs whether to touch it.
3. Comment: Where the reviewer notes that they think the implementation is worth pointing out such that others might do the same in the future. Result: Normally nothing.
Sounds to me that someone or several someones seems to think that 2 requires a mandate and thus they, not you, are missing the point of reviews. And management doesn't understand it either. Micro-management doesn't do anyone any good. And it costs money as well.
|
|
|
|
|