Sylvia Langfield

I am trying to convert programming notes for students from Pascal. I can't find the equivalent in C# or VB for

If MonthNumber In [1,3,5,7,8,10,12]

Then NumberOfDays := 31

Else

If MonthNumber In [4,6,9,11]

Then NumberOfDays := 30

Else

If MonthNumber = 2

Then NumberOfDays := 28

Else Writeln (¡®Invalid month number¡¯);

Do set operations not exist in these languages

Thanks




Re: Visual C# Language set operators in C# or VB 2005

IsshouFuuraibou

I know that in C# you can initialize a new array (set ) with just new Type[] { value1, value2, value3, value4 }; where type is the name of the type used. for example

Code Snippet

if ( Array.IndexOf( new int[] { 1, 3, 5, 7, 8, 10, 12 }, MonthNumber ) != -1 )
{
}



should be the same check as the first If ... In [] above. There are probably other ways to optimize the check, but that should be a similar functionality.





Re: Visual C# Language set operators in C# or VB 2005

Mamush

Try this I am sure it works

Code Snippet

private void TEST1()

{

// Your Code in C# will Look Like This

int MonthNumber = 1;

int NumberOfDays;

List<int> z31 = new List<int>((new int[] { 1, 3, 5, 7, 8, 10, 12 }));

List<int> z30 = new List<int>((new int[] { 4, 6, 9, 11 }));

if (z31.Contains(MonthNumber))

NumberOfDays = 31;

else if (z30.Contains(MonthNumber))

NumberOfDays = 30;

else if (MonthNumber == 2)

{

NumberOfDays = 28;

}

else

MessageBox.Show("Invalid month number");

//A diffrent Alternetive

Hashtable month = new Hashtable();

month.Add(1, 31); month.Add(1, 28);

month.Add(1, 31); month.Add(1, 30);

month.Add(1, 31); month.Add(1, 30);

month.Add(1, 31); month.Add(1, 31);

month.Add(1, 30); month.Add(1, 31);

month.Add(1, 30); month.Add(1, 31);

if (month.ContainsKey(MonthNumber))

{

NumberOfDays = (int)month[MonthNumber];

}

else

MessageBox.Show("Invalid month number");

//A much better solution

if (MonthNumber > 0 && MonthNumber < 13)

{

NumberOfDays = DateTime.DaysInMonth(DateTime.Now.Year, MonthNumber);

}

else

MessageBox.Show("Invalid month number");

}

Hope this helps

Cheers






Re: Visual C# Language set operators in C# or VB 2005

JohnWein

I think the closest code in VB2005 would be:

Code Snippet

Select Case MonthNumber

Case 1, 3, 5, 7, 8, 10, 12

NumberOfDays = 31

Case 4, 6, 9, 11

NumberOfDays = 30

Case 2

NumberOfDays = 28

Case Else

MsgBox("Invalid month number")

End Select

and in C#

Code Snippet

switch (MonthNumber)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

NumberOfDays = 31;

break;

case 4:

case 6:

case 9:

case 11:

NumberOfDays = 30;

break;

case 2:

NumberOfDays = 28;

break;

default:

MsgBox("Invalid month number");

break;

}

Using .NET classes there are many more ways to handle the situation in both languages.





Re: Visual C# Language set operators in C# or VB 2005

timvw

Afaik, there aren't set operations in the syntax (yet, Language Integrated Query should change that).

For now you can use Array.Exists and List.Exists methods.. (An example of their use can be seen in here )

Anyway, in .Net you have the DateTime struct, so the more sane option would be:

Code Snippet

int month = 10;
return DateTime.DaysInMonth(2007, month);







Re: Visual C# Language set operators in C# or VB 2005

Sylvia Langfield

Thanks, but this looks a bit complicated for students just learning to program. It appears set operators don't exist in C# but they are in the syllabus!






Re: Visual C# Language set operators in C# or VB 2005

Sylvia Langfield

Thank you for the work around. It appears there are no set operators in C# or VB!






Re: Visual C# Language set operators in C# or VB 2005

Sylvia Langfield

Thank you for the work around. It appears there are no set operators in C# or VB!






Re: Visual C# Language set operators in C# or VB 2005

Matthew Watson

There are no set operations like the Pascal ones in C# or VB.

However, in .Net 3.x, there is a class called HashSet which provides Set operations.

See here for more information.




Re: Visual C# Language set operators in C# or VB 2005

Sylvia Langfield

Thank you. I will have to wait for .Net 3.x then!






Re: Visual C# Language set operators in C# or VB 2005

IsshouFuuraibou

3.0 is out and can be installed to work with VisualStudio 2005, 3.5 is available as beta and will be released early next year with Visual Studio 2008.