|
Hi
don't know if this is the right place where to post.
But i was wondering if is possible to use switch statement with multiple variables, something as below :
int x = 0;
int y = 1;
switch(x,y)
{
case 1,0 :
Console.WriteLine("1,0");
break;
case 0,1 :
Console.WriteLine("0,1");
break;
case 1,1 :
Console.WriteLine("1,1");
break:
default :
Console.WriteLine("0,0");
}
thanks bye
"For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras
|
|
|
|
|
No.
Did you check the documentation? the language specification?
You could make a composite function, in your example int z=2*x+y would suffice; but readability would suffer.
And you could make a new type, hide the values in there, spend a lot of code and waste numerous CPU cycles. What is it good for?
The obvious approach is a series of if ... else if
|
|
|
|
|
Thaks for the reply..
The obvious approach is a series of if ... else if
i agree!
You could make a composite function, in your example int z=2*x+y would suffice; but readability would suffer.<code><br />
<br />
i used int type variables just for example, actually i'm working with strings.<br />
<br />
<br />
<br />
Thanks again!! :) <br />
bye<br />
<div class="signature">"For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras</div>
|
|
|
|
|
jadughar wrote: actually i'm working with strings
Aha. Strings probably are the only situation where I might consider it, using a concatenation, i.e. calculate x+"#"+y at run-time, and have "0#1" and the like as switch case values (C# supports string-based switches, a lot of languages don't). It might be acceptable as the cost is limited and readability does not suffer at all.
|
|
|
|
|
Good idea!!!!
Thanks Luc!!
"For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras
|
|
|
|
|
you're welcome.
|
|
|
|
|
Luc Pattyn wrote: a lot of languages don't
And for good reason too.
|
|
|
|
|
You could convert it to a string, and switch to the correct label (like "0,0"). This[^] blogentry might be interesting if you're considering that option
I are Troll
|
|
|
|
|
Obviously not, as I'm sure you would have found out by trying to compile this.
Fortunately, switch can be nested.
untested:
int x = 0;
int y = 1;
switch (x)
{
case 0:
switch (y)
{
case 0:
Console.WriteLine("0,0");
break;
case 1:
Console.WriteLine("0,1");
break;
}
break;
case 1:
switch (y)
{
case 0:
Console.WriteLine("1,0");
break;
case 1:
Console.WriteLine("1,1");
break;
}
break;
}
|
|
|
|
|
Having read the other reponses, first off I'll say that I very much dislike switching on strings.
How much do these values vary? Are they coordinates for a small grid? Is the grid size defined?
I would prefer to define an enumeration to hold the values, for instance:
public enum Coord
{
XY00 = 00
,
XY01 = 01
,
XY10 = 10
,
XY11 = 11
}
and then switch on that.
|
|
|
|
|
you are right..but in my case, a switch on strings was suitable..
anyway thanks to all
"For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras
|
|
|
|
|
PIEBALDconsult wrote: I very much dislike switching on strings
Out of curiosity, why is that?
|
|
|
|
|
One big reason is case-sensitivity.
But mainly because it's a symptom of a bad design. Most times I see or would use a switch on a string, there's a better way. I much prefer to have an enumeration. As a plus, the enumeration Parse method accepts a boolean to do case-insensitive parsing.
It's for what my EnumTransmogrifier[^] was created.
|
|
|
|
|
I may be totally missing the point of your question (I usually do ) but wouldn't
Console.WriteLine("{0},{1}", x, y);
do that for you?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Nope. See default .
|
|
|
|
|
I saw it.
Implementing value checking for assignments to 'x' and 'y', using properties perhaps, would obviate the necessity though.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
HI,
May i know,
What are the prerequisites to learn sharepoint?
Iam only C, C++, VC++ Programmer Having 5 years of IT Experiance.
Thanks & Regards,
Krishna Rayalu TV
|
|
|
|
|
Asking in this[^] forum would be a better bet.
|
|
|
|
|
As Pete has said posting to the SharePoint forum would have been better, however, since you've already begun.
First, since you have no background in .NET, start there. The default language for SharePoint is C# so dig in. After that, since SharePoint is based on ASP.NET you will need to learn and understand it. After, and ONLY AFTER, you have these foundations, then you can start learning SharePoint.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Sorry I made a mistake in the previous post. I highlight tho point in the code with the error with the bold characters.
Hello
I am writing very simple code with C# to read and write data from Rockwell Automation PLC through their communication server (OPC srever) RSLINX.
I can read data but I cannot write because executable exit with error when I call the
RSLinxOCgroup.SyncWrite(item, ref syncserverhandles, ref datatowritetoplcmemory, out SyncErrors)
(dll: RsiOPCAuto.OPCGroupClass.SyncWrite(Int32 NumItems, Array& ServerHandles, Array& Values, Array& Errors) )
error : Specified array was not of the expected type.
the error, I suppose, refers to the datatowritetoplcmemory
that is declared as :
System.Array datatowritetoplcmemory;
datatowritetoplcmemory = Array.CreateInstance(System.Type.GetType("System.Int32"), 10)
and then filled for example like this:
for (i = 0; i < 10; i++)
{ datatowritetoplcmemory.SetValue (i * i, i) }
Can somebody help me?
Thank you
Best regards
rodrigo
modified on Friday, December 10, 2010 10:49 AM
|
|
|
|
|
You didn't need to post again. You can edit your posts if they are incorrect. Please use the pre tags for code snippets and remove the other post as it serves no point.
|
|
|
|
|
I've been a Rockwell user for many years, sounds like a good project.
I'd think that your code should interface with any OPC server and not necessarily specific to RSLinx, that's the point right?
|
|
|
|
|
I think you addressed this to the wrong person.
|
|
|
|
|
Hello
Sorry for my late reply.
In this case the application is dedicated to the communication with RSLinx.
Here I attach the code where I cut many non intersting lines of code so it will not run if you try.
Then if yo need I can attach the complete version.
Anyway the lines that i am interested in are highlighted.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Timers;
using RsiOPCAuto;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace opc_ali
{
class Program
{
static RsiOPCAuto.OPCServer RSLinxOPCserver;
static RsiOPCAuto.OPCGroup RSLinxOPCgroup;
static RsiOPCAuto.OPCItem RSLinxOPCItem_1;
static RsiOPCAuto.OPCItem RSLinxOPCItem_2;
static RsiOPCAuto.OPCItem RSLinxOPCItem_3;
static RsiOPCAuto.OPCItem RSLinxOPCItem_4;
static int prec_sec_bl = 0;
static int prec_millis_bl = 0;
static int TimeNow_Sec, TimeNow_mSec;
static int tempo_p, tempo_s, delta;
static int[] DDE_IN_INT = new int[50];
static int[] DDE_OUT_INT = new int[50];
static double[] DDE_IN_DOU = new double[50];
static double[] DDE_OUT_DOU = new double[50];
static int numero_word_I;
static int numero_word_O;
static int tempo_campionamento;
static int numero_campioni;
static int new_line;
public static void Main(string[] args)
{
try
{
RSLinxOPCserver = new RsiOPCAuto.OPCServer();
RSLinxOPCserver.Connect("RSLinx OPC Server", "");
RSLinxOPCgroup = RSLinxOPCserver.OPCGroups.Add("Prova");
RSLinxOPCgroup.IsActive = true;
RSLinxOPCgroup.UpdateRate = 50;
RSLinxOPCgroup.IsSubscribed = true;
RSLinxOPCItem_1 = RSLinxOPCgroup.OPCItems.AddItem("[ali]Program:scrittura_dati_per_out.dde_OUT_INT[0],L1,C1", 1);
RSLinxOPCItem_2 = RSLinxOPCgroup.OPCItems.AddItem("[ali]Program:scrittura_dati_per_out.dde_OUT_INT[0],L" + numero_word_I + ",C1", 2);
RSLinxOPCItem_3 = RSLinxOPCgroup.OPCItems.AddItem("[ali]Program:scrittura_dati_per_out.dde_IN_INT[0],L1,C1", 3);
RSLinxOPCItem_4 = RSLinxOPCgroup.OPCItems.AddItem("[ali]Program:scrittura_dati_per_out.dde_IN_INT[0],L" + numero_word_O + ",C1", 4);
Lettura_scrittura_sincrona();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Errore",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static void Lettura_scrittura_sincrona()
{
int itemcount;
int i;
int num_file = 0;
int num_righe = 1;
System.Array SyncErrors;
System.Array SyncValues;
System.Array SyncErrors2;
Object SyncQualities = null;
Object SyncTime = null;
string fileName;
string rigafile = "";
int anno;
int mese;
int giorno;
string cod_file = "";
new_line = numero_word_I * 10 + 34;
Int32[] DatiRegistrati = new Int32[numero_word_I];
byte[] byte_riga = new byte[new_line * numero_campioni];
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
try
{
itemcount = RSLinxOPCgroup.OPCItems.Count;
System.Array SyncServerHandles;
SyncServerHandles = Array.CreateInstance(System.Type.GetType("System.Int32"), 4);
System.Array DatiDaInviare;
DatiDaInviare = Array.CreateInstance(System.Type.GetType("System.Int32"), 1);
for (i = 0; i < itemcount; i++)
{
SyncServerHandles.SetValue(RSLinxOPCgroup.OPCItems.Item(i + 1).ServerHandle, i);
}
anno = DateTime.Now.Date.Year;
mese = DateTime.Now.Date.Month;
giorno = DateTime.Now.Date.Day;
TimeNow_Sec = DateTime.Now.Second;
TimeNow_mSec = DateTime.Now.Millisecond;
tempo_p = (prec_sec_bl * 1000) + prec_millis_bl;
tempo_s = (TimeNow_Sec * 1000) + TimeNow_mSec;
delta = tempo_s - tempo_p;
prec_sec_bl = TimeNow_Sec;
prec_millis_bl = TimeNow_mSec;
while (true)
{
num_file += 1;
num_righe = 1;
cod_file = "_" + anno.ToString() + "_" + mese.ToString() + "_" + giorno.ToString() +
"_" + num_file;
fileName = "C:\\Archivio_registrazioni_Ali\\registra" + cod_file + ".txt";
if (!File.Exists(fileName))
{
FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.Write);
fs.Close();
}
for (num_righe = 1; num_righe < numero_campioni + 1; num_righe++)
{
for (i = 0; i < numero_word_I; i++)
{
DatiRegistrati[i] = 0;
}
RSLinxOPCgroup.SyncRead(1, 1, ref SyncServerHandles, out SyncValues,
out SyncErrors, out SyncQualities, out SyncTime);
DatiRegistrati = (Int32[])SyncValues.GetValue(1);
TimeNow_Sec = DateTime.Now.Second;
TimeNow_mSec = DateTime.Now.Millisecond;
rigafile = String.Format("{0:00000}", num_righe);
encoding.GetBytes(rigafile).CopyTo(byte_riga, ((num_righe - 1) * new_line) + 0);
for (i = 0; i < numero_word_I; i++)
{
if (DatiRegistrati[i] < 0)
{
rigafile = String.Format("{0:00000000}", DatiRegistrati[i]);
encoding.GetBytes(rigafile).CopyTo(byte_riga, ((num_righe - 1) * new_line) + 32 + (i * 10));
rigafile = ";";
encoding.GetBytes(rigafile).CopyTo(byte_riga, ((num_righe - 1) * new_line) + 41 + (i * 10));
}
else
{
rigafile = String.Format("{0:000000000}", DatiRegistrati[i]);
encoding.GetBytes(rigafile).CopyTo(byte_riga, ((num_righe - 1) * new_line) + 32 + (i * 10));
rigafile = ";";
encoding.GetBytes(rigafile).CopyTo(byte_riga, ((num_righe - 1) * new_line) + 41 + (i * 10));
}
}
rigafile = "\r\n";
encoding.GetBytes(rigafile).CopyTo(byte_riga, ((num_righe - 1) * new_line) + new_line - 2);
<big></big> <big>for (i = 0; i < numero_word_O; i++)
{
DatiDaInviare[i] = i;
}
RSLinxOPCgroup.SyncWrite(3, ref SyncServerHandles, ref DatiDaInviare, out SyncErrors2);</big> Thread.Sleep(tempo_campionamento);
}
if (File.Exists(fileName))
{
File.WriteAllBytes(fileName, byte_riga);
rigafile = "";
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Errore",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
Regards
rodrigo
|
|
|
|
|
I need help on a project which requires me to make use of Expression Blend & WPF to create a software that can:
1. accept & analysis user ink input
2. search library/source/database for similar shape/character
3. display 4 results found
4. display user's choice on textbox
In case I didnt make myself clear, think of it as iPhone's chinese character writing software. Just that in my case im using some image symbols instead of chinese language. Im using Visual Studio 2008 & Expression Blend 2.
|
|
|
|
|