|
c# is case-sensitive, so
Class is not the same as class . C# uses lower-case names for keywords, so the capitalized "Class" is not valid, you have to start it with "class".
You are also missing a } in the code you provided, it should be (in full):
namespace SkiTrip
{
class Student
{
private string name;
private decimal totalPaid;
private bool canTravel;
public void setName (string newName)
{
name = newName;
}
public string getName ()
{
return name;
}
}
}
|
|
|
|
|
Many thanks, I have been trying to sort this for hours, hopefully I wont make any more typo's
Mark
|
|
|
|
|
Use an Integrated Development Environment (IDE), the Express versions of Visual Studio are free!
|
|
|
|
|
Also do not use methods such as setName and getName , use the integrated C# set and get to access properties as described in this tutorial[^]. If you want to read a really good introduction to C# try .NET Book Zero[^].
Veni, vidi, abiit domum
|
|
|
|
|
I am looking for a working in C# that can convert English text to Arabic text before the text is passed to printer.
|
|
|
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(path);
foreach (var movie in di.GetFiles())
{
foreach (var extension in extensionsForMovies)
{
if (movie.Extension.ToUpper() == extension)
{
List1.Add(movie.Name);
}
}
}
DateTime dt = DateTime.UtcNow;
string day = dt.Day.ToString(); if (dt.Day < 10) day = 0 + dt.Day.ToString();
string month = dt.Month.ToString(); if (dt.Month < 10) month = 0 + dt.Month.ToString();
string date = dt.Year.ToString() + month + day;
index = new FileInfo(" index" + List1.Count + " [" + date + "].pls");
label1.Text = List1.Count + " files/" + nrMovies() + " saved";
}
List<string> List1 = new List<string>();
List<string> List2 = new List<string>();
List<string> List3 = new List<string>();
private void button1Create_Click(object sender, EventArgs e)
{
bool isPLSfile = false;
DirectoryInfo di = new DirectoryInfo(path);
foreach (var movie in di.GetFiles())
{
if (movie.Extension.ToUpper() == ".PLS")
{
List2.Clear(); List3.Clear();
StreamReader sr074 = new StreamReader(movie.FullName);
while (!sr074.EndOfStream)
{
List2.Add(sr074.ReadLine() + "\r\n");
}
sr074.Close();
List2.RemoveAt(0);
List2.RemoveAt(0);
isPLSfile = true;
}
}
if (isPLSfile == false) { button0Default.PerformClick(); goto End; }
string llinelist1_ = "", llinelist2_ = "";
again:
for (int i = 0; i < List2.Count; i++)
{
for (int j = 0; j < List1.Count; j++)
{
llinelist1_ = List1[j];
llinelist2_ = List2[i];
if (llinelist2_.Contains(llinelist1_))
{
List3.Add(List1[j]);
List1.RemoveAt(j);
List2.RemoveAt(i);
goto again;
}
}
}
foreach (var List1_Line in List1)
{
List3.Insert(0, List1_Line);
}
List2.Clear();
foreach (var line in List3)
{
List2.Add(line);
}
FileInfo fi = new FileInfo(index.FullName);
fi.Delete();
StreamWriter sw = new StreamWriter(index.Name, false, Encoding.Unicode);
sw.Write("[playlist]\r\nNumberOfEntries=" + List3.Count + "\r\n");
string s4563 = "";
for (int i = 1; i < List2.Count + 1; i++)
{
s4563 = "File" + i + "=" + path + "\\" + List2[i - 1] + "\r\n";
"sw.Write(s4563);
}
sw.Close();
//--------------------------------------------------
//start software
label1.Text = List3.Count + " files/" + nrMovies() + " saved";
Process.Start(path + "
End:
List2.Clear(); List3.Clear();
}
Thanks
|
|
|
|
|
So where are you facing the problem?
|
|
|
|
|
I am thinking on my code as a structure - i want my code to make it more practical, usable, coder friendly, with good structure and architecture. Maybe this is the problem. The actual code is working fine- but when i have to change something, i must spend days to debug the sh*t out.
Thanks Abhinav.
|
|
|
|
|
Being frank here: that code is rather poor.
I'm not too surprised that if you change something, the rest of it starts to fall over - you don't seem to have any consistent structure or to use any good practices.
Don't call things "list1" and "list2" - name them after what they do, just as you do in the comments.
If you only want files which match an extension, then only retrieve file which match the extension:
string[] files = Directory.GetFiles(path, "*.PLS");
If you want all lines from a file, then just read them:
string[] lines = File.ReadAllLines(path);
You can then convert this to a list with the ToList method if you need a list.
And so on - try modularising this code so that you have a method doing one task, then call it from your click handler - that way, you can test the method in isolation and be sure it works.
I'm not at all sure what your code is doing at the moment, so I'm not that surprised that you have problems maintaining it - it looks like you have hacked and slashed it a bit to get it working, then hacked it again to fix that bug, then...all without sitting down and working out what you are trying to achieve and doing a design of some form which meets that aim first.
The only instant messaging I do involves my middle finger.
English doesn't borrow from other languages.
English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.
|
|
|
|
|
_Q12_ wrote: I work at this code from a month now, and still is full of bugs. Where? Your description in the comments section states what you want your application to do but gives no useful details of what the problems are.
Veni, vidi, abiit domum
|
|
|
|
|
First, improve your readability of variables:
List<string> List1 = new List<string>();
List<string> List2 = new List<string>();
List<string> List3 = new List<string>();
List<string> moviesList = new List<string>();
List<string> savesList = new List<string>();
List<string> tempList = new List<string>();
Also, there are FORs that you could change to foreach.
Please, drop the GOTO. Although it has its uses, the same can be achieved with loop controls and it can avoid confusion.
There are no secrets to success. It is the result of preparation, hard work, and learning from failure. Colin Powell
|
|
|
|
|
Okay, I see many areas that can be improved. Here are a few enhancements that you might want to consider as they should make your life easier (and I'm not talking just about renaming your lists).
First of all, you are mixing a lot of concerns in your code. That's a fancy way of saying that your class is trying to do too much. You should simplify and specialise so that you create classes that are responsible only for their own functionality. That makes testing and fixing a lot easier.
You are relying on goto statements - I'm not going to go into a religious gotos are evil argument here as they sometimes do have their place, but in your case they are obscuring the logic.
You can simplify your GetFiles when you are retrieving multiple extensions using a simple LINQ trick:
IEnumerable<string> files = extensionsForMovies.SelectMany(filter =>
Directory.GetFiles(currentFolder, filter, SearchOption.TopDirectoryOnly));
|
|
|
|
|
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 Cv;
using CxCore;
using OtherLibs;
namespace sampleWebcam
{
public partial class Form1 : Form
{
private CvCapture VideoCapture;
private IplImage frame;
private IplImage imgMain;
public Form1()
{
InitializeComponent();
}
private void btnVideo_Click(object sender, EventArgs e)
{
double vidWidth, vidHeight;
if (btnVideo.Text.Equals("Start Video"))
{
VideoCapture = highgui.CvCreateCameraCapture(0);
if (VideoCapture.ptr == IntPtr.Zero)
{
MessageBox.Show("badtrip ah!!!");
return;
}
btnVideo.Text = "Stop Video";
highgui.CvSetCaptureProperty(ref VideoCapture, highgui.CV_CAP_PROP_FRAME_WIDTH, 640);
highgui.CvSetCaptureProperty(ref VideoCapture, highgui.CV_CAP_PROP_FRAME_HEIGHT, 320);
highgui.CvQueryFrame(ref VideoCapture);
vidWidth = highgui.cvGetCaptureProperty(VideoCapture, highgui.CV_CAP_PROP_FRAME_WIDTH);
vidHeight = highgui.cvGetCaptureProperty(VideoCapture, highgui.CV_CAP_PROP_FRAME_HEIGHT);
picBoxMain.Width = (int)vidWidth;
picBoxMain.Height = (int)vidHeight;
timerGrab.Interval = 42;
timerGrab.Enabled = true;
}
else
{
btnVideo.Text = "Start Video";
timerGrab.Enabled = false;
if (VideoCapture.ptr == IntPtr.Zero)
{
highgui.CvReleaseCapture(ref VideoCapture);
VideoCapture.ptr = IntPtr.Zero;
}
}
}
private void timerGrab_Tick(object sender, EventArgs e)
{
frame = highgui.CvQueryFrame(ref VideoCapture);
if (frame.ptr == IntPtr.Zero)
{
timerGrab.Stop();
MessageBox.Show("Wrong");
return;
}
imgMain = cxcore.CvCreateImage(cxcore.CvGetSize(ref frame), 8, 3);
cxcore.CvCopy(ref frame, ref imgMain);
cxcore.CvFlip(ref imgMain, 0);
picBoxMain.Image = highgui.ToBitmap(imgMain, false);
cxcore.CvReleaseImage(ref imgMain);
}
}
}
i have this code. but when i run this. the screen when running is always black. it does not view any picture but always black screen.
i need your help masters. i dont know what to do. (
|
|
|
|
|
Don't repost the same question just to bump it up. What you should do is step through the code in the debugger and determine where it is not giving the results you expect.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
There are a number of threads on StackOverFlow in which people disagree about the possibility of truly sorting a "standard" .NET Generic Dictionary: i.e., not an instance of the .NET SortedDictionay. Given that a .NET Dictionary is implemented with the use of Hashes for fast look-up, those who believe it cannot be ordered describe that, and the statements by Microsoft about "no guarantee" of a specific order, as the reasons sorting is not possible.
In this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Nov2_CustomShape
{
public class RandomizedDictionary
{
private Dictionary<int, int> randomDictionary;
public Dictionary<int, int> sortByKeyAsc;
public Dictionary<int, int> sortByKeyDesc;
public Dictionary<int, int> sortByValueAsc;
public Dictionary<int, int> sortByValueDesc;
private Random rand1, rand2;
private int i, j;
public RandomizedDictionary(int nKeyValuePairs)
{
randomDictionary = new Dictionary<int, int>();
rand1 = new Random((int)DateTime.Now.Ticks);
Thread.Sleep(100);
rand2 = new Random((int)DateTime.Now.Ticks);
while (randomDictionary.Count < nKeyValuePairs)
{
i = rand1.Next(0, nKeyValuePairs);
j = rand2.Next(0, nKeyValuePairs);
if(randomDictionary.Keys.Contains(i) || randomDictionary.Values.Contains(j)) continue;
randomDictionary.Add(i,j);
}
sortByKeyAsc = randomDictionary.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
sortByKeyDesc = randomDictionary.OrderByDescending(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
sortByValueAsc = randomDictionary.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
sortByValueDesc = randomDictionary.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
}
}
} A Dictionary with a specified number of Key-Value Pairs is created where each Key is taken at random from the range #0 to the number of Key-ValuePairs minus one, and the Values are also randomly assigned from the same range: no duplicates are allowed for the Values.'
If you test this:
RandomizedDictionary testDictionarySort = new RandomizedDictionary(1000); and examine the four sorted Dictionaries exposed by the Class, they appear (so far, in my tests) to be sorted as expected.
I'm curious to know if you agree with the opinion of those that believe you cannot rely on a Generic Dictionary, sorted via Linq, to be, in fact, sorted.
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
The current implementation of Dictionary keeps the order of insertion as long as nothing is ever removed. That fits this situation: the dictionaries are filled in the correct order, and that's it. No removals.
It's perfectly possible (though perhaps not 100% safe) to rely on it. I guess what it comes down to is, are you willing to take the risk?
|
|
|
|
|
Thanks for your response, Harold; what you say seems absolutely correct to me.
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
I do not see an issue with you using LINQ OrderedBy to get a sorted copy of a Dictionary.
The OrderedBy method returns: Type: System.Linq.IOrderedEnumerable(Of TSource)
An IOrderedEnumerable(Of TElement) whose elements are sorted according to a key. In the case of a Dictionary, TElement is a KeyValuePair. This is more apparent when calling the static Enumerable.OrderBy method.
System.Linq.IOrderedEnumerable<KeyValuePair<Int32, Int32>> ioe = System.Linq.Enumerable.OrderBy<KeyValuePair<Int32,Int32>, Int32>
(randomDictionary, (KeyValuePair<Int32, Int32> x) => x.Key); Bill said their argument is: Given that a .NET Dictionary is implemented with the use of Hashes for fast look-up, those who believe it cannot be ordered describe that, and the statements by Microsoft about "no guarantee" of a specific order, as the reasons sorting is not possible To this I say crawl into Reflector and see how that nasty hash is used in the Enumerator. All it is used for there is as way to tell if the Dictionary's "entries" array at the current index holds a value.
public bool MoveNext()
{
if (this.version != this.dictionary.version)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
}
while (this.index < this.dictionary.count)
{
if (this.dictionary.entries[this.index].hashCode >= 0)
{
this.current = new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].key, this.dictionary.entries[this.index].value);
this.index++;
return true;
}
this.index++;
}
this.index = this.dictionary.count + 1;
this.current = new KeyValuePair<TKey, TValue>();
return false;
}
About this statement from the documentation being a concern:For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair(Of TKey, TValue) structure representing a value and its key. The order in which the items are returned is undefined. To this, I say so what, if the order of returned KVP's is undefined. It doesn't matter what the returned order is when copying them to another class that will present them in sorted order.
Well, so much for my analysis. I could be full of it on this , but I don't think so.
|
|
|
|
|
Thanks for that detailed technical analysis, TnTinMan !
I assume that all Linq methods are static methods that require an IEnumerable to work. Perhaps I am not fully educated ?
The distinction between calling the "raw" method, and calling the method "off" IEnumerable, as you show, is not clear to me, but I will study your post carefully.
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
Quote: I assume that all Linq methods are static methods that require an IEnumerable to work. Perhaps I am not fully educated ? As far as I know, you are correct and I do hope that I did not imply otherwise. Quote: The distinction between calling the "raw" method, and calling the method "off" IEnumerable, as you show, is not clear to me, but I will study your post carefully.
All that I was trying to convey with that was that a new IEnumerable<KeyValuepair<Int32, Int32>>is returned by the query. If you run GetType.Fullname against it, you will get something like this:
System.Linq.OrderedEnumerable`2[[System.Collections.Generic.KeyValuePair`2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] This returned class is created by enumerating the source (the Dictionary).
I apologize if the following is a bit basic and long winded, but I cannot think of another way to make my point. I do not intend to insult your intellegence.
It is easy to forget how a collection is enumerated especially since the "foreach statement" replaces all the grunt work for us.
Borrowing from http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2[^].
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);
Doing it the long winded way:
IEnumerator en = peopleList.GetEnumerator();
Person p;
while (en.MoveNext())
{
p = (Person)en.Current;
Console.WriteLine(p.firstName + " " + p.lastName);
}
Each MoveNext call checks if any items remain in the collection and method Current returns the next object in the sequence.
With the classes defined as:
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
} The point of this all was to show that an IEnumerable is nothing more than a class that holds a collection of objects (Array or whatever) that provides a mechanism to retrieve those objects in a sequence order defined by the IEnumerator class. In the case of OrderedEnumerable this return order is supplied by a array that contains the backing array indices sorted as stipulated by the supplied IComparer.
Now in the case of Dictionary, I showed in my previous post the MoveNext method that indicates that the hash value is only used determine where or not particular backing array entry is holding a value. The hash value only figures in when trying to retrieve a value by key; a procedure that play no part in enumerating the dictionary.
I hope this helps clear up my ramblings from last night without making it any worse.
|
|
|
|
|
BillWoodruff wrote: you agree with the opinion of those that believe you cannot rely on a Generic
Dictionary, sorted via Linq, to be, in fact, sorted.
Doesn't matter if it is proven to be sorted because it is documented to be unreliable.
And that means that one would need to re-verify after every single maintenance release of .Net to insure that the underlying implementations did not change. So in the real world it isn't something that should be relied upon.
|
|
|
|
|
Hi Bill, not been here for a while.
I'd turn the argument on it's head: Dictionaries aren't meant to be ordered. Practically, it may be (insertion order/ key order? - I haven't checked), but by using the plain, generic dictionary, you are declaring an object that is unordered. Therefore "relying" on any inherent ordering is semantically inappropriate. There are generic OrderedDictionary and SortedDictionary types that might semantically fulfil what is required and not have the ambiguity "not guaranteeing" a specific order.
|
|
|
|
|
Hello,
I have question about burning open file to CD/DVD/...
My idea:
- some application (no my) create file and this file is open in this application, application sometimes write new data to file (for example: recording video from webcam - file is still open and application still has open file and add new frames to this file)
- My C# application burns this file to CD/DVD in the same time like application which saves data to file.
- File in this progress is still enable to read from another application.
My question is - Is this possible to do it? Which C# framework is good for this?
Thank you very much
|
|
|
|
|