|
Do you mean use <pre> so that they are displayed correctly when posting in Code Project?
eg
<PRE> public void static Main()
Brian
|
|
|
|
|
Yes, but if you use the code button, you can select the language so it highlights reserved words etc. So if I use <pre lang="c#"></pre> for some C# code it gets formatted as follows:
using System;
using System.IO;
using System.Text;
class Test
{
static void DoTest()
{
int result = 0;
Console.WriteLine("C# test result: {0}", result);
}
static void Main(string[] args)
{
for (int i = 0; i < args.Length; ++i)
{
Console.WriteLine("Arg {0}: {1}", i, args[i]);
}
try
{
DoTest();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
|
|
|
|
|
Hi Richard.
Do I use <pre lang C#> at the start of my program code
and use </pre> at the end of my code when posting my code?
Due to the changes in the time zone I'm off to bed now but will get your reply tomorrow thanks.
Brian
|
|
|
|
|
Yes, the tags are HTML tags and need to surround the text that is to be formatted. Please also ensure that the checkbox titled "Treat my content as plain text, not as HTML" below the edit box is unchecked, before posting.
|
|
|
|
|
sr is declared in Main: (and please, format your code blocks!)
public static void Main(string[] args)
{
Program prog = new Program();
StreamReader sr = new StreamReader("demo.adv"); So it is a local variable, which only exists inside the curly brackets in which it is declared: outside the Main method, it doesn't exist, and can't be accessed. And more importantly, when Main ends, it will be destroyed. What you need to do is pass it to the functions that need it:
public static void Main(string[] args)
{
Program prog = new Program();
StreamReader sr = new StreamReader("demo.adv");
Console.WriteLine("Start of Program");
prog.y = 4;
prog.ReadHeader(sr);
...
}
public void ReadHeader(StreamReader sr)
{
Console.WriteLine("Read Header");
Text1 = sr.ReadLine();
Console.WriteLine(y);
i = 6;
} The other solution is to move sr out of Main, and make it class level:
private StreamReader sr;
public static void Main(string[] args)
{
Program prog = new Program();
prog.sr = new StreamReader("demo.adv");
Console.WriteLine("Start of Program");
prog.y = 4;
prog.ReadHeader();
...
} And then all the instance methods in you class can access it.
In all seriousness, this is basic stuff, which will be explained - carefully - in the Petzold book, and you really do need to read it before embarking on this project - or you will make so many mistakes that when you look back at it in three months time you'll cringe and wish you hadn't shown 13,889,528 other people your code...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks Griff.
I was hoping that by using the word public that I could pass it (sr) to function but this must only apply to variables.
I was thinking of reading the text file using the Main program rather than in classes like you suggested but I wanted to make the program easier to understand by breaking it into classes so that all the work is done in each of the classes. However I might still keep that option open.
Due to the difference in the time zone I'm about to head to bed now.
Thanks once again for all your help.
Brian
|
|
|
|
|
public and private are access modifiers - they contain if you are allowed to access something from outside a class, they don't control how you do it.
You are really going about this the wrong way: you're trying to change a monolithic program into a "C# program" but ignoring that "the c# way" is very different and much better once you get your head round it. What you will end up with is not a good C# program, and you won't learn much that is actually useful! And if you want to use C# for "real" projects then what you are doing here won't help because it's ignoring the whole point of languages like C# (and C++, and even the .NET version of VB!).
Sleep well, and when you wake up, start reading the Petzold book - I know it's more exciting to rush right in there and code, but you will learn in a structured manner and you'll soon understand why I'm saying "this is the wrong approach".
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi Griff.
I think your referring to the structure of a C# program. I made that mistake some years ago when I tried to convert a old program written in BASIC into Visual Basic just by changing some of the commands. Years ago programs were very messy with lots of 'GOTO' commands compared to the cleaner structure of programs these days. Now I study how a program works so I can write it more constructed and not copy the program exactly in how it is written.
For me I get a certain amount of enjoyment in writing some code in C# and seeing it work without errors. The adventure compiler I'm writing is not that important to me as it's just a test program to get some practice with; chances are I might rewrite the adventure compiler differently once I've learn more about C# such as the use of functions etc.
I have a more complex program I want to convert but it will wait until I have a better understanding of C#.
Brian
|
|
|
|
|
The problem is that that teaches you nothing good - it teaches you "bad habits" - because it worked, so why not do it again - instead of "good habits" which help you to make better programs later on.
And bad habits are very hard to break!
While you can make monolithic, goto strewn, pointer infused, impenetrable spaghetti code in C# (just as in any other language) that doesn't mean it's efficient. If the code you are "converting" uses arrays because they are there you don't learn about .NET's more modern Collections which do the hard work for you for example. Or Generics, or Linq, or ... the list goes on. While you don't "need" them, being able to sort, filter, transform, and process a whole collection in a single line of readable code saves you huge amounts of time and promotes reliable code as well (That's Linq for you!).
Unless you follow a course or book, you don't come into contact with them, so you don't even know they exist. So seriously: sit down with Petzold, start reading, and do the exercises! I know it's boring, and that leaping into code is way more fun - but it makes your life a whole load easier and more fun in the future...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi Griff.
I hope to view some tutorial YouTube videos on C# as will as reading about C#.
Is PetZold the free book that someone
on CodeProject suggest that I download?
If so I've downloaded this book and have started to read it.
Brian
|
|
|
|
|
Excellent!
But ... watch out for "youtube tutorials" - most of 'em are made by people who know nothing about making a video, know little more about the subject than you do, and understand less. There are exceptions, but they are rare!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I know what you mean Griff as I've seen a few poor tutorials. Usually you can tell by comments from others on how good the tutoial is. The better ones are done over several YouTube videos.
I also have managed to get some library books but some are more for the advanced C# programmer and the beginner books seem to leave out some of the C# features. The free downloaded book on C# might be better.
Brian
|
|
|
|
|
As I'm new to C#, I'm not doing anything major at this stage.
The program I'm trying to write reads in lines from a adventure script file line by line and creates strings for the read info for each of the program classes then it writes a compiled file..
If I can get it to read a line of text then it will be a good achievement.
I find if I have a small propject to work on then it helps me to learn as I need to practice what I have learnt.
I do understand what you mean by learning to walk before you can run.
Brian
|
|
|
|
|
why is your cat sitting on your neighbour's red car, and not on your keyboard as it should be?
|
|
|
|
|
I bribed him.
Hi Luc! Been a while ... how are you doing?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm fine thanks; I am enjoying car forums, pie day, and all things "smooth and orderly"...
|
|
|
|
|
Luc Pattyn wrote: I am enjoying ... pie day
... but only to four significant digits!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
float? double? float converted to double?
|
|
|
|
|
I'm new to programming.
I've created a ListView1 in C#
This ListView1 contains the columns: Student, DOB, and Location.
There are different groups. Each student is grouped together with other students sharing the same age group.
The DOB field is in date format representing date of birth of each student.
Student DOB Location
Group1
AAA 10-05-2000 Mumbai
BBB 05-02-2000 Pune
CCC 01-01-2000 Delhi (youngest)
Group2
DDD 20-03-1999 Lucknow (youngest)
EEE 15-06-1999 Chennai
FFF 18-09-1999 Ahmedabad
Can you please help me with the Code to automatically delete all items except the youngest student in each of the groups (by comparing DOB)?
Thanks a lot in advance. :)
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Get the youngest in each group; which is the same as what you're asking.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
An amazingly inane comment. Downvoted.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Either:
1. create a structured database, and bind the ListView to it.
or
2. create appropriate class objects and bind the ListView to a collection object class that maps to the ListView column structure.
There are lots of resources here: [^]
and, on the web: [^] to help you learn how to use databinding.
Once you have the ListView databound, then you either execute a command/query on the database, or you do something like this using Linq:
IEnumerable<Student> earlydatestudents = Students.GroupBy(std => std.Group)
.Select(grp =>
{
DateTime dt = grp.Min(s => s.DOB);
return grp.Where(st => st.DOB == dt);
})
.SelectMany(slist => slist);
var toDeleteList = Students.Except(earlydatestudents).ToList();
Note the above code will possibly return more than one student ... you could have students with the same birthdate in each group. You should implement IEquatable in your 'Student Class to do comparison based on more than just the birthdate (DOB) property.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 15-Mar-19 23:34pm.
|
|
|
|
|
If you send plain Json formatted un-serialized text from a web api and have it be de-serialized in a C# function using the jsonConvert.DeserializeObject functional? I have a co-worker who created a web api in the cloud and he sends plain text formatted to look like un-serialized Json which I try to use C# functionality to Deserialize but when I try to convert what is sent into Model classes it fails. I am telling him that the C# JsonConvert.SerializeObject must be used or it won't work. Can someone help clarify this with me?
This is what is being sent:
This is what is being sent: { [ \"accesspoint\" : { \"api\" : \"test\"} ] }
modified 13-Mar-19 18:17pm.
|
|
|
|
|
That isn't valid JSON, even if you remove the "\" characters that aren't really there (they are added by VS to show the double quotes).
That doesn't mean he's trying to send you JSON at all - so talk to him, and ask him what exactly he is trying to send.
If it is JSON and he generates it himself, then he needs to generate code that will work with this: json2csharp - generate c# classes from json[^] or no "real" JSON deserializer will work.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|