I have figured out most (I think) of the values for PID and RAX using NHapi for hl7 2.5.1.
What I am having difficulties on are the ones that (I am assuming) use components such as rxa.AdministeredCode.Components[5].
How do I set that value?
I assume same thing for rxa.GetSubstanceManufacturerName(0).Components? and
rxa.GetAdministrationNotes(0).
Gina
Try this
class Program
{
static void Main(string[] args)
{
EncodingCharacters enchars = new EncodingCharacters('|', "^~\\&");
IModelClassFactory theFactory = new DefaultModelClassFactory();
NHapi.Model.V251.Segment.RXA rxa = new NHapi.Model.V251.Segment.RXA(new VXU_V04(theFactory), theFactory);
IType[] t = rxa.GetSubstanceManufacturerName(0).Components;
SetRXA(t);
Debug.Print(PipeParser.Encode(rxa, enchars));
Console.Read();
}
public static void SetRXA(IType[] components)
{
for (int i = 0; i < components.Length; i++)
{
if (components[i] is IPrimitive)
{
IPrimitive prim = (IPrimitive)components[i];
prim.Value = "Component"+i;
}
else if (components[i] is IComposite)
SetRXA(((IComposite)components[i]).Components);
//if Varies do something else
}
}
}
Related
I am new to c# and I want to print object which is of List type. How can I print this object and display list in console?
Below is my code:
class Program{
public List<double> GetPowersOf2(int input)
{
var returnList = new List<double>();
for (int i = 0; i < input + 1; i++)
{
returnList.Add(Math.Pow(2, i));
}
returnList.ForEach(Console.WriteLine);//display list from method.
return returnList;
}
static void Main(String[] args)
{
Program p = new Program();
Console.WriteLine(p.GetPowersOf2(2));//not display list...
}
}
It gives error: System.Collections.Generic.List`1[System.Double]
Please suggest solution.
Thanks in advance.
Try a simple Linq and Join the outcome into a single string:
// Let's use BigInteger instead of Double to represent 2's powers
using System.Numerics;
...
int input = 12;
string report = string.Join(Environment.NewLine, Enumerable
.Range(0, input)
.Select(index => BigInteger.Pow(2, index)));
Console.Write(report);
Outcome:
1
2
4
8
16
32
64
128
256
512
1024
2048
Using Linq:
public class Program{
public List<double> GetPowersOf2(int input)
{
var returnList = new List<double>();
for (int i = 0; i < input + 1; i++)
{
returnList.Add(Math.Pow(2, i));
}
return returnList;
}
public static void Main(String[] args)
{
Program p = new Program();
p.GetPowersOf2(2).ForEach(Console.WriteLine);
}
}
So you are aware of displaying a list in the console, as you did within the method(returnList.ForEach(Console.WriteLine);). Now you returning the list object to the calling method and now you don't know how to display it, right? Why don't you try the same here, like this:
p.GetPowersOf2(2).ForEach(Console.WriteLine);
Because the variable returnList is returning from the method, so definitely the same will receive in the calling method.
Dont return the List and just call it like that:
class Program{
public void GetPowersOf2(int input)
{
var returnList = new List<double>();
for (int i = 0; i < input + 1; i++)
{
returnList.Add(Math.Pow(2, i));
}
returnList.ForEach(Console.WriteLine);//display list from method.
//return returnList;
}
static void Main(String[] args)
{
Program p = new Program();
p.GetPowersOf2(2);
}
}
OR return it like that:
class Program{
public List<double> GetPowersOf2(int input)
{
var returnList = new List<double>();
for (int i = 0; i < input + 1; i++)
{
returnList.Add(Math.Pow(2, i));
}
//returnList.ForEach(Console.WriteLine);//display list from method.
return returnList;
}
static void Main(String[] args)
{
Program p = new Program();
p.GetPowersOf2(2).ForEach(Console.WriteLine);//not display list...
}
}
var powers=p.GetPowersOf2(2);
foreach(double power in powers) Console.Writeline(power+", ");
The reason why your original code did not work is is because the default implementation of .ToString() is to simply print the name of class. Only in certain classes is this overridden.
If you want to change the default behavior of .ToString for your list you will have to derive a class from List.
public class PrintableList<T> : List<T>
{
public override string ToString()
{
string result = "";
foreach (T obj in this)
{
result += obj.ToString() + "\n";
}
return result;
}
}
Use this in place of your list in your code, and your code will work fine.
I can't permanently replace the array members. When I change the value of String Clue, the string being displayed only displays the current value of clue. I think the problem us on the initialization of char[]. I tried to put them in other parts of the code but it produces error. Beginner here! Hope you can help me. Thanks! :)
private void clues(String clue)
{
int idx = numb[wordOn]+4;
char[] GuessHide = Words[idx].ToUpper().ToCharArray();
char[] GuessShow = Words[idx].ToUpper().ToCharArray();
for (int a = 0; a < GuessHide.Length; a++)
{
if (GuessShow[a] != Convert.ToChar(clue.ToUpper()))
GuessHide[a] = '*';
else
GuessHide[a] = Convert.ToChar(clue.ToUpper());
}
Guess(string.Join("", GuessHide));
}
Edited - because you initalize GuessHide at each call of calls in your code and you do not store its current state you basically reset it each time. Still, you can make some small changes in your code like this:
private static void clues(string clue, char[] GuessHide, char[] GuessShow)
{
for (int a = 0; a < GuessHide.Length; a++)
{
if (GuessShow[a] == Convert.ToChar(clue.ToUpper()))
{
GuessHide[a] = Convert.ToChar(clue.ToUpper());
}
}
Console.WriteLine(string.Join("", GuessHide));
}
Call it like this:
clues("p", GuessHide, GuessShow);
clues("a", GuessHide, GuessShow);
Initialise GuessShow and GuessHide in the outside code like this:
char[] GuessHide = new string('*', Words[idx].Length).ToCharArray();
char[] GuessShow = Words[idx].ToUpper().ToCharArray();
public class Program
{
static string[] Words;
static string[] HiddenWords;
public static void Main()
{
Words = new string[] { "Apple", "Banana" };
HiddenWords = new string[Words.Length];
for (int i = 0; i < Words.Length; i++)
{
HiddenWords[i] = new string('*', Words[i].Length);
}
Guess('P', 0);
Guess('a', 0);
Guess('A', 1);
Guess('N', 1);
Console.ReadLine();
}
private static void Guess(char clue, int idx)
{
string originalWord = Words[idx];
string upperedWord = Words[idx].ToUpper();
char[] foundSoFar = HiddenWords[idx].ToCharArray();
clue = char.ToUpper(clue);
for (int i = 0; i < upperedWord.Length; i++)
{
if (upperedWord[i] == clue)
{
foundSoFar[i] = originalWord[i];
}
}
HiddenWords[idx] = new string(foundSoFar);
Console.WriteLine(HiddenWords[idx]);
}
}
What I'd like to do is exactly the first example from this page but...
http://nunit.org/index.php?p=testCaseSource&r=2.5
with value that I can change
static object[] DivideCases =
{
for (int i = 0; i < qtyCmd(); i++)
{
new object[] { getCmd[i] },
}
};
qtyCmd is just a static method which return a number
getCmd read a line (index sent as parameter) in a text file
where the arrays command. I know about Data-Driven Unit Test, but I was asked to do not use it. To be more specific, I am asked to do so with [TestCase]
You can turn DivideCases into a method:
private object[] DivideCases() {
var amountOfSamples = qtyCmd();
var result = new object[amountOfSamples];
for (var i = 0; i < amountOfSamples; i++) {
result[i] = new object[] {getCmd[i]};
}
return result;
}
And then use it with TestCaseSource:
[Test, TestCaseSource("DivideCases")]
public void TestMethod(object[] samples) {
// Your test here.
}
I am trying to port some code from java to C#, I have faced 2 problems so far. Here is the Java code:
public static void main(String[] args)
{
var ia = new byte[args.length];
for (int i = 0; i < args.length; i++)
try
{
ia[i] = Integer.decode(args[i]).byteValue();
}
catch (NumberFormatException e)
{
}
System.out.
println(Integer.toHexString(Calc(ia, ia.length)));
}
Obviously I have to change main to Main, length to Length but no idea about:
Integer.decode(args[i]).byteValue()
and
Integer.toHexString(Calc(ia, ia.length)).
Can someone tell me please what are the avilable options in .NET in these cases?!
Possible conversion code from java to c#.Net:
public static void Main(string[] args)
{
var ia = new byte[args.Length];
for (int i = 0; i < args.Length; i++)
try
{
ia[i] = Convert.ToByte(args[i]);
}
catch (FormatException e)
{
}
System.Console.WriteLine(String.Format("{0:X}",Calc(ia, ia.Length))); /// I assume Calc is function return something
}
You can use Convert.toInt32(string) or Parse.Int32(string)
I am pretty new to c# . Got a problem with command line arguments. what i want to do is make use of the third cmd line argument and write to it. I have specified the path of the file I want to write into and other stuffs. But the question here is can i access the command line arguments(for eg; args[3]) from user defined functions? How do we do tat? below is my code.
public class Nodes
{
public bool isVisited;
public string parent;
public string[] neighbour;
public int nodeValue;
public Nodes(string[] arr, int nodeValue)
{
this.neighbour = new string[arr.Length];
for (int x = 0; x < arr.Length; x++)
this.neighbour[x] = arr[x];//hi...works??
this.isVisited = false;
this.nodeValue = nodeValue;
}
}
public class DFS
{
static List<string> traversedList = new List<string>();
static List<string> parentList = new List<string>();
static BufferBlock<Object> buffer = new BufferBlock<object>();
static BufferBlock<Object> buffer1 = new BufferBlock<object>();
static BufferBlock<Object> buffer3 = new BufferBlock<object>();
static BufferBlock<Object> buffer2 = new BufferBlock<object>();
public static void Main(string[] args)
{
int N = 100;
int M = N * 4;
int P = N * 16;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
List<string> global_list = new List<string>();
StreamReader file = new StreamReader(args[2]);
string text = file.ReadToEnd();
string[] lines = text.Split('\n');
string[][] array1 = new string[lines.Length][];
Nodes[] dfsNodes = new Nodes[lines.Length];
for (int i = 0; i < lines.Length; i++)
{
lines[i] = lines[i].Trim();
string[] words = lines[i].Split(' ');
array1[i] = new string[words.Length];
dfsNodes[i] = new Nodes(words, i);
for (int j = 0; j < words.Length; j++)
{
array1[i][j] = words[j];
}
}
StreamWriter sr = new StreamWriter(args[4]);
int startNode = int.Parse(args[3]);
if (args[1].Equals("a1"))
{
Console.WriteLine("algo 0");
buffer.Post(1);
dfs(dfsNodes, startNode, "root");
}
else if (args[1].Equals("a2"))
{
Console.WriteLine("algo 1");
buffer1.Post(1);
dfs1(dfsNodes, startNode, "root",sr);
}
else if (args[1].Equals("a3"))
{
buffer3.Post(1);
List<string> visitedtList = new List<string>();
Console.WriteLine("algo 2");
dfs2(dfsNodes, startNode, "root", visitedtList,sr);
}
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);
Console.ReadLine();
}
public static void dfs(Nodes[] node, int value, string parent,StreamWriter sr1)
{
int id = (int)buffer.Receive();
sr1=new StreamWriter(arg
Console.WriteLine("Node:" + value + " Parent:" + parent + " Id:" + id);
sr1.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
id++;
traversedList.Add(value.ToString());
buffer.Post(id);
for (int z = 1; z < node[value].neighbour.Length; z++)
{
if (!traversedList.Contains(node[value].neighbour[z]))
{
dfs(node, int.Parse(node[value].neighbour[z]), value.ToString(),sr1);
}
}
return;
}
public static void dfs1(Nodes[] node, int value, string parent, StreamWriter sr)
{
int id = (int)buffer1.Receive();
sr.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
node[value].isVisited = true;
node[value].parent = parent;
id++;
buffer1.Post(id);
for (int z = 1; z < node[value].neighbour.Length; z++)
{
buffer2.Post(node[int.Parse(node[value].neighbour[z])]);
if (!isVisited())
{
dfs1(node, int.Parse(node[value].neighbour[z]), value.ToString(),sr);
}
}
return;
}
public static void dfs2(Nodes[] node, int value, string parent, List<string> visitedtList, StreamWriter sr)
{
int id = (int)buffer3.Receive();
sr.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
id++;
visitedtList.Add(value.ToString());
buffer3.Post(id);
for (int z = 1; z < node[value].neighbour.Length; z++)
{
buffer2.Post(node[int.Parse(node[value].neighbour[z])]);
if (!visitedtList.Contains(node[value].neighbour[z]))
dfs2(node, int.Parse(node[value].neighbour[z]), value.ToString(), visitedtList,sr);
}
return;
}
public static bool isVisited()
{
Nodes node = (Nodes)buffer2.Receive();
return node.isVisited;
}
}
So the thing is I want to write the output of each dfs to the file specified as the command line argument. So can I have access to the args in the dfs, dfs1 methods??? Thank you.
You could either keep a static field to hold it, or just use Environment.GetCommandLineArgs().
Well, in its simplest form, just save it to use later
class Program
{
static string _fpath;
static void Main(string[] args)
{
// ...stuff
_fpath = args[3];
}
static void WriteFile()
{
using(var stream = File.Open(_fpath, ...))
{
// write to file
}
}
}
Not necessarily exactly how I would do it, but you get the idea.
Also, regarding this bit of code...
this.neighbour = new string[arr.Length];
for (int x = 0; x < arr.Length; x++)
this.neighbour[x] = arr[x];//hi...works??
You can simply write
this.neighbour = arr;
Ahh, the wonders of managed code :D. No need to copy elements across to the second array. Of course, you need to consider the fact that changes to elements in the argument array (arr) will be reflected in your internal array now.
It would be better to pass arguments into functions instead of relying on some "hidden" way to pass them.
Both static variable and GetCommandLineArgs are useful to pass them in hidden way (as pointed out in other answers). Drawbacks are harder to test (since need to set static shared dependency) and less clear for future readers that there is this hidden dependency.