Option Strict On
Imports System
Namespace JaggedArray
Public Class Tester
Public Sub Run()
Const rowsUB As Integer = 3
Const rowZero As Integer = 5
Const rowOne As Integer = 2
Const rowTwo As Integer = 3
Const rowThree As Integer = 5
Dim i As Integer
'declare the jagged array as 4 rows high
Dim jaggedArray(rowsUB)() As Integer
'declare the rows of various lengths
ReDim jaggedArray(0)(rowZero)
ReDim jaggedArray(1)(rowOne)
ReDim jaggedArray(2)(rowTwo)
ReDim jaggedArray(3)(rowThree)
'fill some (but not all) elements of the rows
jaggedArray(0)(3) = 15
jaggedArray(1)(1) = 12
jaggedArray(2)(1) = 9
jaggedArray(2)(2) = 99
jaggedArray(3)(0) = 10
jaggedArray(3)(1) = 11
jaggedArray(3)(2) = 12
jaggedArray(3)(3) = 13
jaggedArray(3)(4) = 14
For i = 0 To rowZero
Console.WriteLine("jaggedArray(0)({0}) = {1}", i, jaggedArray(0)(i))
Next
For i = 0 To rowOne
Console.WriteLine("jaggedArray(1)({0}) = {1}", i, jaggedArray(1)(i))
Next
For i = 0 To rowTwo
Console.WriteLine("jaggedArray(2)({0}) = {1}", i, jaggedArray(2)(i))
Next
For i = 0 To rowThree
Console.WriteLine("jaggedArray(3)({0}) = {1}", i, jaggedArray(3)(i))
Next
End Sub
Public Shared Sub Main()
Dim t As Tester = New Tester()
t.Run()
End Sub
End Class
End Namespace
Option Strict On
Imports System
Namespace ReverseAndSort
Class Tester
Public Shared Sub DisplayArray(ByVal theArray() As Object)
Dim obj As Object
For Each obj In theArray
Console.WriteLine("Value : {0}", obj)
Next obj
Console.WriteLine(ControlChars.Lf)
End Sub 'DisplayArray
Public Sub Run()
Dim myArray As [String]() = {"Who", "is", "John", "Galt"}
Console.WriteLine("Display myArray...")
DisplayArray(myArray)
Console.WriteLine("Reverse and display myArray...")
Array.Reverse(myArray)
DisplayArray(myArray)
Dim myOtherArray As [String]() = _
{"We", "Hold", "These", "Truths", "To", "Be", "Self", "Evident"}
Console.WriteLine("Display myOtherArray...")
DisplayArray(myOtherArray)
Console.WriteLine("Sort and display myOtherArray...")
Array.Sort(myOtherArray)
DisplayArray(myOtherArray)
End Sub 'run
Public Shared Sub Main()
Dim t As New Tester()
t.Run()
End Sub 'Main
End Class 'Tester
End Namespace 'Reverse and sort
Option Strict On
Imports System
Namespace Indexers
'a simplified ListBox control
Public Class ListBoxTest
Private strings(255) As String
Private ctr As Integer = 0
'initialize the list box with strings
'because you cannot nkow how many strings will be added
'you use the keyword ParamArray
Public Sub New(ByVal ParamArray initialStrings() As String)
Dim s As String
'copy the strings passed in to the constructor
For Each s In initialStrings
strings(ctr) = s
ctr += 1
Next
End Sub
'add a single string to the end of the list box
Public Sub Add(ByVal theString As String)
If ctr >= strings.Length Then
'handle bad index
Else
strings(ctr) = theString
ctr += 1
End If
End Sub
'allow array-like access
Default Public Property Item(ByVal index As Integer) As String
Get
If index < 0 Or index >= strings.Length Then
'handle bad index
Else
Return strings(index)
End If
End Get
Set(ByVal value As String)
If index >= ctr Then
'handle error
Else
strings(index) = value
End If
End Set
End Property
'publish how many strings you hold
Public Function Count() As Integer
Return ctr
End Function
End Class
Public Class Tester
Public Sub Run()
'create a new list box and initialize
Dim lbt As New ListBoxTest("Hello", "World")
Dim i As Integer
Console.WriteLine("After creation...")
For i = 0 To lbt.Count - 1
Console.WriteLine("lbt({0}): {1}", i, lbt(i))
Next
lbt.Add("who")
lbt.Add("is")
lbt.Add("john")
lbt.Add("galt")
Console.WriteLine("After adding strings")
For i = 0 To lbt.Count - 1
Console.WriteLine("lbt({0}): {1}", i, lbt(i))
Next
'test the access
Dim subst As String = "Universe"
lbt(1) = subst
'access all the strings
Console.WriteLine("After editing strings...")
For i = 0 To lbt.Count - 1
Console.WriteLine("lbt({0}): {1}", i, lbt(i))
Next
End Sub
Public Shared Sub Main()
Dim t As New Tester()
t.Run()
End Sub
End Class
End Namespace
using System;
using System.Data;
namespace CreateUniqueConstraint
{
class Program
{
static void Main(string[] args)
{
//create a table
DataTable dt = new DataTable("Table-1");
//add two columns
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Field1", typeof(string)).MaxLength = 50;
//create a unique constraint on Field1
UniqueConstraint uc1 = new UniqueConstraint("UniqueConstraint", dt.Columns["Field1"]);
//add the constraints to the table
dt.Constraints.Add(uc1);
//output the properties of the table constraint added
OutputConstraintProperties(dt);
//verify the unique constraint by adding rows
try
{
AddRow(dt, 1, "Value 1");
AddRow(dt, 2, "Value 2");
AddRow(dt, 3, "Value 2");
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
private static void OutputConstraintProperties(DataTable dt)
{
Console.WriteLine("DataTable {0} => Constraint Properties: ", dt.TableName);
Console.WriteLine("\tName = ", dt.Constraints[0].ConstraintName);
Console.WriteLine("\tIsPrimaryKey = {0}", ((UniqueConstraint)dt.Constraints[0]).IsPrimaryKey);
Console.WriteLine("\tColumns: ");
foreach (DataColumn col in ((UniqueConstraint)dt.Constraints[0]).Columns)
{
Console.WriteLine("\t\t{0}", col.ColumnName);
}
}
private static void AddRow(DataTable dt, int id, string field1)
{
Console.WriteLine("\nAdding row: {0}, {1}", id, field1);
dt.Rows.Add(new object[] { id, field1 });
Console.WriteLine("Row added.");
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment