|
That description sounds more like a Windows Form application than a Console. Perhaps you should switch to a framework that makes the use of those features easier.
|
|
|
|
|
Is the console window part of your own app or is it an external to your app?
|
|
|
|
|
I wrote a program in C# that runs in Visual Studio as a console application.
When I run the program, in debug, it runs in a console window.
|
|
|
|
|
OK, so then why are you trying to copy'n'paste your own window text?
You're going to have to rethink your code and write a module that can write the required text to both the console window and to the file you want to capture the data to.
|
|
|
|
|
I change over to using StreamWriter. I have used it before, I wasn't thinking.
using System.IO;
using (StreamWriter list = new StreamWriter("C:\\Users\\Gene\\Desktop\\Test.scr", true)) {
list.WriteLine("Line " + cabinetx + "," + topy + " @" + cabinetWidth + ",0\n");
}
I changed from Console.WriteLine( ) to list.WriteLine( )
Works great. Beautiful thing. It doesn't print to screen but it does write to a text file.
I use progeCad. (similar to AutoCad)
I saw this online. Running script in Cad program
It draws a 1" square box. And it did it all in 1 line of code.
Line 0,0 @1,0 @0,1 @-1,0 close
Starts a line command. Space is like hitting the spacebar or Enter key.
Where do you want to start from? x,y coordinate 0,0. Space
What's the next point? @1,0. Space. and so forth
After that, I drew a cabinet, used it as a model to gather locations and sizes of Left end, right end, top, deck and measured for precise locations and sizes.
From those measurements I typed out these commands
Rectangle 0,4 @0.75,30.5
Rectangle 30,4 @-0.75,30.5
Rectangle 0.75,34.5 @28.5,-0.75
Rectangle 0.75,4 @28.5,0.75
Saved as a script file (.scr) and ran in progeCad as script file
It drew out in a split second what I had drawn previously for a model
After that, it was like candy. What other parts can I draw?
The program is two parts
First part gathers information- width, height, depth. Top drawer and 1 door, with 2 adjustable shelves. This is done thru user input, to change location, sizes and choice of parts.
Numerous windows display, allowing changes. Window is cleared and the next window is diplayed...
Second part is where all the commands code is generated. (similar to lines of code above)
I copy the command codes, paste in a text file, save as script file and run in progecad
Each line, represents one full command in progeCad. The first line runs and jumps to the next line until the end. It just drew several views of the cabinet.
Using StreamWriter, no more copy and pasting.
Thanks again.
|
|
|
|
|
OK, so what's wrong with adding a Console.Write immediately after your write to the file?
using (StreamWriter list = new StreamWriter("C:\\Users\\Gene\\Desktop\\Test.scr", true)) {
string content = "Line " + cabinetx + "," + topy + " @" + cabinetWidth + ",0\n";
list.WriteLine(content);
Console.Write(content);
}
|
|
|
|
|
I need to save the text that comes up on the last screen displayed.
It's the whole purpose of the program, to generate the text displayed on the last screen.
I don't need to see it. I do need to copy and paste it.
Before, I have printed the text to the last screen for one reason.
To select all- and copy to clipboard then to paste and save in a text document.
I needed it displayed so I could copy it.
Now, using StreamWriter, that information is written to a text file and saved
I no longer have to copy and paste. I don't need to see it.
And if I want to see the text from the last page, I can open the text file. It's an exact copy
There are several hundred lines I need to change from Console.WriteLine to list.Writeline
I have changed several methods that are writing to the text file. It's working as expected. Thanks
modified 10-Sep-19 22:10pm.
|
|
|
|
|
Greetings all - first time poster here, so please forgive me if I've submitted this incorrectly
A user selects from a list of enum. I want that enum to be the actual name of a var. Then I'd like to use (some kind of placeholder argument in a method), to call that selected enum and evaluate the method. Here is the relevant example section:
{
public enum Fruit
{
Apple, Orange
}
[Parameter("Fruit Type")]
public Fruit FruitType { get; set; }
public override void Calculate
{
var Apple = 5
var Orange = 1
bool MoreApples = Apple > Orange;
bool FewerApples = Apple < Orange;
if (FruitType == Fruit.Apple && MoreApples)
{
}
if (FruitType == Fruit.Orange && FewerApples)
{
}
Is there some better way to assign the enum to a var (or other way around??), and then have a Method call the chosen enum, and return the actual bool data in order to see if the IF is satisfied? Perhaps this is an array thing?
I was hoping to use something like: FruitType == Fruit.XXX, with XXX pulling selected enum.
Thank you all for helping the C# newbie -
|
|
|
|
|
Quote: I want that enum to be the actual name of a var.
Nope. You don't get to do that. The values in an enum should not be thought of as variable names at all. Expanding that a little, if you have a string variable with the name of a variable as its value, that's also wrong.
It may work in interpreted languages, but it doesn't work in compiled languages. The variable names no longer exist(*).
* There is, of course, an exception. The namespaces, classes, method names, variables, and other code objects/names ARE available as metadata in .NET code. You access that data through the System.Reflection classes. Using reflection in your code will WAY over-complicate your code and reduce its performance. Reflection is not a quick thing. Don't do it unless you have a very specific reason and no other options.
In your example, your Calculate method should accept a parameter, FruitType. You don't appear to need the property at all. Of course, this makes assumptions that you don't show in your code nor explain in your question.
public override void Calculate(FruitType fruitType)
A method with the name Calculate also suggests it should return a value, which your code doesn't.
|
|
|
|
|
Dave Kreskowiak wrote: ... values in an enum should not be thought of as variable names at all ... It may work in interpreted languages, but it doesn't work in compiled languages. The variable names no longer exist(*). Hi, Dave, I take exception to this conclusion in the limited sense that we can use Enum values as variables which function as indexes to deferred execution code, as shown in my response on this thread.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
While you are correct, I limited my answer to the context of "beginner". Yeah, it's a little white lie, for the sake of simplicity and learning the basics first.
|
|
|
|
|
Dave Kreskowiak wrote: context of "beginner" good point, Dave ! I've added a second reply here showing use of a 'switch statement ... hope it be useful to the OP.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Dave -- Thank you VERY much for the explanation-
I am such beginner, I am having trouble knowing what I yet don't know (which is mostly everything).
1) a user selectable list of things (enums - or some other way?)
2) var(s) initialized that calculate something
3) an IF that wants to evaluate whether some var && some other var are true), and if so, do something.
as it is now, I have to hard code all possibilities for each if (of each selected enums) using the vars directly. What I'd like is a way to have the user selected choice populate the iF with the appropriate var - that would alleviate hard coding each and every possibility.
Am I even phrasing the question properly? probably not -- And if not, can you point me to an area I should study as to be better informed about syntax and possibilities? If I am way off, many apologies...
Thanks so much again for your help-
|
|
|
|
|
See if the code using a 'switch statement in my second reply seems useful.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
I am going to guess that what you are after is something like this: using a Dictionary whose Key is an Enum value, and whose Value is executable code defined in a deferred form (Func, Action, Delegate, Lambda Expression):
using System;
using System.Collections.Generic;
namespace YourNameSpace
{
public enum Fruit
{
NoFruit, Apple, Orange
}
public class FruitCalc
{
private Dictionary<Fruit, Func<int, int, Fruit>> CalcFruitDict
= new Dictionary<Fruit, Func<int, int, Fruit>>
{
{Fruit.Apple, (int napples, int noranges) => napples > noranges ? Fruit.Apple : Fruit.NoFruit },
{Fruit.Orange, (int napples, int noranges) => napples < noranges ? Fruit.Orange : Fruit.NoFruit },
};
public Fruit Calculate(Fruit fruit, int napples, int noranges)
{
if (fruit == Fruit.NoFruit)
{
throw new InvalidOperationException("Sorry, no fruit means no fruit");
}
return CalcFruitDict[fruit](napples, noranges);
}
}
} Usage example:
FruitCalc fcalc = new FruitCalc();
Fruit f1 = fcalc.Calculate(Fruit.Apple, 12, 4);
Fruit f2 = fcalc.Calculate(Fruit.Orange, 14, 24);
Fruit f3 = fcalc.Calculate(Fruit.Apple, 2, 4);
Fruit f4 = fcalc.Calculate(Fruit.Orange, 14, 2);
Fruit f5 = fcalc.Calculate(Fruit.NoFruit, 12, 4); Note: the case where number of apples equals number of oranges is not handled here.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
This is a simpler version using a 'switch statement:
public Fruit Calculate2(Fruit fruit, int napples, int noranges)
{
switch (fruit)
{
case Fruit.Apple:
return napples > noranges ? Fruit.Apple : Fruit.NoFruit;
case Fruit.Orange:
return napples < noranges ? Fruit.Orange : Fruit.NoFruit;
default:
throw new InvalidOperationException("Sorry, no fruit means no fruit");
}
}
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
To add to what the others say: never rely on variable names in your code in the way you seem to be describing. While (as Dave says) that can be done, it does have a significant drawback in the real world.
Namely: it may fail spectacularly in production. It's quite common to use obfuscation to "protect" source code from re-generation from the EXE file - which is certainly possible in C# thanks to the metadata that Dave mentioned.
But obfuscation throws away that metadata and substitutes a randomised version to make it a lot harder to read. And there go your enum names, your variables, even your method names (unless they are directly exposed outside the assembly).
Generally, if you are trying to do that, you are over complicating your software, and probably should sit down and think very carefully if that is the way you need to go.
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!
|
|
|
|
|
|
In your UI, you have an event handler for the dropdown list, so we'll start from that perspective.
public void DropDownSelectChanged(object sender, EventArgs args)
{
var ddl = sender as DropDown;
Calculate(ddl.SelectedItem);
}
The Calculate method will then call the appropriate Action to react to the state of the application.
public Action MoreApplesAction {get; set;}
public Action FewerApplesAction {get; set;}
public void Caclulate(Fruit selectedFruit)
{
var Apple = 5
var Orange = 1
var MoreApples = Apple > Orange;
var FewerApples = Apple < Orange;
switch(selectedFruit)
{
case Apple:
if(MoreApplies) MoreApplesAction?.Invoke();
break;
case Orange:
if(FewerApples) FewerApplesAction?.Invoke();
break;
}
}
The Sharp Ninja
|
|
|
|
|
Dear Folks,
Im a newbie to this forum and appreciate i could get a guidance for my above subject.
I have a csv file ABC.Txt file with contents as indicated below
Name, Age, TelNum, Emailid
Chris, 42, 5555, chris.walker@gmail.com
Rob, 35, 6666, rob.randell@gmail.com
from my C# Script, i want to vlookup by name and get info like age, TelNum or EmailId.
e.g for Chris, if I query for Emailid , i must get his emailid chris.walker@gmail.com.
Appreciate if Could i get a typical code example for this
Cheers!!
Venkat
|
|
|
|
|
Try this: A Fast CSV Reader[^]
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!
|
|
|
|
|
Assuming you are someone who really wants to learn C#, consider this pattern:
1 define a Class for 'Person:
public class Person
{
public Person(string name, int age, int telNum, string emailId)
{
Name = name;
Age = age;
TelNum = telNum;
EmailId = emailId;
}
public string Name { get; set; }
public int Age { get; set; }
public int TelNum { get; set; }
public string EmailId { get; set; }
}
public static class PersonFactory
{
static char[] delimiter = new []{','};
public static Person MakePerson(string line)
{
try
{
var data = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
var name = data[0];
var age = Int32.Parse(data[1]);
var tel = Int32.Parse(data[2]);
var eml = data[3];
return new Person(name, age, tel, eml);
}
catch (Exception ex)
{
throw new InvalidDataException(ex.Message);
}
}
public static List<Person> PersonsFromFile(string filepath)
{
var lines = File.ReadAllLines(filepath).Skip(1);
return lines.Select(line => MakePerson(line.Trim())).ToList();
}
} Sample usage:
var persons = PersonFactory.PersonsFromFile(@"C:\Users\###\##\##?.txt");
int chrisage = persons.First(p => p.Name == "Chris").Age;
«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 1-Sep-19 22:04pm.
|
|
|
|
|
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.
Code in C#
using System;
using System.IO;
using System.Globalization;
namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine();
cmt = _sr.ReadLine();
dash = _sr.ReadLine();
cmt = _sr.ReadLine();
dash = _sr.ReadLine();
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine();
cmt = _sr.ReadLine();
dash = _sr.ReadLine();
MandW = new matrix(x,2);
for (int i= 0, i<x, i++)
{
values = IO.split_string(_sr.ReadLine());
for (int j = 0; j < 2; j++)
{
MandW[i,j] = Convert.ToDouble(values[j], _ci);
}
}
Time = new matrix(y,1);
VandA = new matrix(y,2);
Distance = new matrix(y,1);
}
}
}
}
I am having trouble constructing these matrices..
Thank you in advance.
modified 29-Aug-19 3:31am.
|
|
|
|
|
Member 14571520 wrote: I am having trouble constructing these matrices..
What trouble?
What does it do that you didn't expect, or not do that you did?
Any error messages?
What have you tried to fix it?
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!
|
|
|
|
|
Your main problem seems to be that you do no checks on the data you are reading. You should check the content of each line to ensure it contains the elements you expect. You should also check the result of your calls to IO.split_string ; and why are you not using string.split() ? And do not use Convert to convert strings to numbers, use the TryParse() method for each type; that way you will not be caught out by bad data. And finally, if you look at your data the lines containing the mass and weight values have a line number at the beginning which will confuse the converter: 10. 4.0 5.0 3.56 4.78 5.098 2 .
|
|
|
|
|