noam_dz
asked on
K. C#3 3 Struct q.
I am learning c# via KeyStone CBT.
But the CBT contains excresises without answers.
Here are my answers.
The build runes OK.
using System;
namespace structNamespace
{
public struct MyStruct
{
//1. Define a public struct with the name MyStruct with a default Constructor. 3.2
}
public struct Region
{
//2. Define a public struct called Region that defines two private strings, City and State. Next, add a Constructor that accepts those string parameters. Lastly, add a method that concatenates those strings and gives you the normal City, State format. 3.2
private string City;
private string State;
public Region (string City,string State)
{
this.City=City;
this.State=State;
}
public string GetRegion()
{
return City + "," + State;
}
}
// Region region= new Region("boston","Californi a");
//4. Define a class called CallStruct that uses the Region struct. Have it send Region the parameters it needs for its constructor and for instantiation, then call the GetRegion method of the Region struct to return the values you set it. 3.3
class callStruct
{
static void Main(string[] args)
{
Region region= new Region("boston","Californi a");
String writeRegion = region.GetRegion ();
//5. Use the {0} syntax in two WriteLine statements designed to output to the console the Region information obtained in the preceding Exercise. 3.3
Console.WriteLine ("the region is {0}",writeRegion);
}
}
}
But the CBT contains excresises without answers.
Here are my answers.
The build runes OK.
using System;
namespace structNamespace
{
public struct MyStruct
{
//1. Define a public struct with the name MyStruct with a default Constructor. 3.2
}
public struct Region
{
//2. Define a public struct called Region that defines two private strings, City and State. Next, add a Constructor that accepts those string parameters. Lastly, add a method that concatenates those strings and gives you the normal City, State format. 3.2
private string City;
private string State;
public Region (string City,string State)
{
this.City=City;
this.State=State;
}
public string GetRegion()
{
return City + "," + State;
}
}
// Region region= new Region("boston","Californi
//4. Define a class called CallStruct that uses the Region struct. Have it send Region the parameters it needs for its constructor and for instantiation, then call the GetRegion method of the Region struct to return the values you set it. 3.3
class callStruct
{
static void Main(string[] args)
{
Region region= new Region("boston","Californi
String writeRegion = region.GetRegion ();
//5. Use the {0} syntax in two WriteLine statements designed to output to the console the Region information obtained in the preceding Exercise. 3.3
Console.WriteLine ("the region is {0}",writeRegion);
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER