I'm working on some C# code where I have several files that use the same public variables (let's call these variables a, b, and c for now). Disclaimer: please don't berate me on the quality of the code, I didn't write it, it's simply my job to fix it. These variables are public and shared across all files, and should really be an array (a, b, and c all do the same thing, with slightly different properties). I am modifying the largest file of the bunch to take out all references to the variables and replace them with an array letters[], but other files contain disparate references to these variables. Is there any way to, in other files, define a variable or macro of some kind so that I don't have to change every reference in every file?
For example: In the biggest file "main.cs", I used to have:
public string a;
public string b;
public string c;
a = "a";
b = "b";
c = "c";
but I fixed it to have:
string[] letters;
letters[0] = "a";
//etc
Now, in file "small.cs", I have
a = "hello world";
b = "goodbye world";
Instead of having to go through every single file, is there any way that I could just have 'a' be defined as the first element of letters (letters[0]), b reference letters[1], etc? This way, the program would still run, and in the small files, C# would know that any reference to 'a' really means a references to letters[0].
Use a property:
public string A { get { return letters[0]; } }
Reference them from a property:
public string a
{
get
{
return letters[0];
}
set
{
letters[0] = value;
}
}
instead of writing
public string a;
public string b;
public string c;
a = "a";
b = "b";
c = "c";
write
private List<string> vals = new List<string>{ "a", "b", "c" };
public string a { get { return vals[0]; } set { vals[0] = value; } }
public string b { get { return vals[1]; } set { vals[1 = value; } }
public string c { get { return vals[2]; } set { vals[2] = value; } }
Related
I'm creating a program to generate schematics based off of user input. This has to be done dynamically/by hand due to the sheer volume of different possibilities (6.8M, growing exponentially). Right now I'm working on importing some data via CSV.
Example data:
Type,TIN_pos,TIN_ID,Desc
Elect, 0, X, Manual Regulator
Elect, 0, A, Electronic Regulator
Import code:
List<TIN_Fields> values = File.ReadAllLines("C:\\Users\\User\\Desktop\\Visual Basic\\CSV_Test_1.csv")
.Skip(1)
.Select(v => TIN_Fields.FromCsv(v))
.ToList();
public class TIN_Fields
{
public string Type;
public int TIN_pos;
public string TIN_ID;
public string Desc;
public static TIN_Fields FromCsv(string csvLine)
{
string[] values = csvLine.Split(',');
TIN_Fields _Fields = new TIN_Fields();
_Fields.Type = Convert.ToString(values[0]);
_Fields.TIN_pos = Convert.ToInt16(values[1]);
_Fields.TIN_ID = Convert.ToString(values[2]);
_Fields.Desc = Convert.ToString(values[3]);
return _Fields;
}
}
Once that data is Imported, I need to do two things with it,
display the raw csv data in a ListView table, just so users can see if anything in the list needs updating.
be able to compare the items in the list to various characters in a 10-digit hexadecimal code, and spit out some results.
First and foremost, i need to run through the list that was created with the above code, make sure that:
TIN_pos value = 0
because that is the character position of the input box.
Then, with the remaining options, look for the character represented in the input in the TIN_ID field.
Once found, it should then output the Desc field.
Everywhere I have looked says to use foreach, but that requires the array name, which is the part that is confusing me. I've tried filling in basically all of the variables in the FromCSV Method and usually get an error that the class doesn't have a definition.
to hopefully clear up confusion with my explanation, here is the code I created that does the same thing, but with the CSV data hard coded into it, using switch cases and if statements.
public partial class Form1 : Form
{
public string Model_Chassis;
public string Model_Test_Type;
public int ChannelNumberVar => Convert.ToInt32(TextBox_TIN[2]);
public string Tester_Type_Selector;
public string TextBox_TIN
{
get { return TIN_Entry_TextBox.Text; }
set { TIN_Entry_TextBox.Text = value; }
}
public string Model_Data_D
{
get { return Model_Data.Text; }
set { Model_Data.Text = value; }
}
public Form1()
{
InitializeComponent();
}
//Method grabs TIN Box data and decodes it to model information.
public void Model_Select()
{
//Picks Model Chassis
switch (char.ToUpper(TextBox_TIN[0]))
{
case 'H':
{
Model_Chassis = Coding.Model1.description;
}
break;
default:
{
Model_Data_D = "INVALID TIN";
}
break;
}
//Picks Test Type
switch (char.ToUpper(TextBox_TIN[3]))
{
case '0':
{
Model_Test_Type = Test_Types.TT_PD.TT_tt;
}
break;
case '1':
{
Model_Test_Type = Test_Types.TT_PV.TT_tt;
}
break;
default:
{
Model_Test_Type = "";
}
break;
}
//Puts chassis and Test Type together
if (Model_Data_D.Equals("INVALID TIN"))
{
;
}
else if (char.ToUpper(TextBox_TIN[2]).Equals(Coding.Num_Chan_1_2.tin_id))
{
Model_Data_D = $"{Model_Chassis}-{Model_Test_Type}";
}
else
{
Model_Data_D = $"{Model_Chassis}-{TextBox_TIN[2]}{Model_Test_Type}";
}
}
public class Coding
{
public char tin_id;
public string description;
public Coding(char TIN_ID, string Desc)
{
tin_id = TIN_ID;
description = Desc;
}
public static Coding Model1 = new Coding('H', "Model1");
public static Coding Num_Chan_1_2 = new Coding('X', "Single Channel");
public static Coding Elect_Reg_F_1 = new Coding('X', "Manual Regulator");
}
}
INPUT:
HXX0X
OUTPUT
Model1-PD
Thanks in advance for the help!
You're asking quite a few questions, and providing a lot of extra details in here, but for this:
"First and foremost, i need to run through the list that was created with the above code, make sure that:
TIN_pos value = 0
because that is the character position of the input box."
(seeing as you say you need to do this 'first and foremost').
In your FromCsv method, check the value as you create the record, and throw an error if it is invalid. Like this:
public static TIN_Fields FromCsv(string csvLine)
{
string[] values = csvLine.Split(',');
TIN_Fields _Fields = new TIN_Fields();
_Fields.Type = Convert.ToString(values[0]);
_Fields.TIN_pos = Convert.ToInt16(values[1]);
if(_Fields.TIN_pos != 0){
throw new Exception("TIN_pos must be 0");
}
_Fields.TIN_ID = Convert.ToString(values[2]);
_Fields.Desc = Convert.ToString(values[3]);
return _Fields;
}
Assuming you've read in your CSV correctly, which it seems you have, then selecting the appropriate TIN from the list is a simple LINQ statement. The following code assumes that TIN IDs are unique and only a single character in length.
static void Main(string[] args)
{
string testCsv = #"C:\Users\User\Desktop\Visual Basic\CSV_Test_1.csv";
List<TIN_Fields> values = File.ReadAllLines(testCsv)
.Skip(1)
.Select(v => TIN_Fields.FromCsv(v))
.ToList();
// Simulates input received from form
string input = "HXX0X";
TIN_Fields selectedTIN = values.First(x => x.TIN_ID == Convert.ToString(input[0]));
// Insert the description as needed in your ouput.
string output = $"{ selectedTIN.Desc }-";
}
Hopefully that answers another part of the problem. The Convert.ToString() is required because the output of input[0] is a char.
The test should check when a train is assigned to a line he losts its previous line. The class Train should implement this test in the function public void AssignTo(ILine l) by following those steps (they need to be respected):
Current assignment = l1
l1.Trains contains THIS
need to remove THIS from l1.trains
need to change current line and add train to new line
internal class Train : ITrain
{
internal Train(string name, Company company)
{
this.Name = name;
this.Company = company;
}
public string Name
{
get;
}
public ICompany Company
{
get;
}
public ILine Assignment
{
get;
private set;
}
public void AssignTo(ILine l)
{
//Current assignment = l1
var l1 = Assignment;
//l1,Trains contains THIS
this.AssignTo(l1.train)
//need to remove THIS from l1.trains
((List<Train>)l1.Trains).Remove(this);
//need to change current line and add train to new line
((List<Train>)l.Trains).Add(this);
Assignment = l;
}
}
}
[Test]
public void T2_when_a_train_is_assigned_to_a_line_he_losts_its_previous_line()
{
ICity s = CityFactory.CreateCity("Paris");
ICompany c = s.AddCompany("SNCF");
ILine l1 = s.AddLine("RER A");
ILine l2 = s.AddLine("RER B");
ITrain t1 = c.AddTrain("RER1");
t1.AssignTo(l1);
t1.Assignment.Should().BeSameAs(l1);
t1.AssignTo(l2);
t1.Assignment.Should().BeSameAs(l2);
l1.Trains.Count().Should().Be(0);
l2.Trains.Single().Should().BeSameAs(t1);
}
The problem with your current interfaces are that ILine.Trains is an IEnumerable, and you can't really remove something from an IEnumerable. In your code, you have assumed that it will always be a List, which you shouldn't really do, but if you can't change the type of ILine.Trains then I guess that's the only way.
Anyway, the reason why your code doesn't work is that you are recursively calling AssignTo for some reason. You should remove this call:
public void AssignTo(ILine l)
{
var l1 = Assignment;
// remove this line
// this.AssignTo(l1.train)
((List<Train>)l1.Trains).Remove(this);
((List<Train>)l.Trains).Add(this);
Assignment = l;
}
Although this isn't required, (it seems to be one of your requirements), you need to check whether l1.Trains contains this first:
var l1 = Assignment;
if (l1.Trains.Contains(this)) {
((List<Train>)l1.Trains).Remove(this);
}
((List<Train>)l.Trains).Add(this);
Assignment = l;
I have a linq table "KUND" who is read only to me. It has some special characters in it to which i have writter a function to switch them out to the ones i want.
public static string changeSpecialCharacters(string kund)
{
StringBuilder b = new StringBuilder(kund);
b = b.Replace("Õ", "å");
b = b.Replace("┼", "Å");
b = b.Replace("õ", "ä");
b = b.Replace("─", "Ä");
b = b.Replace("÷", "ö");
b = b.Replace("Í", "Ö");
b = b.Replace("'", " ");
b = b.Replace("¦", "´");
b = b.Replace("Ï", "Ø");
return b.ToString();
}
I now have two questions:
1 Can i add this function to the GET in the autogenerated datacontext so i dont have to call it all over my code? Ive added it but it seems to be deleted whenever i change how my datacontext is (add/remove table). 2 Any suggestions how to make that function better in regards to speed perhaps?
Never edit the .designer.cs; instead, add a second file, and use partial class to add the method, for example:
namespace Your.Namespace
{
partial class YourDataContext
{
// your methods here
}
}
No; you can't add this to the get. Another alternative, though, is an extension method:
namespace Some.Utility.Namespace
{
public static class SomeUtilityClass
{
public static string ChangeSpecialCharacters(this string kund)
{ ... } // note the "this" in the above line
}
}
Now you can use:
string name = obj.Name.ChangeSpecialCharacters();
personally I would rename this to clarify the direction of the change, and have two methods - one to encode, one to decode.
Re doing this for a set of data; perhaps:
public static IEnumerable<SomeType> ChangeSpecialCharacters(
this IEnumerable<SomeType> items)
{
foreach(var item in items)
{
item.Name = item.Name.ChangeSpecialCharacters();
item.Foo = item.Foo.ChangeSpecialCharacters();
...
item.Bar = item.Bar.ChangeSpecialCharacters();
yield return item;
}
}
probably you could initialize your variable as:
private string kund;
public string Kund
{
get
{
return changeSpecialCharacters(string kund);
}
set
{
kund = value;
}
}
I am dealing with values delimited by commas sent to me as a string. The strings come in many different structures (meaning different data types in different locations of the string as well as varying amounts of data). So while one string might be represented as:
- common data,identifier,int,string,string,string.
Another might be represented as:
- common data,identifier,int,int,string,string,string.
Design goals:
Common parse method
Common validation (i.e. int.TryParse() returns true)
Readily able to add different structures
Is there a good design pattern, or combination of design patterns, that allows me to parse the values, check them, and return an object only if the right amount of values were pulled in and those values were the expected data types?
Note: I am dealing with more than 30 different string structures.
If all the lines start with common data, identifier, and then are followed by a variable but expected (i.e. known based on the identifier) set of values, then a table approach could work well. To continue your example, say you have two different types:
common data,identifier,int,string,string,string.
common data,identifier,int,int,string,string,string.
You can build a class that defines what you're looking for:
class ItemDesc
{
public string Ident { get; private set; }
public string Fields { get; private set; }
public ItemDesc(string id, string flds)
{
Ident = id;
Fields = flds;
}
}
The Fields property is just a string that contains one-character type descriptions for the variable data. That is, "isss" would be interpreted as int,string,string,string.
You can then build a Dictionary<string, ItemDesc> that you can use to look these up:
Dictionary<string, ItemDesc> ItemLookup = new Dictionary<string, ItemDesc>
{
{ "ItemType1", new ItemDesc("ItemType1", "isss") },
{ "ItemType2", new ItemDesc("ItemType2", "iisss") },
};
Now when you read a line, use string.Split() to split it into fields. Get the identifier, look it up the dictionary to get the item descriptions, and then parse the rest of the fields. Something like:
string line = GetLine();
var fields = line.Split(',');
// somehow get the identifier
string id = GetIdentifier();
ItemDesc desc;
if (!ItemLookup.TryGetValue(id, out desc))
{
// unrecognized identifier
}
else
{
int fieldNo = 3; // or whatever field is after the identifier
foreach (var c in desc.Fields)
{
switch (c)
{
case 'i' :
// try to parse an int and save it.
break;
case 's' :
// save the string
break;
default:
// error, unknown field type
break;
}
++fieldNo;
}
}
// at this point if no errors occurred, then you have a collection
// of parsed fields that you saved. You can now create your object.
would need little more details, based on your problem domain it could entirely change. but following seem to be the first set of patterns, they are ordered on suitability.
Interpreter
Strategy
Builder
Just split them using string.Split(), and then int.Parse() or int.TryParse() each int value in the resulting array as needed.
var myStrings = string.Split(sourceString);
int myint1 = int.Parse(myStrings[0]);
There are several ways of dealing with this. Here's a simple one (outputting just an object array):
class Template
{
// map identifiers to templates
static Dictionary<string, string> templates = new Dictionary<string, string>
{
{ "type1", "isss" },
{ "type2", "iisss" },
};
static bool ParseItem(string input, char type, out object output)
{
output = null;
switch (type)
{
case 'i':
int i;
bool valid = int.TryParse(input, out i);
output = i;
return valid;
case 's':
output = input;
return true;
}
return false;
}
public static object[] ParseString(string input)
{
string[] items = input.Split(',');
// make sure we have enough items
if (items.Length < 2)
return null;
object[] output = new object[items.Length - 2];
string identifier = items[1];
string template;
// make sure a valid identifier was specified
if (!templates.TryGetValue(identifier, out template))
return null;
// make sure we have the right amount of data
if (template.Length != output.Length)
return null;
// parse each item
for (int i = 0; i < template.Length; i++)
if (!ParseItem(items[i + 2], template[i], out output[i]))
return null;
return output;
}
}
If you're interested in returning actual objects instead of just object arrays, you can put metadata into the class definitions of the objects you're returning. Then when you get the object type you look for the metadata to figure out where to find its value in the input array. Here's a quick example:
namespace Parser
{
// create metadata attribute
class CsvPositionAttribute : Attribute
{
public int Position { get; set; }
public CsvPositionAttribute(int position)
{
Position = position;
}
}
// define some classes that use our metadata
public class type1
{
[CsvPosition(0)]
public int int1;
[CsvPosition(1)]
public string str1;
[CsvPosition(2)]
public string str2;
[CsvPosition(3)]
public string str3;
}
public class type2
{
[CsvPosition(0)]
public int int1;
[CsvPosition(1)]
public int int2;
[CsvPosition(2)]
public string str1;
[CsvPosition(3)]
public string str2;
[CsvPosition(4)]
public string str3;
}
public class CsvParser
{
public static object ParseString(string input)
{
string[] items = input.Split(',');
// make sure we have enough items
if (items.Length < 2)
return null;
string identifier = items[1];
// assume that our identifiers refer to a type in our namespace
Type type = Type.GetType("Parser." + identifier, false);
if (type == null)
return null;
object output = Activator.CreateInstance(type);
// iterate over fields in the type -- you may want to use properties
foreach (var field in type.GetFields())
// find the members that have our position attribute
foreach (CsvPositionAttribute attr in
field.GetCustomAttributes(typeof(CsvPositionAttribute),
false))
// if the item exists, convert it to the type of the field
if (attr.Position + 2 >= items.Length)
return null;
else
// ChangeType may throw exceptions on failure;
// catch them and return an error
try { field.SetValue(output,
Convert.ChangeType(items[attr.Position + 2],
field.FieldType));
} catch { return null; }
return output;
}
}
}
I was looking for a way to switch my assignments i.e:
a = b;
becomes
b = a;
In case anyone is wondering, it's for loading settings and unloading them.
I crafted a regular expression for it:
Find what: {[^:b]*} = {[^;]*}
Replace with: \2 = \1
This works fine, but is there another way to load and save settings that I'm missing?
An alternative approach: what about making a settings class with a method that copies values? In that way you write the list of assignments only once:
using System;
class Settings {
public int ValueA { get; set; }
public string ValueB { get; set; }
public void CopySettings(Settings other) {
ValueA = other.ValueA;
ValueB = other.ValueB;
}
}
class Program {
static void Main(string[] args) {
Settings a = new Settings() { ValueA = 3, ValueB = "something" };
Settings b = new Settings();
// and then you can do one the following, which will copy all settings
// in both cases...
b.CopySettings(a);
a.CopySettings(b);
}
}