Instanstiate an object named empObject [duplicate] - vba

This question already has an answer here:
How do Class definitions work in VBA
(1 answer)
Closed 3 months ago.
Assume that you have a class called Employee that has FirstName and LastName as its properties. Write code to instantiate object named empObject and set its first name to "Peter" and last name to "Johns"
public class Enployee

class Employee {
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
}
const empObject = new Employee('Peter', 'Johns');
This is how it is done in Javascript.

Related

Hibernate #DynamicUpdate on an immutable entity

Suppose that I have a Hibernate entity whose fields are altered via new instance creation (e.g. Lombok builder).
Does that mean that #DynamicUpdate is not going work? Is is required to change the fields via setters in this case?
P.S. Here is an example
#Table
#Entity
#Builder(toBuilder = true)
class Person {
private String firstName;
private String lastName;
}
...
Person person = personRepository.findById(1).orElseThrow();
personRepository.saveAndFlush(
person.toBuilder()
.firstName("newName")
.build() // creates new instance
);

How to bind enum values with SWT Combo

I want to bind enum values to SWT Combo.
Lets says I am having following code snippet
Person model class
public class Person {
private String name;
private Gender gender;
public setGender(Gender gender) {
this.gender = gender;
}
public Gender getGender() {
return gender;
}
}
Gender enum
public enum Gender {
MALE("male"), FEMALE("female");
}
Binding between combo selection and gender property of Person.
IObservableValue<?> observeWidget = WidgetProperties.selection().observe(combo);
IObservableValue<?> observeModel = PojoProperties.value(person.getClass(), "gender").observe(person);
Binding between combo values and enum Gender.
IObservableList<?> observeWidget = WidgetProperties.items().observe(combo);
IObservableList<?> observableList = PojoProperties.list(enumGender.getClass(), "???? property name ????").observe(enumGender);
How i can achive above binding ??
I want to bind Gender.values() (this method default in every enum and return an array of string) with SWT combo.
For binding, we must provide property name in API call, but enum does not contain any such property.
I know we can achieve this using ComboBoxViewer like
comboViwer.setInput(Gender.values()).
but I want to do bind any enum with combo.
Did you check the snippet Snippet034:
https://github.com/eclipse/eclipse.platform.ui/blob/master/examples/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet034ComboViewerAndEnum.java
Basing on the version you are using maybe you have to adapt the call to "bindValue" passing less/more parameters

What is the codefirst syntax to add a simple list of an elementary type to a database table?

Hopefully I simplified everything well to focus on my question. I am defining a class to create a database using code first EF. When I do the following:
Public Class question
Public Property QuestionID As Integer
Public Property Answers As String
End Class
I get this in the migration file:
CreateTable(
"dbo.questions",
Function(c) New With
{
.QuestionID = c.Int(nullable:=False, identity:=True),
.Answers = c.String()
}) _
.PrimaryKey(Function(t) t.QuestionID)
So far, good. Now, when I try to change the second property to be a list of string as follows:
Public Class question
Public Property QuestionID As Integer
Public Property Answers As New List(Of String)
End Class
I get this in the migration file:
CreateTable(
"dbo.questions",
Function(c) New With
{
.QuestionID = c.Int(nullable:=False, identity:=True)
}) _
.PrimaryKey(Function(t) t.QuestionID)
The way I've written it does not generate a list of strings. Hopefully someone can illuminate the basic concept I'm missing here.
Don't create a text field with the answers, no!
You need to create a new table, the Answers table, then add a ForeignKey to your question table like this:
Public Class question
Public Property QuestionID As Integer
'Public Property Answers As String
Public Overridable Property Answers() as ICollection (Of Answer)
End Class
Public Class Answer
Public Property ID As Integer
Public Property questionID as Integer
Public Property Answers As String
<ForeignKey("questionID")>
Public Overridable Property Question as question
End Class
Then you just do this (C#):
IEnumerable<answer> answers=question1.Answers.ToList();

How to identify an object and make a relationship among them [c#] [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
What is the best way to identify the objects and their Relationship between them ?
As i am new to Programming[c# windows application],I am finding difficult in making relationship between objects .
Can anyone suggest me the best way to start with?
Thanks,
Karthik
You should probably read up on object oriented programming and design. Then read up on C# programming.
Here are a few book suggestions:
Head First Object Oriented Analysis and Design
Beginning C# 3.0: An Introduction to Object Oriented Programming
Then when you're ready, you can move on to Jon Skeet's book =)
If you're not looking to purchase a book, maybe take a look at this site. It covers key object oriented design principles.
I'll try and point you in the right direction with a contrived example:
Suppose we want to make a simple management application for supermarket staff. We are required to store some personal information and work-related information, so we'll need name, address, date started, department, and position.
So now we can try and summarise this specification and think about how to model each bit of data.
StaffMember
Name
Address
Date Started
Department
Department
Name
Position
Position
Title
Roles
Now we can map these ideas to c# classes and use properties to hold the bits of data we need:
class StaffMember
{
public string Name { get; set; }
public string Address { get; set; }
public DateTime DateStarted { get; set; }
public Department Department { get; set; } // class Department
}
class Department
{
public string Name { get; set; }
public Position Position { get; set; } // class Position
}
class Position
{
public string Title { get; set; }
public string PrimaryRole { get; set; }
}
Sample usage:
static void Main()
{
StaffMember employee = new StaffMember();
employee.Name = "Ali Gray";
employee.Address = "123 Abc Street";
employee.DateStarted = DateTime.Now;
// Now add the employees department
employee.Department = new Department();
employee.Department.Name = "Checkout";
// Now add the employees position
employee.Department.Position = new Position();
employee.Department.Position.Title = "Bag Packer";
employee.Department.Position.PrimaryRole = "Pack bags";
}
Obviously this is an extremely simplified example, but hopefully it'll help you on your way to understanding oo design.

Dynamic Table Names in Linq to SQL

Hi all I have a horrid database I gotta work with and linq to sql is the option im taking to retrieve data from. anywho im trying to reuse a function by throwing in a different table name based on a user selection and there is no way to my knowledge to modify the TEntity or Table<> in a DataContext Query.
This is my current code.
public void GetRecordsByTableName(string table_name){
string sql = "Select * from " + table_name;
var records = dataContext.ExecuteQuery</*Suppossed Table Name*/>(sql);
ViewData["recordsByTableName"] = records.ToList();
}
I want to populate my ViewData with Enumerable records.
You can call the ExecuteQuery method on the DataContext instance. You will want to call the overload that takes a Type instance, outlined here:
http://msdn.microsoft.com/en-us/library/bb534292.aspx
Assuming that you have a type that is attributed correctly for the table, passing that Type instance for that type and the SQL will give you what you want.
As casperOne already answered, you can use ExecuteQuery method first overload (the one that asks for a Type parameter). Since i had a similar issue and you asked an example, here is one:
public IEnumerable<YourType> RetrieveData(string tableName, string name)
{
string sql = string.Format("Select * FROM {0} where Name = '{1}'", tableName, name);
var result = YourDataContext.ExecuteQuery(typeof(YourType), sql);
return result;
}
Pay attention to YourType since you will have to define a type that has a constructor (it can't be abstract or interface). I'd suggest you create a custom type that has exactly the same attributes that your SQL Table. If you do that, the ExecuteQuery method will automatically 'inject' the values from your table to your custom type. Like that:
//This is a hypothetical table mapped from LINQ DBML
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.ClientData")]
public partial class ClientData : INotifyPropertyChanging, INotifyPropertyChanged
{
private int _ID;
private string _NAME;
private string _AGE;
}
//This would be your custom type that emulates your ClientData table
public class ClientDataCustomType
{
private int _ID;
private string _NAME;
private string _AGE;
}
So, on the former example, the ExecuteQuery method would be:
var result = YourDataContext.ExecuteQuery(typeof(ClientDataCustomType), sql);